Browse Source

Merge pull request #1221 from zloster/dropwizard-java-random

TechEmpower/FrameworkBenchmarks#1152 - dropwizard now using ThreadLocalRandom
Mike Smith 10 years ago
parent
commit
156dd585e1

+ 45 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/Helper.java

@@ -0,0 +1,45 @@
+package com.example.helloworld.resources;
+
+import java.util.concurrent.ThreadLocalRandom;
+
+import com.google.common.base.Optional;
+
+/**
+ * Provides utility methods for the benchmark tests.
+ * Taken from undertow-edge project.
+ */
+final class Helper {
+  private Helper() {
+    throw new AssertionError();
+  }
+
+  /**
+   * Returns the value of the "queries" request parameter, which is an integer
+   * bound between 1 and 500 with a default value of 1.
+   *
+   * @param exchange the current HTTP exchange
+   * @return the value of the "queries" request parameter
+   */
+  static int getQueries(Optional<String> queries) {
+    String value = queries.orNull();
+    if (value == null) {
+      return 1;
+    }
+    try {
+      int parsedValue = Integer.parseInt(value);
+      return Math.min(500, Math.max(1, parsedValue));
+    } catch (NumberFormatException e) {
+      return 1;
+    }
+  }
+
+  /**
+   * Returns a random integer that is a suitable value for both the {@code id}
+   * and {@code randomNumber} properties of a world object.
+   *
+   * @return a random world number
+   */
+  static int randomWorld() {
+    return 1 + ThreadLocalRandom.current().nextInt(10000);
+  }
+}

+ 14 - 41
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -1,9 +1,5 @@
 package com.example.helloworld.resources;
 package com.example.helloworld.resources;
 
 
-import com.example.helloworld.db.WorldDAO;
-import com.example.helloworld.db.model.World;
-import com.google.common.base.Optional;
-import com.google.common.primitives.Ints;
 import io.dropwizard.hibernate.UnitOfWork;
 import io.dropwizard.hibernate.UnitOfWork;
 
 
 import javax.ws.rs.GET;
 import javax.ws.rs.GET;
@@ -11,14 +7,14 @@ import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MediaType;
-import java.util.Random;
+
+import com.example.helloworld.db.WorldDAO;
+import com.example.helloworld.db.model.World;
+import com.google.common.base.Optional;
 
 
 @Path("/db")
 @Path("/db")
 @Produces(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public class WorldResource {
 public class WorldResource {
-
-    private static final Random RANDOM = new Random();
-
     private final WorldDAO worldDAO;
     private final WorldDAO worldDAO;
 
 
     public WorldResource(WorldDAO worldDAO) {
     public WorldResource(WorldDAO worldDAO) {
@@ -28,57 +24,34 @@ public class WorldResource {
     @GET
     @GET
     @UnitOfWork
     @UnitOfWork
     public Object dbTest(@QueryParam("queries") Optional<String> queries) {
     public Object dbTest(@QueryParam("queries") Optional<String> queries) {
-        if (!queries.isPresent()) {
-            final long worldId = RANDOM.nextInt(10_000) + 1;
-            return worldDAO.findById(worldId).orNull();
-        }
-
-        Integer totalQueries = Ints.tryParse(queries.orNull());
-        if (totalQueries != null) {
-            if (totalQueries > 500) {
-                totalQueries = 500;
-            } else if (totalQueries < 1) {
-                totalQueries = 1;
-            }
-        } else {
-            totalQueries = 1;
-        }
-
-        
+    	int totalQueries = Helper.getQueries(queries);
         final World[] worlds = new World[totalQueries];
         final World[] worlds = new World[totalQueries];
 
 
         // TODO: Is parallelising this cheating?
         // TODO: Is parallelising this cheating?
         for (int i = 0; i < totalQueries; i++) {
         for (int i = 0; i < totalQueries; i++) {
-            final long worldId = RANDOM.nextInt(10_000) + 1;
+            final long worldId = Helper.randomWorld();
             worlds[i] = worldDAO.findById(worldId).orNull();
             worlds[i] = worldDAO.findById(worldId).orNull();
         }
         }
-
-        return worlds;
+        if (!queries.isPresent()) {
+        	return worlds[0];
+        } else {
+        	return worlds;
+        }
     }
     }
 
 
     @GET
     @GET
     @Path("/update")
     @Path("/update")
     @UnitOfWork
     @UnitOfWork
     public World[] updateTest(@QueryParam("queries") Optional<String> queries) {
     public World[] updateTest(@QueryParam("queries") Optional<String> queries) {
-        Integer totalQueries = Ints.tryParse(queries.orNull());
-        if (totalQueries != null) {
-            if (totalQueries > 500) {
-                totalQueries = 500;
-            } else if (totalQueries < 1) {
-                totalQueries = 1;
-            }
-        } else {
-            totalQueries = 1;
-        }
-
+        int totalQueries = Helper.getQueries(queries);
         final World[] worlds = new World[totalQueries];
         final World[] worlds = new World[totalQueries];
 
 
         // TODO: Is parallelising this cheating?
         // TODO: Is parallelising this cheating?
         for (int i = 0; i < totalQueries; i++) {
         for (int i = 0; i < totalQueries; i++) {
-            final long worldId = RANDOM.nextInt(10_000) + 1;
+            final long worldId = Helper.randomWorld();
 
 
             final World world = worldDAO.findById(worldId).orNull();
             final World world = worldDAO.findById(worldId).orNull();
-            world.setRandomNumber(RANDOM.nextInt(10_000) + 1);
+            world.setRandomNumber(Helper.randomWorld());
             worlds[i] = worldDAO.update(world);
             worlds[i] = worldDAO.update(world);
         }
         }