Browse Source

Merge branch 'master' of te.github.com:TechEmpower/FrameworkBenchmarks

Patrick Falls 12 years ago
parent
commit
2a4f2ec21a

+ 54 - 16
play1/app/controllers/Application.java

@@ -10,6 +10,7 @@ import java.util.concurrent.ExecutionException;
 import models.World;
 import play.db.jpa.JPAPlugin;
 import play.jobs.Job;
+import play.libs.F.Promise;
 import play.mvc.Controller;
 
 public class Application extends Controller {
@@ -23,17 +24,38 @@ public class Application extends Controller {
 		render();
 	}
 
+	public static void hello() {
+		renderText("hello world");
+	}
+
 	public static void json() {
 		Map<String, String> result = new HashMap<String, String>();
 		result.put("message", "Hello World!");
 		renderJSON(result);
 	}
 
+	/**
+	 * this version is used in the tests. it is the simplest and fastest.
+	 * 
+	 * @param queries
+	 */
+	public static void dbSync(int queries) {
+		if (queries == 0)
+			queries = 1;
+		final List<World> worlds = new ArrayList<World>();
+		for (int i = 0; i < queries; ++i) {
+			Long id = Long.valueOf(random.nextInt(TEST_DATABASE_ROWS) + 1);
+			World result = World.findById(id);
+			worlds.add(result);
+		}
+		renderJSON(worlds);
+	}
+
 	@play.db.jpa.NoTransaction
 	public static void setup() {
 		JPAPlugin plugin = play.Play.plugin(JPAPlugin.class);
 		plugin.startTx(true);
-		
+
 		// clean out the old
 		World.deleteAll();
 		System.out.println("DELETED");
@@ -42,19 +64,46 @@ public class Application extends Controller {
 			int randomNumber = random.nextInt(TEST_DATABASE_ROWS) + 1;
 			new World(i, randomNumber).save();
 			if (i % 100 == 0) {
-				
+
 				World.em().flush();
 				World.em().clear();
 				System.out.println("FLUSHED : " + i + "/" + TEST_DATABASE_ROWS);
-				
+
 			}
 		}
 		System.out.println("ADDED");
 		plugin.closeTx(false);
 	}
 
-	public static void db(int queries) throws InterruptedException,
-			ExecutionException {
+	/**
+	 * note this is method is much slower than the synchronous version
+	 */
+	public static void dbAsyncEachQuery(int queries)
+			throws InterruptedException, ExecutionException {
+		if (queries == 0)
+			queries = 1;
+		final int queryCount = queries;
+		List<Promise<World>> promises = new ArrayList<Promise<World>>();
+		for (int i = 0; i < queryCount; ++i) {
+			final Long id = Long
+					.valueOf(random.nextInt(TEST_DATABASE_ROWS) + 1);
+			Job<World> job = new Job<World>() {
+				public World doJobWithResult() throws Exception {
+					World result = World.findById(id);
+					return result;
+				};
+			};
+			promises.add(job.now());
+		}
+		List<World> result = await(Promise.waitAll(promises));
+		renderJSON(result);
+	}
+
+	/**
+	 * note this is method is a bit slower than the synchronous version
+	 */
+	public static void dbAsyncAllQueries(int queries)
+			throws InterruptedException, ExecutionException {
 		if (queries == 0)
 			queries = 1;
 		final int queryCount = queries;
@@ -74,15 +123,4 @@ public class Application extends Controller {
 		renderJSON(result);
 	}
 
-	public static void dbSync(int queries) {
-		if (queries == 0)
-			queries = 1;
-		final List<World> worlds = new ArrayList<World>();
-		for (int i = 0; i < queries; ++i) {
-			Long id = Long.valueOf(random.nextInt(TEST_DATABASE_ROWS) + 1);
-			World result = World.findById(id);
-			worlds.add(result);
-		}
-		renderJSON(worlds);
-	}
 }

+ 3 - 3
play1/benchmark_config

@@ -3,9 +3,9 @@
   "tests": [{
     "default": {
       "setup_file": "setup",
-      "json_url": "/play1/json",
-      "db_url": "/play1/db",
-      "query_url": "/play1/db?queries=",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
       "port": 8080,
       "sort": 65
     }

+ 14 - 6
play1/conf/application.conf

@@ -30,6 +30,10 @@ date.format=yyyy-MM-dd
 # ~~~~~
 # If you need to change the HTTP port, uncomment this (default is set to 9000)
 # http.port=9000
+
+#modified to match previous port on test 
+http.port=8080
+
 #
 # By default the server listen for HTTP on the wilcard address.
 # You can restrict this.
@@ -100,6 +104,8 @@ date.format=yyyy-MM-dd
 db.pool.timeout=1000
 db.pool.maxSize=30
 db.pool.minSize=10
+
+
 #
 # If you want to reuse an existing Datasource from your application server, use:
 # db=java:/comp/env/jdbc/myDatasource
@@ -110,15 +116,16 @@ db.pool.minSize=10
 # db.destroyMethod=close
 db.driver= com.mysql.jdbc.Driver
 db.url=jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true
+#db.url=jdbc:mysql://localhost:3306/hello_world
 db.user=benchmarkdbuser
 db.pass=benchmarkdbpass
-db.jndiName=DefaultDS
+#db.jndiName=DefaultDS
 
-db.default.driver= com.mysql.jdbc.Driver
-db.default.url=jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true
-db.default.user=benchmarkdbuser
-db.default.pass=benchmarkdbpass
-db.default.jndiName=DefaultDS
+#db.default.driver= com.mysql.jdbc.Driver
+#db.default.url=jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true
+#db.default.user=benchmarkdbuser
+#db.default.pass=benchmarkdbpass
+#db.default.jndiName=DefaultDS
 
 
 # JPA Configuration (Hibernate)
@@ -205,6 +212,7 @@ mail.smtp=mock
 # Size of the Jobs pool
 # play.jobs.pool=10
 
+
 # Execution pool
 # ~~~~~
 # Default to 1 thread in DEV mode or (nb processors + 1) threads in PROD mode.

+ 4 - 2
play1/conf/routes

@@ -5,8 +5,10 @@
 
 # Home page
 GET     /json                           Application.json
-GET     /db                             Application.db
-GET     /db_sync                        Application.dbSync
+GET     /db                             Application.dbSync
+GET     /db2                            Application.dbAsyncAllQueries
+GET     /db3                            Application.dbAsyncEachQuery
 GET     /setup                          Application.setup
 GET     /                               Application.index
+GET     /hello                          Application.hello
 

+ 7 - 7
play1/setup.py

@@ -4,16 +4,16 @@ import setup_util
 
 def start(args):
   setup_util.replace_text("play1/conf/application.conf", "jdbc:mysql:\/\/.*:3306", "jdbc:mysql://" + args.database_host + ":3306")
-  
-  subprocess.check_call("rm -rf $RESIN_HOME/webapps/*", shell=True)
-  subprocess.check_call("play1 war -o $RESIN_HOME/webapps/play1 --exclude benchmark_config", shell=True, cwd="play1")
-  subprocess.check_call("$RESIN_HOME/bin/resinctl start", shell=True)
-
+  subprocess.check_call("play1 start --%prod", shell=True, cwd="play1")
+#  subprocess.check_call("rm -rf $RESIN_HOME/webapps/*", shell=True)
+#  subprocess.check_call("play1 war -o $RESIN_HOME/webapps/play1 --exclude benchmark_config", shell=True, cwd="play1")
+#  subprocess.check_call("$RESIN_HOME/bin/resinctl start", shell=True)
   return 0
 def stop():
   try:
-    subprocess.check_call("$RESIN_HOME/bin/resinctl shutdown", shell=True)
-    subprocess.check_call("rm -rf $RESIN_HOME/webapps/*", shell=True)
+     subprocess.check_call("play1 stop", shell=True, cwd="play1")
+#    subprocess.check_call("$RESIN_HOME/bin/resinctl shutdown", shell=True)
+#    subprocess.check_call("rm -rf $RESIN_HOME/webapps/*", shell=True)
     return 0
   except subprocess.CalledProcessError:
     return 1 

+ 2 - 1
spring/benchmark_config

@@ -6,8 +6,9 @@
       "json_url": "/spring/json",
       "db_url": "/spring/db",
       "query_url": "/spring/db?queries=",
+      "fortune_url": "/spring/fortunes",
       "port": 8080,
       "sort": 22
     }
   }]
-}
+}

+ 45 - 0
spring/src/main/java/hello/domain/Fortune.java

@@ -0,0 +1,45 @@
+package hello.domain;
+
+import java.util.*;
+
+import javax.persistence.*;
+
+@Entity
+public class Fortune
+  implements Comparable<Fortune>
+{
+  @Id
+  @GeneratedValue(strategy = GenerationType.IDENTITY)
+  public int id;
+  public String message;
+  
+  public Fortune()
+  {
+
+  }
+  
+  public Fortune(int id, String message)
+  {
+    this.id = id;
+    this.message = message;
+  }
+  
+  public int getId()
+  {
+    return this.id;
+  }
+
+  public String getMessage()
+  {
+    return this.message;
+  }
+
+  /**
+   * For our purposes, Fortunes sort by their message text. 
+   */
+  @Override
+  public int compareTo(Fortune other)
+  {
+    return message.compareTo(other.message);
+  }
+}

+ 44 - 0
spring/src/main/java/hello/web/HelloFortuneController.java

@@ -0,0 +1,44 @@
+package hello.web;
+
+import hello.domain.*;
+
+import java.io.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+import javax.servlet.http.*;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+
+import org.springframework.http.*;
+import org.springframework.http.converter.*;
+import org.springframework.http.converter.json.*;
+import org.springframework.http.server.*;
+import org.springframework.stereotype.*;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.servlet.ModelAndView;
+
+@Controller
+public class HelloFortuneController
+{
+
+  @RequestMapping(value = "/fortunes")
+  public ModelAndView fortunes()
+  {
+    
+    final Session session = HibernateUtil.getSessionFactory().openSession();
+    try
+    {
+      List fortunes = new ArrayList(session.createCriteria(Fortune.class).list());
+      fortunes.add(new Fortune(0, "Additional fortune added at request time."));
+      Collections.sort(fortunes);
+
+      return new ModelAndView("fortunes", "fortunes", fortunes);
+    }
+    finally
+    {
+      session.close();
+    }
+  }
+}

+ 1 - 0
spring/src/main/resources/hibernate.cfg.xml

@@ -14,5 +14,6 @@
         <property name="hibernate.current_session_context_class">thread</property>
 
         <mapping class="hello.domain.World" />
+        <mapping class="hello.domain.Fortune" />
     </session-factory>
 </hibernate-configuration>

+ 22 - 0
spring/src/main/webapp/WEB-INF/jsp/fortunes.jsp

@@ -0,0 +1,22 @@
+<%@page contentType="text/html" pageEncoding="UTF-8"%>
+<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
+<!DOCTYPE html>
+<html>
+<head>
+<title>Fortunes</title>
+</head>
+<body>
+<table>
+<tr>
+<th>id</th>
+<th>message</th>
+</tr>
+<c:forEach var="o" items="${fortunes}">
+<tr>
+<td>${o.getId()}</td>
+<td><spring:escapeBody htmlEscape="true">${o.getMessage()}</spring:escapeBody></body></td>
+</tr>
+</c:forEach>
+</table>
+</html>

+ 5 - 0
spring/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

@@ -16,5 +16,10 @@
  
     <!-- Enables the Spring MVC @Controller programming model -->
     <mvc:annotation-driven />
+    
+    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+          <property name="prefix" value="/WEB-INF/jsp/" />
+          <property name="suffix" value=".jsp" />
+       </bean>
  
 </beans>