Kaynağa Gözat

add smart-servlet (#6234)

* update smart-http to 1.0.15-SNAPSHOT

* optimize performance

* update smart-http to 1.0.15

* update aio-enhance to 1.0.1-SNAPSHOT

* optimize performance

* optimize performance

* update smart-http to 1.0.16-SNAPSHOT

* update smart-http to 1.0.17-SNAPSHOT

* update aio-enhance to 1.0.2-SNAPSHOT

* disable jni

* update smart-http to 1.0.18

* 还原配置

* upgrade smart-http

* add smart-servlet

* 优化diamante

* 优化代码

* update smart-socket to 1.5.5-SNAPSHOT

Co-authored-by: 三刀 <[email protected]>
三刀 4 yıl önce
ebeveyn
işleme
01fbaa8b79

+ 19 - 0
frameworks/Java/smart-socket/benchmark_config.json

@@ -23,6 +23,25 @@
         "display_name": "smart-socket",
         "notes": "",
         "versus": "smart-socket"
+      },
+      "smart-servlet": {
+        "json_url": "/json",
+        "plaintext_url": "/plaintext",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Platform",
+        "database": "Postgres",
+        "framework": "None",
+        "language": "Java",
+        "flavor": "None",
+        "orm": "Raw",
+        "platform": "smartboot",
+        "webserver": "None",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "smart-servlet",
+        "notes": "",
+        "versus": "smart-servlet"
       }
     }
   ]

+ 7 - 8
frameworks/Java/smart-socket/pom.xml

@@ -6,22 +6,21 @@
     <artifactId>smart-socket-benchmark</artifactId>
     <version>1.0</version>
     <packaging>jar</packaging>
-    <parent>
-        <groupId>org.smartboot.http</groupId>
-        <artifactId>smart-http-parent</artifactId>
-        <version>1.0.19</version>
-    </parent>
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <maven.compiler.source>11</maven.compiler.source>
         <maven.compiler.target>11</maven.compiler.target>
         <log4j.version>2.11.0</log4j.version>
         <aio-enhance.version>1.0.2</aio-enhance.version>
+        <smartservlet.version>0.1.2-SNAPSHOT</smartservlet.version>
+        <smartsocket.version>1.5.5-SNAPSHOT</smartsocket.version>
     </properties>
+
     <dependencies>
         <dependency>
-            <groupId>org.smartboot.http</groupId>
-            <artifactId>smart-http-server</artifactId>
+            <groupId>org.smartboot.servlet</groupId>
+            <artifactId>servlet-core</artifactId>
+            <version>${smartservlet.version}</version>
         </dependency>
         <dependency>
             <groupId>org.smartboot.aio</groupId>
@@ -36,7 +35,7 @@
         <dependency>
             <groupId>org.smartboot.socket</groupId>
             <artifactId>aio-pro</artifactId>
-            <version>1.5.1</version>
+            <version>${smartsocket.version}</version>
         </dependency>
         <dependency>
             <groupId>com.jsoniter</groupId>

+ 10 - 0
frameworks/Java/smart-socket/smart-socket-smart-servlet.dockerfile

@@ -0,0 +1,10 @@
+FROM maven:3.6.1-jdk-11-slim as maven
+WORKDIR /smart-socket
+COPY pom.xml pom.xml
+COPY src src
+RUN mvn compile assembly:single -q
+
+FROM openjdk:11.0.3-jdk-slim
+WORKDIR /smart-socket
+COPY --from=maven /smart-socket/target/smart-socket-benchmark-1.0-jar-with-dependencies.jar app.jar
+CMD ["java", "-server", "-XX:+UseNUMA", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-cp", "app.jar", "org.smartboot.servlet.Bootstrap"]

+ 1 - 1
frameworks/Java/smart-socket/src/main/java/org/smartboot/http/Message.java → frameworks/Java/smart-socket/src/main/java/org/smartboot/Message.java

@@ -1,4 +1,4 @@
-package org.smartboot.http;
+package org.smartboot;
 
 /**
  * @author 三刀

+ 1 - 0
frameworks/Java/smart-socket/src/main/java/org/smartboot/http/Bootstrap.java

@@ -10,6 +10,7 @@ package org.smartboot.http;
 
 import com.zaxxer.hikari.HikariConfig;
 import com.zaxxer.hikari.HikariDataSource;
+import org.smartboot.Message;
 import org.smartboot.aio.EnhanceAsynchronousChannelProvider;
 import org.smartboot.http.server.HttpMessageProcessor;
 import org.smartboot.http.server.HttpRequestProtocol;

+ 17 - 0
frameworks/Java/smart-socket/src/main/java/org/smartboot/http/JsonUtil.java

@@ -5,6 +5,7 @@ import com.jsoniter.output.JsonStreamPool;
 import com.jsoniter.spi.JsonException;
 import com.jsoniter.spi.Slice;
 
+import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 
 /**
@@ -27,4 +28,20 @@ public class JsonUtil {
             JsonStreamPool.returnJsonStream(stream);
         }
     }
+
+    public static void writeJsonBytes(HttpServletResponse httpResponse, Object obj) {
+        JsonStream stream = JsonStreamPool.borrowJsonStream();
+        try {
+            stream.reset(null);
+            stream.writeVal(obj.getClass(), obj);
+            stream.buffer();
+            Slice slice = stream.buffer();
+            httpResponse.setContentLength(slice.tail());
+            httpResponse.getOutputStream().write(slice.data(), 0, slice.tail());
+        } catch (IOException e) {
+            throw new JsonException(e);
+        } finally {
+            JsonStreamPool.returnJsonStream(stream);
+        }
+    }
 }

+ 86 - 0
frameworks/Java/smart-socket/src/main/java/org/smartboot/servlet/Bootstrap.java

@@ -0,0 +1,86 @@
+package org.smartboot.servlet;
+
+import org.smartboot.aio.EnhanceAsynchronousChannelProvider;
+import org.smartboot.http.server.HttpMessageProcessor;
+import org.smartboot.http.server.HttpRequestProtocol;
+import org.smartboot.http.server.Request;
+import org.smartboot.servlet.conf.ServletInfo;
+import org.smartboot.socket.StateMachineEnum;
+import org.smartboot.socket.buffer.BufferFactory;
+import org.smartboot.socket.buffer.BufferPagePool;
+import org.smartboot.socket.extension.plugins.MonitorPlugin;
+import org.smartboot.socket.extension.processor.AbstractMessageProcessor;
+import org.smartboot.socket.transport.AioQuickServer;
+import org.smartboot.socket.transport.AioSession;
+
+import java.io.IOException;
+
+/**
+ * @author 三刀([email protected]
+ * @version V1.0 , 2020/12/22
+ */
+public class Bootstrap {
+
+    public static void main(String[] args) {
+        System.setProperty("java.nio.channels.spi.AsynchronousChannelProvider", EnhanceAsynchronousChannelProvider.class.getName());
+
+        ServletHttpHandle httpHandle = new ServletHttpHandle();
+        ContainerRuntime containerRuntime = new ContainerRuntime("/");
+        // plaintext
+        ServletInfo plainTextServletInfo = new ServletInfo();
+        plainTextServletInfo.setServletName("plaintext");
+        plainTextServletInfo.setServletClass(HelloWorldServlet.class.getName());
+        plainTextServletInfo.addMapping("/plaintext");
+        containerRuntime.getDeploymentInfo().addServlet(plainTextServletInfo);
+
+        // json
+        ServletInfo jsonServletInfo = new ServletInfo();
+        jsonServletInfo.setServletName("json");
+        jsonServletInfo.setServletClass(HelloWorldServlet.class.getName());
+        jsonServletInfo.addMapping("/json");
+        containerRuntime.getDeploymentInfo().addServlet(jsonServletInfo);
+        httpHandle.addRuntime(containerRuntime);
+
+        httpHandle.start();
+        HttpMessageProcessor processor = new HttpMessageProcessor();
+        processor.pipeline(httpHandle);
+        http(processor);
+    }
+
+    public static void http(final HttpMessageProcessor processor) {
+        AbstractMessageProcessor<Request> messageProcessor = new AbstractMessageProcessor<Request>() {
+            @Override
+            public void process0(AioSession session, Request msg) {
+                processor.process(session, msg);
+            }
+
+            @Override
+            public void stateEvent0(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
+                processor.stateEvent(session, stateMachineEnum, throwable);
+            }
+        };
+        messageProcessor.addPlugin(new MonitorPlugin(5));
+//        messageProcessor.addPlugin(new SocketOptionPlugin());
+
+        int cpuNum = Runtime.getRuntime().availableProcessors();
+        // 定义服务器接受的消息类型以及各类消息对应的处理器
+        AioQuickServer<Request> server = new AioQuickServer<>(8080, new HttpRequestProtocol(), messageProcessor);
+        server.setThreadNum(cpuNum + 2)
+                .setReadBufferSize(1024 * 4)
+                .setBufferFactory(new BufferFactory() {
+                    @Override
+                    public BufferPagePool create() {
+                        return new BufferPagePool(10 * 1024 * 1024, cpuNum + 2, 64 * 1024 * 1024, true);
+                    }
+                })
+                .setBannerEnabled(false)
+                .setWriteBuffer(1024 * 4, 8);
+
+//        messageProcessor.addPlugin(new BufferPageMonitorPlugin(server, 6));
+        try {
+            server.start();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 23 - 0
frameworks/Java/smart-socket/src/main/java/org/smartboot/servlet/HelloWorldServlet.java

@@ -0,0 +1,23 @@
+package org.smartboot.servlet;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * @author 三刀([email protected]
+ * @version V1.0 , 2020/12/22
+ */
+public class HelloWorldServlet extends HttpServlet {
+    private final static byte[] BODY = "Hello, World!".getBytes();
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        resp.setContentLength(BODY.length);
+        resp.setContentType("text/plain; charset=UTF-8");
+        resp.setBufferSize(0);
+        resp.getOutputStream().write(BODY);
+    }
+}

+ 23 - 0
frameworks/Java/smart-socket/src/main/java/org/smartboot/servlet/JsonServlet.java

@@ -0,0 +1,23 @@
+package org.smartboot.servlet;
+
+import org.smartboot.Message;
+import org.smartboot.http.JsonUtil;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * @author 三刀([email protected]
+ * @version V1.0 , 2020/12/22
+ */
+public class JsonServlet extends HttpServlet {
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        resp.setContentType("application/json");
+        resp.setBufferSize(0);
+        JsonUtil.writeJsonBytes(resp, new Message("Hello, World!"));
+    }
+}