Browse Source

Merge pull request #1222 from zloster/dropwizard-mongodb-java-random

Dropwizard mongodb java random
Mike Smith 10 years ago
parent
commit
a36f0a9216

+ 45 - 0
frameworks/Java/dropwizard-mongodb/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);
+  }
+}

+ 2 - 5
frameworks/Java/dropwizard-mongodb/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -1,7 +1,5 @@
 package com.example.helloworld.resources;
 package com.example.helloworld.resources;
 
 
-import java.util.Random;
-
 import javax.ws.rs.GET;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.Produces;
@@ -32,11 +30,10 @@ public class WorldResource
   @GET
   @GET
   public Object dbTest(@QueryParam("queries") Optional<String> queries)
   public Object dbTest(@QueryParam("queries") Optional<String> queries)
   {
   {
-    final Random random = new Random(System.currentTimeMillis());
     if (!queries.isPresent()) 
     if (!queries.isPresent()) 
     {
     {
       DBObject query = new BasicDBObject();
       DBObject query = new BasicDBObject();
-      query.put("_id", (random.nextInt(10000) + 1));
+      query.put("_id", Helper.randomWorld());
       DBCursor<World> dbCursor = collection.find(query);
       DBCursor<World> dbCursor = collection.find(query);
       return (dbCursor.hasNext()) ? dbCursor.next() : null;
       return (dbCursor.hasNext()) ? dbCursor.next() : null;
     }
     }
@@ -60,7 +57,7 @@ public class WorldResource
     for (int i = 0; i < totalQueries; i++)
     for (int i = 0; i < totalQueries; i++)
     {
     {
       DBObject query = new BasicDBObject();
       DBObject query = new BasicDBObject();
-      query.put("_id", (random.nextInt(10000) + 1));
+      query.put("_id", Helper.randomWorld());
       DBCursor<World> dbCursor = collection.find(query);
       DBCursor<World> dbCursor = collection.find(query);
       worlds[i] = (dbCursor.hasNext()) ? dbCursor.next() : null;
       worlds[i] = (dbCursor.hasNext()) ? dbCursor.next() : null;
     }
     }