Browse Source

Upgraded to 2.3.3.
Adjusted as per e9341fe

Donovan Muller 11 years ago
parent
commit
5cf5f7afe9

+ 4 - 3
frameworks/Java/play-java-jpa/README.md

@@ -14,8 +14,8 @@ This is the Play portion of a [benchmarking test suite](../) comparing a variety
 ## Infrastructure Software Versions
 The tests were run with:
 
-* [Java OpenJDK 1.7.0_09](http://openjdk.java.net/)
-* [Play 2.1.0](http://http://www.playframework.com/)
+* [Java OpenJDK 1.7](http://openjdk.java.net/)
+* [Play 2.3.3](http://http://www.playframework.com/)
 
 ## Test URLs
 ### JSON Encoding Test
@@ -24,4 +24,5 @@ http://localhost/json
 
 ### Data-Store/Database Mapping Test
 
-http://localhost/db?queries=5
+http://localhost/db
+http://localhost/queries?queries=10

+ 52 - 25
frameworks/Java/play-java-jpa/app/controllers/Application.java

@@ -8,7 +8,6 @@ import play.Play;
 import play.core.NamedThreadFactory;
 import play.libs.F;
 import play.libs.Json;
-
 import play.mvc.Controller;
 import play.mvc.Result;
 import scala.concurrent.ExecutionContext;
@@ -18,7 +17,10 @@ import utils.Predicated;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
-import java.util.concurrent.*;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 public class Application extends Controller {
 
@@ -38,39 +40,48 @@ public class Application extends Controller {
             new NamedThreadFactory("dbEc"));
     private static final ExecutionContext dbEc = ExecutionContexts.fromExecutorService(tpe);
 
-    // A predicate for checking our ability to service database requests is determined by ensuring that the request
-    // queue doesn't fill up beyond a certain threshold. For convenience we use the max number of connections * the max
-    // # of db requests per web request to determine this threshold. It is a rough check as we don't know how many
-    // queries we're going to make or what other threads are running in parallel etc. Nevertheless, the check is
-    // adequate in order to throttle the acceptance of requests to the size of the pool.
+    public static Result json() {
+        final ObjectNode result = OBJECT_MAPPER.createObjectNode();
+        result.put("message", "Hello World!");
+        return ok(result);
+    }
+
+    // If the thread-pool used by the database grows too large then our server
+    // is probably struggling, and we should start dropping requests. Set
+    // the max size of our queue something above the number of concurrent
+    // connections that we need to handle.
     public static class IsDbAvailable implements Predicate {
         @Override
         public boolean condition() {
-            return (tpe.getQueue().size() < maxConnections * MAX_QUERIES_PER_REQUEST);
+            return tpe.getQueue().size() <= 1024;
         }
     }
 
-    public static Result json() {
-        final ObjectNode result = OBJECT_MAPPER.createObjectNode();
-        result.put("message", "Hello World!");
-        return ok(result);
+    @Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE)
+    public static F.Promise<Result> db() {
+        return getRandomWorlds(1).map(new F.Function<List<World>, Result>() {
+            @Override
+            public Result apply(List<World> worlds) {
+                return ok(Json.toJson(worlds.get(0)));
+            }
+        });
     }
 
-
     @Predicated(predicate = IsDbAvailable.class, failed = SERVICE_UNAVAILABLE)
-    public static F.Promise<Result> db(final Integer queries) {
-        final Random random = ThreadLocalRandom.current();
-        final List<F.Promise<? extends World>> promises = new ArrayList<F.Promise<? extends World>>(queries);
-        for (int i = 0; i < queries; ++i) {
-            final F.Promise<World> p = F.Promise.promise(new F.Function0<World>() {
-                @Override
-                public World apply() throws Throwable {
-                    return World.findById(Long.valueOf(random.nextInt(TEST_DATABASE_ROWS) + 1));
-                }
-            }, dbEc);
-            promises.add(p);
+    public static F.Promise<Result> queries(final String queryCountString) {
+        int queryCount;
+        try {
+            queryCount = Integer.parseInt(queryCountString, 10);
+        } catch (NumberFormatException e) {
+            queryCount = 1;
+        }
+        if (queryCount < 1) {
+            queryCount = 1;
+        } else if (queryCount > 500) {
+            queryCount = 500;
         }
-        return F.Promise.sequence(promises).map(new F.Function<List<World>, Result>() {
+
+        return getRandomWorlds(queryCount).map(new F.Function<List<World>, Result>() {
             @Override
             public Result apply(List<World> worlds) {
                 return ok(Json.toJson(worlds));
@@ -78,4 +89,20 @@ public class Application extends Controller {
         });
     }
 
+    private static F.Promise<List<World>> getRandomWorlds(final int n) {
+        return F.Promise.promise(new F.Function0<List<World>>() {
+            @Override
+            public List<World> apply() throws Throwable {
+                Random random = ThreadLocalRandom.current();
+                List<World> worlds = new ArrayList<World>(n);
+                for (int i = 0; i < n; ++i) {
+                    long randomId = random.nextInt(TEST_DATABASE_ROWS) + 1;
+                    World world = World.findById(randomId);
+                    worlds.add(world);
+                }
+                return worlds;
+            }
+        }, dbEc);
+    }
+
 }

+ 3 - 3
frameworks/Java/play-java-jpa/app/utils/PredicatedAction.java

@@ -8,16 +8,16 @@ package utils;
 import play.libs.F;
 import play.mvc.Action;
 import play.mvc.Http;
-import play.mvc.SimpleResult;
+import play.mvc.Result;
 
 public class PredicatedAction extends Action<Predicated> {
     @Override
-    public F.Promise<SimpleResult> call(final Http.Context ctx) throws Throwable {
+    public F.Promise<Result> call(final Http.Context ctx) throws Throwable {
         final Predicate p = configuration.predicate().newInstance();
         if (p.condition()) {
             return delegate.call(ctx);
         } else {
-            return F.Promise.<SimpleResult>pure(status(configuration.failed()));
+            return F.Promise.<Result>pure(status(configuration.failed()));
         }
     }
 }

+ 7 - 5
frameworks/Java/play-java-jpa/build.sbt

@@ -2,13 +2,15 @@ name := "play-java-jpa"
 
 version := "1.0-SNAPSHOT"
 
+lazy val root = (project in file(".")).enablePlugins(PlayJava)
+
+scalaVersion := "2.11.2"
+
 libraryDependencies ++= Seq(
   javaJdbc,
   javaJpa,
-  "mysql" % "mysql-connector-java" % "5.1.22",
-  "org.hibernate" % "hibernate-entitymanager" % "4.2.1.Final"
+  "mysql" % "mysql-connector-java" % "5.1.32",
+  "org.hibernate" % "hibernate-entitymanager" % "4.3.6.Final"
   )
 
-dependencyOverrides += "com.jolbox" % "bonecp" % "0.7.1.RELEASE"
-
-playJavaSettings
+dependencyOverrides += "com.jolbox" % "bonecp" % "0.8.0.RELEASE"

+ 4 - 3
frameworks/Java/play-java-jpa/conf/routes

@@ -3,8 +3,9 @@
 # ~~~~
 
 # Home page
-GET     /json                           controllers.Application.json
-GET     /db                             controllers.Application.db(queries: Int ?= 1)
+GET        /json                controllers.Application.json
+GET        /db                  controllers.Application.db()
+GET        /queries             controllers.Application.queries(queries ?= "1")
 
 # Map static resources from the /public folder to the /assets URL path
-GET     /assets/*file               controllers.Assets.at(path="/public", file)
+GET        /assets/*file        controllers.Assets.at(path="/public", file)

+ 1 - 1
frameworks/Java/play-java-jpa/project/build.properties

@@ -1 +1 @@
-sbt.version=0.13.0
+sbt.version=0.13.5

+ 1 - 1
frameworks/Java/play-java-jpa/project/plugins.sbt

@@ -5,4 +5,4 @@ logLevel := Level.Warn
 resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
 
 // Use the Play sbt plugin for Play projects
-addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.2.0")
+addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.3")

+ 27 - 5
frameworks/Java/play-java-jpa/setup.py

@@ -1,12 +1,34 @@
-import setup_util
+import os
+import signal
 import subprocess
 
+dirname = projname = 'play-java'
+is_windows = args.os.lower() == "windows"
+cmd_suffix = '.bat' if is_windows else ''
+
 def start(args, logfile, errfile):
-  setup_util.replace_text("play-java-jpa/conf/application.conf", "jdbc:mysql:\/\/.*:3306", "jdbc:mysql://" + args.database_host + ":3306")
-  subprocess.Popen(["play","start"], stdin=subprocess.PIPE, cwd="play-java-jpa", stderr=errfile, stdout=logfile)
+  kill_running_process() # Kill the running process and delete the 
+                         # RUNNING_PID file (if any). With any luck no 
+                         # new process has picked up the same PID.
+
+  subprocess.call(['sbt'+cmd_suffix,"stage"], stdin=subprocess.PIPE, cwd=dirname, stderr=errfile, stdout=logfile)
+  subprocess.Popen([os.path.join("target","universal","stage","bin",projname+cmd_suffix)], shell=True, stdin=subprocess.PIPE, cwd=dirname, stderr=errfile, stdout=logfile)
   return 0
 
 def stop(logfile, errfile):
-  p = subprocess.Popen(["play","stop"], cwd="play-java-jpa", stderr=errfile, stdout=logfile)
-  p.communicate()
+  kill_running_process()  
   return 0
+
+def kill_running_process():
+  pidfile = os.path.join(dirname,"target","universal","stage","RUNNING_PID")
+  try:
+    with open(pidfile) as f:
+      pid = int(f.read())
+      os.kill(pid, signal.SIGTERM)
+  except:
+    pass
+
+  try:
+    os.remove(pidfile)
+  except OSError:
+    pass