Browse Source

Match parallelizm of the test framework; parallelize database operations

denka 10 years ago
parent
commit
f33ebb7966

+ 1 - 0
frameworks/Java/grizzly-jersey/source_code

@@ -1,6 +1,7 @@
 ./grizzly-jersey/src/main/
 ./grizzly-jersey/src/main/java
 ./grizzly-jersey/src/main/java/hello
+./grizzly-jersey/src/main/java/hello/Common.java
 ./grizzly-jersey/src/main/java/hello/DbResource.java
 ./grizzly-jersey/src/main/java/hello/FortunesResource.java
 ./grizzly-jersey/src/main/java/hello/SessionFactoryProvider.java

+ 20 - 0
frameworks/Java/grizzly-jersey/src/main/java/hello/Common.java

@@ -0,0 +1,20 @@
+package hello;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author denkab
+ */
+public class Common {
+
+    private static final int cpuCount = Runtime.getRuntime().availableProcessors();
+
+    public static ExecutorService EXECUTOR = new ThreadPoolExecutor(
+        cpuCount * 2, cpuCount * 25, 200, TimeUnit.MILLISECONDS,
+        new LinkedBlockingQueue<Runnable>(cpuCount * 100),
+        new ThreadPoolExecutor.CallerRunsPolicy());
+
+}

+ 24 - 5
frameworks/Java/grizzly-jersey/src/main/java/hello/DbResource.java

@@ -3,7 +3,12 @@ package hello;
 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
 import hello.domain.World;
 
+import java.util.Map;
 import java.util.Random;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
 import java.util.concurrent.ThreadLocalRandom;
 
 import javax.ws.rs.DefaultValue;
@@ -29,16 +34,30 @@ public class DbResource {
   
   @GET
   @Produces(APPLICATION_JSON + "; charset=utf-8")
-  public Object db(@QueryParam("queries") @DefaultValue("1") final int queries) {
+  public Object db(@QueryParam("queries") @DefaultValue("1") final int queries)
+      throws ExecutionException, InterruptedException {
+
     final World[] worlds = new World[queries];
     final Random random = ThreadLocalRandom.current();
     final Session session = sessionFactory.openSession();
-    
+    session.setDefaultReadOnly(true);
+
+    Map<Integer, Future<World>> futureWorlds = new ConcurrentHashMap<>();
+    for (int i = 0; i < queries; i++) {
+      futureWorlds.put(i, Common.EXECUTOR.submit(
+        new Callable<World>() {
+          @Override
+          public World call() throws Exception {
+            return (World) session.byId(World.class).load(random.nextInt(DB_ROWS) + 1);
+          }
+        }
+      ));
+    }
+
     for (int i = 0; i < queries; i++) {
-        worlds[i] = (World) session.byId(World.class).load(random.nextInt(DB_ROWS) + 1);
+      worlds[i] = futureWorlds.get(i).get();
     }
-    
-    session.close();
+
     return queries == 1 ? worlds[0] : worlds;
   }
   

+ 3 - 3
frameworks/Java/grizzly-jersey/src/main/resources/hibernate.cfg.xml

@@ -8,9 +8,9 @@
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
     <property name="hibernate.cache.use_query_cache">false</property>
     <property name="hibernate.show_sql">false</property>
-    <property name="hibernate.c3p0.min_size">5</property>
-    <property name="hibernate.c3p0.max_size">40</property>
+    <property name="hibernate.c3p0.min_size">256</property>
+    <property name="hibernate.c3p0.max_size">256</property>
     <property name="hibernate.c3p0.timeout">1800</property>
-    <property name="hibernate.c3p0.max_statements">50</property>
+    <property name="hibernate.c3p0.max_statements">2048</property>
   </session-factory>
 </hibernate-configuration>