Browse Source

make play1 use built-in netty server. also changed db test to non-async version (simpler and faster)

Tom Carchrae 12 years ago
parent
commit
804c89297f
5 changed files with 82 additions and 34 deletions
  1. 54 16
      play1/app/controllers/Application.java
  2. 3 3
      play1/benchmark_config
  3. 14 6
      play1/conf/application.conf
  4. 4 2
      play1/conf/routes
  5. 7 7
      play1/setup.py

+ 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