Browse Source

Dependency updates, minor tweaks and plain text test.

trautonen 11 years ago
parent
commit
d662da26c2

+ 10 - 4
spark/README.md

@@ -17,9 +17,9 @@ with Spark filters.
 
 
 ## Infrastructure Software Versions
 ## Infrastructure Software Versions
 
 
-* [Spark 0.9.9.7-SNAPSHOT](http://www.sparkjava.com/)
-* [Hibernate 4.2.0.Final](http://www.hibernate.org/)
-* [Gson 2.2.2](https://code.google.com/p/google-gson/)
+* [Spark 1.1](http://www.sparkjava.com/)
+* [Hibernate 4.2.6.Final](http://www.hibernate.org/)
+* [Gson 2.2.4](https://code.google.com/p/google-gson/)
 
 
 
 
 ## Different test setups
 ## Different test setups
@@ -50,4 +50,10 @@ http://localhost:8080/spark/json
 
 
 http://localhost:4567/db?queries=5
 http://localhost:4567/db?queries=5
 
 
-http://localhost:8080/spring/db?queries=5
+http://localhost:8080/spark/db?queries=5
+
+### Plain Text Test
+
+http://localhost:4567/plaintext
+
+http://localhost:8080/spark/plaintext

+ 1 - 0
spark/benchmark_config

@@ -6,6 +6,7 @@
       "json_url": "/spark/json",
       "json_url": "/spark/json",
       "db_url": "/spark/db",
       "db_url": "/spark/db",
       "query_url": "/spark/db?queries=",
       "query_url": "/spark/db?queries=",
+      "plaintext_url": "/spark/plaintext",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Micro",
       "classification": "Micro",

+ 14 - 11
spark/pom.xml

@@ -10,18 +10,28 @@
 
 
     <properties>
     <properties>
         <java-version>1.7</java-version>
         <java-version>1.7</java-version>
-        <spark-version>0.9.9.7-SNAPSHOT</spark-version>
-        <hibernate-version>4.2.0.Final</hibernate-version>
-        <gson-version>2.2.2</gson-version>
-        <mysql-connector-version>5.1.24</mysql-connector-version>
+        <spark-version>1.1</spark-version>
+        <hibernate-version>4.2.6.Final</hibernate-version>
+        <gson-version>2.2.4</gson-version>
+        <mysql-connector-version>5.1.26</mysql-connector-version>
         <slf4j-version>1.7.5</slf4j-version>
         <slf4j-version>1.7.5</slf4j-version>
     </properties>
     </properties>
 
 
+    <prerequisites>
+        <maven>3.0.0</maven>
+    </prerequisites>
+
     <dependencies>
     <dependencies>
         <dependency>
         <dependency>
             <groupId>com.sparkjava</groupId>
             <groupId>com.sparkjava</groupId>
             <artifactId>spark-core</artifactId>
             <artifactId>spark-core</artifactId>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+            <version>3.0.1</version>
+            <scope>provided</scope>
+        </dependency>
         <dependency>
         <dependency>
             <groupId>com.google.code.gson</groupId>
             <groupId>com.google.code.gson</groupId>
             <artifactId>gson</artifactId>
             <artifactId>gson</artifactId>
@@ -93,13 +103,6 @@
         </profile>
         </profile>
     </profiles>
     </profiles>
     
     
-    <repositories>
-        <repository>
-            <id>Spark repository</id>
-            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
-        </repository>
-    </repositories>
-    
     <build>
     <build>
         <plugins>
         <plugins>
             <plugin>
             <plugin>

+ 5 - 3
spark/src/main/java/hello/web/HibernateUtil.java

@@ -4,6 +4,7 @@ import hello.domain.World;
 
 
 import org.hibernate.Session;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.SessionFactory;
+import org.hibernate.cfg.AvailableSettings;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.dialect.MySQLDialect;
 import org.hibernate.dialect.MySQLDialect;
 import org.hibernate.service.ServiceRegistryBuilder;
 import org.hibernate.service.ServiceRegistryBuilder;
@@ -32,9 +33,10 @@ public class HibernateUtil {
     
     
     private static SessionFactory createSessionFactory() {
     private static SessionFactory createSessionFactory() {
         Configuration configuration = configuration();
         Configuration configuration = configuration();
-        configuration.setProperty("hibernate.dialect ", MySQLDialect.class.getName());
-        configuration.setProperty("hibernate.cache.use_query_cache", "false");
-        configuration.setProperty("show_sql", "false");
+        configuration.setProperty(AvailableSettings.DIALECT, MySQLDialect.class.getName());
+        configuration.setProperty(AvailableSettings.USE_QUERY_CACHE, "false");
+        configuration.setProperty(AvailableSettings.SHOW_SQL, "false");
+        configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");
         configuration.addAnnotatedClass(World.class);
         configuration.addAnnotatedClass(World.class);
         ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
         ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
         return configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
         return configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());

+ 30 - 0
spark/src/main/java/hello/web/JsonTransformer.java

@@ -0,0 +1,30 @@
+package hello.web;
+
+import spark.Request;
+import spark.Response;
+import spark.ResponseTransformerRoute;
+
+import com.google.gson.Gson;
+
+public abstract class JsonTransformer extends ResponseTransformerRoute {
+
+    private static final Gson   GSON              = new Gson();
+    private static final String CONTENT_TYPE_JSON = "application/json";
+    
+    protected JsonTransformer(final String path) {
+        super(path);
+    }
+
+    @Override
+    public String render(final Object model) {
+        return GSON.toJson(model);
+    }
+
+    @Override
+    public Object handle(final Request request, final Response response) {
+        response.type(CONTENT_TYPE_JSON);
+        return handleInternal(request, response);
+    }
+    
+    protected abstract Object handleInternal(Request request, Response response);
+}

+ 29 - 18
spark/src/main/java/hello/web/SparkApplication.java

@@ -5,7 +5,9 @@ import static spark.Spark.get;
 import hello.domain.Message;
 import hello.domain.Message;
 import hello.domain.World;
 import hello.domain.World;
 
 
+import java.util.Date;
 import java.util.Random;
 import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 
 import org.hibernate.Session;
 import org.hibernate.Session;
 
 
@@ -14,38 +16,34 @@ import spark.Request;
 import spark.Response;
 import spark.Response;
 import spark.Route;
 import spark.Route;
 
 
-import com.google.gson.Gson;
-
 public class SparkApplication implements spark.servlet.SparkApplication {
 public class SparkApplication implements spark.servlet.SparkApplication {
 
 
-    private static final Gson   GSON                 = new Gson();
-    private static final Random RANDOM               = new Random();
-    private static final int    DB_ROWS              = 10000;
-    private static final String CONTENT_TYPE_JSON    = "application/json";
+    private static final int DB_ROWS = 10000;
+    private static final String MESSAGE = "Hello, World!";
+    private static final String CONTENT_TYPE_TEXT = "text/plain";
     
     
     @Override
     @Override
     public void init() {
     public void init() {
-        get(new Route("/json") {
+        get(new JsonTransformer("/json") {
             @Override
             @Override
-            public Object handle(final Request request, final Response response) {
-                response.type(CONTENT_TYPE_JSON);
-                return GSON.toJson(new Message());
+            protected Object handleInternal(final Request request, final Response response) {
+                return new Message();
             }
             }
         });
         });
-        get(new Route("/db") {
+        get(new JsonTransformer("/db") {
             @Override
             @Override
-            public Object handle(final Request request, final Response response) {
-                response.type(CONTENT_TYPE_JSON);
-                int queries = getQueries(request);
+            protected Object handleInternal(final Request request, final Response response) {
+                final int queries = getQueries(request);
                 
                 
-                World[] worlds = new World[queries];
-                Session session = HibernateUtil.getSession();
+                final World[] worlds = new World[queries];
+                final Session session = HibernateUtil.getSession();
+                final Random random = ThreadLocalRandom.current();
                 
                 
                 for (int i = 0; i < queries; i++) {
                 for (int i = 0; i < queries; i++) {
-                    worlds[i] = (World) session.byId(World.class).load(RANDOM.nextInt(DB_ROWS) + 1);
+                    worlds[i] = (World) session.byId(World.class).load(random.nextInt(DB_ROWS) + 1);
                 }
                 }
 
 
-                return GSON.toJson(worlds);
+                return worlds;
             }
             }
             
             
             private int getQueries(final Request request) {
             private int getQueries(final Request request) {
@@ -53,12 +51,25 @@ public class SparkApplication implements spark.servlet.SparkApplication {
                 return (param == null ? 1 : Integer.parseInt(param));
                 return (param == null ? 1 : Integer.parseInt(param));
             }
             }
         });
         });
+        get(new Route("/plaintext") {
+            @Override
+            public Object handle(final Request request, final Response response) {
+                response.type(CONTENT_TYPE_TEXT);
+                return MESSAGE;
+            }
+        });
         after(new Filter("/db") {
         after(new Filter("/db") {
             @Override
             @Override
             public void handle(final Request request, final Response response) {
             public void handle(final Request request, final Response response) {
                 HibernateUtil.closeSession();
                 HibernateUtil.closeSession();
             }
             }
         });
         });
+        after(new Filter() {
+            @Override
+            public void handle(final Request request, final Response response) {
+                response.raw().addDateHeader("Date", new Date().getTime());
+            }
+        });
     }
     }
     
     
     public static void main(final String[] args) {
     public static void main(final String[] args) {

+ 1 - 1
spark/src/main/resources/hibernate-local.cfg.xml

@@ -7,7 +7,7 @@
         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&amp;elideSetAutoCommits=true&amp;useLocalSessionState=true&amp;cachePrepStmts=true&amp;cacheCallableStmts=true&amp;alwaysSendSetIsolation=false&amp;prepStmtCacheSize=4096&amp;cacheServerConfiguration=true&amp;prepStmtCacheSqlLimit=2048&amp;zeroDateTimeBehavior=convertToNull&amp;traceProtocol=false&amp;useUnbufferedInput=false&amp;useReadAheadInput=false&amp;maintainTimeStats=false&amp;useServerPrepStmts&amp;cacheRSMetadata=true</property>
         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&amp;elideSetAutoCommits=true&amp;useLocalSessionState=true&amp;cachePrepStmts=true&amp;cacheCallableStmts=true&amp;alwaysSendSetIsolation=false&amp;prepStmtCacheSize=4096&amp;cacheServerConfiguration=true&amp;prepStmtCacheSqlLimit=2048&amp;zeroDateTimeBehavior=convertToNull&amp;traceProtocol=false&amp;useUnbufferedInput=false&amp;useReadAheadInput=false&amp;maintainTimeStats=false&amp;useServerPrepStmts&amp;cacheRSMetadata=true</property>
         <property name="hibernate.connection.username">benchmarkdbuser</property>
         <property name="hibernate.connection.username">benchmarkdbuser</property>
         <property name="hibernate.connection.password">benchmarkdbpass</property>
         <property name="hibernate.connection.password">benchmarkdbpass</property>
-        <property name="hibernate.c3p0.min_size">5</property>
+        <property name="hibernate.c3p0.min_size">20</property>
         <property name="hibernate.c3p0.max_size">20</property>
         <property name="hibernate.c3p0.max_size">20</property>
         <property name="hibernate.c3p0.timeout">1800</property>
         <property name="hibernate.c3p0.timeout">1800</property>
         <property name="hibernate.c3p0.max_statements">50</property>
         <property name="hibernate.c3p0.max_statements">50</property>