Просмотр исходного кода

Proteus: Updated framework. (#3142)

* Proteus: Updated framework.

* Removed commented sections.
noboomu 7 лет назад
Родитель
Сommit
827242151b

+ 2 - 2
frameworks/Java/proteus/benchmark_config.json

@@ -4,8 +4,8 @@
     "default": {
       "setup_file": "setup",
       "json_url": "/json",
-      "db_url": "/db/postgres",
-      "fortune_url": "/fortunes/postgres",
+      "db_url": "/db",
+      "fortune_url": "/fortunes",
       "plaintext_url": "/plaintext",
       "port": 8080,
       "approach": "Realistic",

+ 1 - 1
frameworks/Java/proteus/conf/application.conf

@@ -62,7 +62,7 @@ postgres
    {
         description="postgresql server"
         jdbcUrl= "jdbc:postgresql://TFB-database:5432/hello_world",
-        maximumPoolSize=48
+        maximumPoolSize=256
         username = benchmarkdbuser
         password = benchmarkdbpass
   }

+ 1 - 1
frameworks/Java/proteus/pom.xml

@@ -215,7 +215,7 @@
 		<dependency>
 			<groupId>io.sinistral</groupId>
 			<artifactId>proteus-core</artifactId>
-			<version>0.1.7.2-SNAPSHOT</version>
+			<version>0.1.7.4-SNAPSHOT</version>
 			
 		</dependency>
 			<dependency>

+ 1 - 1
frameworks/Java/proteus/setup.sh

@@ -5,4 +5,4 @@ fw_depends postgresql mysql java maven
 
 mvn -U clean package
 cd target
-java -Dlogback.configurationFile="conf/logback.xml" -Dconfig.file="conf/application.conf" -server  -Xms1g -Xmx2g -classpath "./proteus-techempower-1.0.0.jar:lib/*" io.sinistral.ExampleApplication 
+java -Dlogback.configurationFile="conf/logback.xml" -Dconfig.file="conf/application.conf" -Xms2g -Xmx2g  -XX:+AggressiveOpts -server   -XX:-UseBiasedLocking -XX:+UseStringDeduplication -Djava.net.preferIPv4Stack=true -classpath "./proteus-techempower-1.0.0.jar:lib/*" io.sinistral.ExampleApplication 

+ 42 - 2
frameworks/Java/proteus/src/main/java/io/sinistral/ExampleApplication.java

@@ -6,10 +6,12 @@ import java.nio.ByteBuffer;
 
 import com.jsoniter.output.EncodingMode;
 import com.jsoniter.output.JsonStream;
+import com.mysql.jdbc.log.Log;
 
 import io.sinistral.controllers.Benchmarks;
 import io.sinistral.models.Message;
 import io.sinistral.proteus.ProteusApplication;
+import io.sinistral.proteus.controllers.handlers.BenchmarksRouteSupplier;
 import io.sinistral.proteus.services.AssetsService;
 import io.sinistral.proteus.services.SwaggerService;
 import io.sinistral.services.MySqlService;
@@ -35,6 +37,44 @@ public class ExampleApplication extends ProteusApplication
     	 
     }
     
+    @Override
+    public void buildServer()
+    {
+    	int httpPort = config.getInt("application.ports.http");
+		
+		if(System.getProperty("http.port") != null)
+		{
+			httpPort = Integer.parseInt(System.getProperty("http.port"));
+		}
+		
+		System.out.println("httpPort: " + httpPort);
+		
+		this.ports.add(httpPort);
+		
+		Benchmarks controller = this.getInjector().getInstance(Benchmarks.class);
+		
+		HttpHandler pathsHandler = new BenchmarksRouteSupplier(controller, null).get();
+		
+		HttpHandler rootHandler = new SetHeaderHandler(pathsHandler, "Server", config.getString("globalHeaders.Server"));
+		
+		Undertow.Builder undertowBuilder = Undertow.builder().addHttpListener(httpPort, config.getString("application.host"))
+				.setBufferSize(16 * 1024)
+				.setIoThreads(Runtime.getRuntime().availableProcessors() * 2)
+//				.setServerOption(UndertowOptions.ALWAYS_SET_DATE, true)
+				.setSocketOption(org.xnio.Options.BACKLOG, 10000)
+				.setServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, false)
+				.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, false)
+                .setServerOption(UndertowOptions.ENABLE_CONNECTOR_STATISTICS, false)
+				.setServerOption(UndertowOptions.MAX_ENTITY_SIZE, config.getBytes("undertow.server.maxEntitySize"))
+				.setWorkerThreads(200)
+				.setHandler(rootHandler);
+		
+		this.undertow = undertowBuilder.build();
+ 		
+		log.debug("Completed server build!");
+
+    }
+    
     
     public static void main( String[] args )
     {
@@ -48,8 +88,8 @@ public class ExampleApplication extends ProteusApplication
 		app.addService(MySqlService.class);
 
 		app.addService(PostgresService.class);
- 
-		app.addController(Benchmarks.class);  
+// 
+//		app.addController(Benchmarks.class);  
 		
 		app.start();
 		

+ 16 - 5
frameworks/Java/proteus/src/main/java/io/sinistral/controllers/Benchmarks.java

@@ -6,7 +6,12 @@ package io.sinistral.controllers;
 import static io.undertow.util.Headers.CONTENT_TYPE;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.StringWriter;
+import java.net.URISyntaxException;
+import java.net.URL;
 import java.nio.ByteBuffer;
+import java.nio.file.Paths;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -25,6 +30,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
 import com.fizzed.rocker.runtime.StringBuilderOutput;
 import com.github.mustachejava.DefaultMustacheFactory;
+import com.github.mustachejava.Mustache;
 import com.github.mustachejava.MustacheFactory;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
@@ -61,14 +67,19 @@ public class Benchmarks
 	private static final String HTML_UTF8_TYPE = io.sinistral.proteus.server.MediaType.TEXT_HTML_UTF8.toString(); 
 	private static final ByteBuffer MESSAGE_BUFFER;
     private static final String MESSAGE = "Hello, World!";
-    
+    private static final String FORTUNES_TEMPLATE = Benchmarks.class.getResource("/templates/Fortunes.mustache").getFile();
+ 
 	private static final ObjectMapper DEFAULT_MAPPER = new ObjectMapper();
  
+	private final MustacheFactory mustacheFactory = new DefaultMustacheFactory();
      
 
  
     static {
+    	    	
+
     	MESSAGE_BUFFER = ByteBuffer.allocateDirect(MESSAGE.length());
+    	
     	try {
     		MESSAGE_BUFFER.put(MESSAGE.getBytes("US-ASCII"));
      } catch (Exception e) {
@@ -113,7 +124,7 @@ public class Benchmarks
 	
 	
 	@GET
-	@Path("/db/postgres")
+	@Path("/db")
 	@Blocking
 	@ApiOperation(value = "World postgres db endpoint",   httpMethod = "GET" , response = World.class)
 	public void dbPostgres(HttpServerExchange exchange)
@@ -145,7 +156,8 @@ public class Benchmarks
 		  
  		 
 	}
-	
+
+
 	
 	@GET
 	@Path("/db/mysql")
@@ -222,10 +234,9 @@ public class Benchmarks
 	        exchange.getResponseSender().send(render);  
 		  
 	}
-
 	
 	@GET
-	@Path("/fortunes/postgres")
+	@Path("/fortunes")
 	@Blocking
 	@Produces(MediaType.TEXT_HTML)
 	@ApiOperation(value = "Fortunes postgres endpoint",   httpMethod = "GET"  )

+ 110 - 0
frameworks/Java/proteus/src/main/java/io/sinistral/proteus/controllers/handlers/BenchmarksRouteSupplier.java

@@ -0,0 +1,110 @@
+package io.sinistral.proteus.controllers.handlers;
+
+import static io.sinistral.proteus.server.Extractors.*;
+
+import com.google.inject.Inject;
+import com.google.inject.name.Named;
+import io.sinistral.controllers.Benchmarks;
+import io.undertow.server.HandlerWrapper;
+import io.undertow.server.HttpHandler;
+import io.undertow.server.RoutingHandler;
+import io.undertow.server.handlers.PathHandler;
+
+import java.lang.String;
+import java.util.Map;
+import java.util.function.Supplier;
+
+/**
+ * 
+ * This would normally be generated by the ProteusApplication.
+ * @author noboomu
+ *
+ */
+public class BenchmarksRouteSupplier implements Supplier<HttpHandler> {
+  protected final Benchmarks benchmarksController;
+
+  protected final Map<String, HandlerWrapper> registeredHandlerWrappers;
+
+  @Inject
+  public BenchmarksRouteSupplier(Benchmarks benchmarksController,
+      @Named("registeredHandlerWrappers") Map<String, HandlerWrapper> registeredHandlerWrappers) {
+    this.benchmarksController = benchmarksController;
+    this.registeredHandlerWrappers = registeredHandlerWrappers;
+  }
+
+  public HttpHandler get() {
+	  
+    final PathHandler router = new PathHandler();
+
+    final io.undertow.server.HttpHandler benchmarksDbPostgresHandler = new io.undertow.server.HttpHandler() {
+      @java.lang.Override
+      public void handleRequest(final io.undertow.server.HttpServerExchange exchange) throws
+          java.lang.Exception {
+
+        benchmarksController.dbPostgres(exchange);
+      }
+    };
+
+    router.addExactPath("/db",new io.undertow.server.handlers.BlockingHandler(benchmarksDbPostgresHandler));
+    
+
+
+    final io.undertow.server.HttpHandler benchmarksDbMySqlHandler = new io.undertow.server.HttpHandler() {
+      @java.lang.Override
+      public void handleRequest(final io.undertow.server.HttpServerExchange exchange) throws
+          java.lang.Exception {
+
+        benchmarksController.dbMySql(exchange);
+      }
+    };
+
+    router.addExactPath("/db/mysql",new io.undertow.server.handlers.BlockingHandler(benchmarksDbMySqlHandler));
+
+    final io.undertow.server.HttpHandler benchmarksFortunesMysqlHandler = new io.undertow.server.HttpHandler() {
+      @java.lang.Override
+      public void handleRequest(final io.undertow.server.HttpServerExchange exchange) throws
+          java.lang.Exception {
+
+        benchmarksController.fortunesMysql(exchange);
+      }
+    };
+
+    router.addExactPath("/fortunes/mysql",new io.undertow.server.handlers.BlockingHandler(benchmarksFortunesMysqlHandler));
+
+    final io.undertow.server.HttpHandler benchmarksFortunesPostgresHandler = new io.undertow.server.HttpHandler() {
+      @java.lang.Override
+      public void handleRequest(final io.undertow.server.HttpServerExchange exchange) throws
+          java.lang.Exception {
+
+        benchmarksController.fortunesPostgres(exchange);
+      }
+    };
+
+    router.addExactPath("/fortunes",new io.undertow.server.handlers.BlockingHandler(benchmarksFortunesPostgresHandler));
+
+    final io.undertow.server.HttpHandler benchmarksPlaintextHandler = new io.undertow.server.HttpHandler() {
+      @java.lang.Override
+      public void handleRequest(final io.undertow.server.HttpServerExchange exchange) throws
+          java.lang.Exception {
+
+        benchmarksController.plaintext(exchange);
+      }
+    };
+
+    router.addExactPath("/plaintext",benchmarksPlaintextHandler);
+
+    final io.undertow.server.HttpHandler benchmarksJsonHandler = new io.undertow.server.HttpHandler() {
+      @java.lang.Override
+      public void handleRequest(final io.undertow.server.HttpServerExchange exchange) throws
+          java.lang.Exception {
+
+        benchmarksController.json(exchange);
+      }
+    };
+
+    router.addExactPath("/json",benchmarksJsonHandler);
+
+
+    return router;
+  }
+}