Browse Source

fixed transactions in Update test to be one commit per read/write. Not one commit per batch. Improved Helper class to use Optional properly and not do Null checks

Yun Zhi Lin 10 years ago
parent
commit
08613dac9e

+ 1 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/WorldDAO.java

@@ -5,4 +5,5 @@ import com.example.helloworld.db.model.World;
 public interface WorldDAO {
     World findById(int id);
     World findAndModify(int id, int newRandomNumber);
+    World[] updatesQueries(int queries);
 }

+ 19 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/hibernate/WorldHibernateImpl.java

@@ -2,8 +2,10 @@ package com.example.helloworld.db.hibernate;
 
 import com.example.helloworld.db.WorldDAO;
 import com.example.helloworld.db.model.World;
+import com.example.helloworld.resources.Helper;
 import io.dropwizard.hibernate.AbstractDAO;
 import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
 
 public class WorldHibernateImpl extends AbstractDAO<World> implements WorldDAO {
     public WorldHibernateImpl(SessionFactory factory) {
@@ -19,4 +21,21 @@ public class WorldHibernateImpl extends AbstractDAO<World> implements WorldDAO {
         world.setRandomNumber(newRandomNumber);
         return persist(world);
     }
+
+    @Override
+    public World[] updatesQueries(int totalQueries) {
+        final World[] worlds = new World[totalQueries];
+        for (int i = 0; i < totalQueries; i++) {
+            Transaction transaction = currentSession().beginTransaction();
+            try {
+                worlds[i] = findAndModify(Helper.randomWorld(), Helper.randomWorld());
+                transaction.commit();
+            } catch (Exception e) {
+                transaction.rollback();
+                throw new RuntimeException(e);
+            }
+        }
+        return worlds;
+    }
+
 }

+ 10 - 0
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/mongo/WorldMongoImpl.java

@@ -2,6 +2,7 @@ package com.example.helloworld.db.mongo;
 
 import com.example.helloworld.db.WorldDAO;
 import com.example.helloworld.db.model.World;
+import com.example.helloworld.resources.Helper;
 import org.mongojack.DBProjection;
 import org.mongojack.DBQuery;
 import org.mongojack.DBUpdate;
@@ -32,4 +33,13 @@ public class WorldMongoImpl implements WorldDAO {
                 false);
 
     }
+
+    @Override
+    public World[] updatesQueries(int totalQueries) {
+        final World[] worlds = new World[totalQueries];
+        for (int i = 0; i < totalQueries; i++) {
+            worlds[i] = findAndModify(Helper.randomWorld(), Helper.randomWorld());
+        }
+        return worlds;
+    }
 }

+ 19 - 33
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/Helper.java

@@ -1,45 +1,31 @@
 package com.example.helloworld.resources;
 
-import java.util.concurrent.ThreadLocalRandom;
-
 import com.google.common.base.Optional;
 
+import java.util.concurrent.ThreadLocalRandom;
+
 /**
  * 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;
+public final class Helper {
+    private Helper() {
+        throw new AssertionError();
     }
-    try {
-      int parsedValue = Integer.parseInt(value);
-      return Math.min(500, Math.max(1, parsedValue));
-    } catch (NumberFormatException e) {
-      return 1;
+
+    static int getQueries(Optional<String> queries) {
+        if (!queries.isPresent()) {
+            return 1;
+        }
+        try {
+            int parsedValue = Integer.parseInt(queries.get());
+            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);
-  }
+    public static int randomWorld() {
+        return 1 + ThreadLocalRandom.current().nextInt(10000);
+    }
 }

+ 10 - 17
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -23,29 +23,22 @@ public class WorldResource {
     @GET
     @UnitOfWork
     public Object dbTest(@QueryParam("queries") Optional<String> queries) {
-        int totalQueries = Helper.getQueries(queries);
-        final World[] worlds = new World[totalQueries];
-
-        for (int i = 0; i < totalQueries; i++) {
-            worlds[i] = worldDAO.findById(Helper.randomWorld());
-        }
-        if (!queries.isPresent()) {
-        	return worlds[0];
+        if (queries.isPresent()) {
+            int totalQueries = Helper.getQueries(queries);
+            final World[] worlds = new World[totalQueries];
+            for (int i = 0; i < totalQueries; i++) {
+                worlds[i] = worldDAO.findById(Helper.randomWorld());
+            }
+            return worlds;
         } else {
-        	return worlds;
+            return worldDAO.findById(Helper.randomWorld());
         }
     }
 
     @GET
     @Path("/update")
-    @UnitOfWork
+    @UnitOfWork(transactional = false)
     public World[] updateTest(@QueryParam("queries") Optional<String> queries) {
-        int totalQueries = Helper.getQueries(queries);
-        final World[] worlds = new World[totalQueries];
-
-        for (int i = 0; i < totalQueries; i++) {
-            worlds[i] = worldDAO.findAndModify(Helper.randomWorld(), Helper.randomWorld());
-        }
-        return worlds;
+        return worldDAO.updatesQueries(Helper.getQueries(queries));
     }
 }