incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carlosv...@apache.org
Subject svn commit: r956138 - in /incubator/bval/sandbox/jsr303-impl-bench/src: main/java/org/apache/bval/bench/ main/resources/ test/java/org/apache/bval/bench/
Date Fri, 18 Jun 2010 22:02:16 GMT
Author: carlosvara
Date: Fri Jun 18 22:02:16 2010
New Revision: 956138

URL: http://svn.apache.org/viewvc?rev=956138&view=rev
Log:
Multi-threaded test & writing test results to file

Modified:
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
    incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java
    incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/ApacheTest.java
    incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/HibernateTest.java

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java?rev=956138&r1=956137&r2=956138&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
Fri Jun 18 22:02:16 2010
@@ -192,6 +192,21 @@ public enum Config {
     public void printProperties() {
         p.list(System.out);
     }
+    
+    /**
+     * @return The number of times each test is repeated.
+     */
+    public int numTestRepetitions() {
+        return new Integer(p.getProperty("tester.repetitions"));
+    }
+
+    /**
+     * @return The number of concurrent threads to execute in the multi-threaded
+     *         tests.
+     */
+    public int numThreads() {
+        return new Integer(p.getProperty("tester.numthreads"));
+    }
 
 
     /**

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties?rev=956138&r1=956137&r2=956138&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
Fri Jun 18 22:02:16 2010
@@ -37,4 +37,6 @@ generator.level2.fillrate=0.40
 generator.level3.fillrate=0.20
 generator.beanswithgroups=0.30
 generator.validvalues=0.80
-generator.inheritanceratio=0.30
\ No newline at end of file
+generator.inheritanceratio=0.30
+tester.repetitions=10
+tester.numthreads=4
\ No newline at end of file

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java?rev=956138&r1=956137&r2=956138&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java
Fri Jun 18 22:02:16 2010
@@ -18,9 +18,9 @@
  */
 package org.apache.bval.bench;
 
-
+import java.util.concurrent.CyclicBarrier;
 import javax.validation.Validator;
-
+import javax.validation.ValidatorFactory;
 import org.apache.bval.bench.generated.Holder;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -33,120 +33,216 @@ import org.junit.Test;
  * @author Carlos Vara
  */
 public abstract class AbstractBenchTest {
-    
-    // Each tested validator must override this
-    public abstract Validator getValidatorInNewFactory();
-    
-    
-    private static final int TEST_TIMES = 10;
-    
+
+
     private static Holder holder;
-    
+
     // Variables to hold the results
-    private static long fpavTime = 0l;
-    private static long fpavErrors = 0l;
-    private static long rvTime = 0l;
-    private static long rvErrors = 0l;
-    private static long rpTime = 0l;
-    private static long rpDescs = 0l;
-    
-    
+    protected static long fpavTime = 0l;
+    protected static long fpavErrors = 0l;
+    protected static long rvTime = 0l;
+    protected static long rvErrors = 0l;
+    protected static long rpTime = 0l;
+    protected static long rpDescs = 0l;
+
+    protected static Long mtStart = null;
+    protected static long mtElapsed = 0l;
+
+
+    // Barrier to control the synchronous execution of threads
+    public static CyclicBarrier barrier = new CyclicBarrier(Config.CFG.numThreads(), new
Runnable() {
+        @Override
+        public void run() {
+            // On start
+            if (mtStart == null) {
+                mtStart = System.nanoTime();
+            }
+            // On finish
+            else {
+                mtElapsed = System.nanoTime() - mtStart;
+                mtStart = null; // Restore, although not needed atm
+            }
+        }
+    });
+
+
+    /**
+     * @return A concrete implementation of the validator factory.
+     */
+    public abstract ValidatorFactory getValidatorFactory();
+
+
     /**
      * Tests the speed of validating beans that are already parsed.
      */
     @Test
     public void checkRawValidationSpeed() {
-        
-        Validator validator = getValidatorInNewFactory();
-        
+
+        Validator validator = getValidatorFactory().getValidator();
+
         // Initial parse
-        for ( Object o : holder.beans ) {
+        for (Object o : holder.beans) {
             rvErrors += validator.validate(o).size();
         }
-        
+
         // Check validation speed
-        for ( int i=0; i<TEST_TIMES; ++i ) {
-            long initTime = System.currentTimeMillis();
-            for ( Object o : holder.beans ) {
+        for (int i = 0; i < Config.CFG.numTestRepetitions(); ++i) {
+            long initTime = System.nanoTime();
+            for (Object o : holder.beans) {
                 rvErrors += validator.validate(o).size();
             }
-            rvTime += System.currentTimeMillis() - initTime;
+            rvTime += System.nanoTime() - initTime;
         }
-        
+
     }
 
-    
+
     /**
      * Tests the speed of parsing beans without performing any validation.
      */
     @Test
     public void checkRawParsingSpeed() {
-        
-        for ( int i=0; i<TEST_TIMES; ++i ) {
-            
+
+        for (int i = 0; i < Config.CFG.numTestRepetitions(); ++i) {
+
             // Validator in new factory
-            Validator validator = getValidatorInNewFactory();
-            
+            Validator validator = getValidatorFactory().getValidator();
+
             // Validate all the classes in the holder
-            long initTime = System.currentTimeMillis();
-            
-            for ( Object o : holder.beans ) {
-                rpDescs += (validator.getConstraintsForClass(o.getClass()).isBeanConstrained())?1:0;
+            long initTime = System.nanoTime();
+
+            for (Object o : holder.beans) {
+                rpDescs += (validator.getConstraintsForClass(o.getClass()).isBeanConstrained())
? 1 : 0;
             }
-            
-            rpTime += System.currentTimeMillis() - initTime;
+
+            rpTime += System.nanoTime() - initTime;
         }
-        
+
     }
-    
-    
+
+
     /**
      * Tests the speed of parsing and doing one validation of all the beans.
      */
     @Test
     public void checkFirstParseAndValidateSpeed() {
-        
-        for ( int i=0; i<TEST_TIMES; ++i ) {
-        
+
+        for (int i = 0; i < Config.CFG.numTestRepetitions(); ++i) {
+
             // Validator in new factory
-            Validator validator = getValidatorInNewFactory();
-            
-            long initTime = System.currentTimeMillis();
-            for ( Object o : holder.beans ) {
+            Validator validator = getValidatorFactory().getValidator();
+
+            long initTime = System.nanoTime();
+            for (Object o : holder.beans) {
                 fpavErrors += validator.validate(o).size();
             }
-            
-            fpavTime += System.currentTimeMillis() - initTime;
-            
+
+            fpavTime += System.nanoTime() - initTime;
+
+        }
+
+    }
+
+    @Test
+    public void checkMultithreadedValidationSpeed() throws InterruptedException {
+
+        // Build the factory and parse all the beans
+        ValidatorFactory vf = getValidatorFactory();
+        Validator validator = vf.getValidator();
+
+        // Initial parse
+        for (Object o : holder.beans) {
+            validator.validate(o).size();
+        }
+
+        // Launch X threads
+        Thread[] workers = new Thread[Config.CFG.numThreads()];
+        for (int i = 0; i < Config.CFG.numThreads(); ++i) {
+            workers[i] = new Thread(new ValidatorTask(vf.getValidator()));
+            workers[i].start();
+        }
+
+        // Wait for all the threads to finish
+        for (int i = 0; i < Config.CFG.numThreads(); ++i) {
+            workers[i].join();
+        }
+
+    }
+
+
+    /**
+     * A Runnable that will execute N validations of the whole bean list.
+     */
+    public class ValidatorTask implements Runnable {
+
+        private final Validator val;
+
+        public ValidatorTask(Validator val) {
+            this.val = val;
+        }
+
+        @Override
+        public void run() {
+            // Wait for the other threads
+            try {
+                barrier.await();
+            } catch (Exception e) {
+                throw new RuntimeException("Error while synchronizing threads.", e);
+            }
+
+            // Validate!
+            for (int i = 0; i < Config.CFG.numTestRepetitions(); ++i) {
+                for (Object o : holder.beans) {
+                    this.val.validate(o);
+                }
+            }
+
+            // Wait for the other threads
+            try {
+                barrier.await();
+            } catch (Exception e) {
+                throw new RuntimeException("Error while synchronizing threads.", e);
+            }
+
         }
 
     }
 
-    
+
     /**
-     * Only create the bean graph once.<p>
-     * Reset the counters.
+     * Only create the bean graph once.
      */
     @BeforeClass
     public static void createBeans() {
         holder = new Holder();
+    }
+
+    /**
+     * Reset the static counters every time a subclass of this executes.
+     */
+    @BeforeClass
+    public static void resetCounters() {
         fpavTime = 0l;
         fpavErrors = 0l;
         rvTime = 0l;
         rvErrors = 0l;
         rpTime = 0l;
         rpDescs = 0l;
+        mtStart = null;
+        mtElapsed = 0l;
     }
-    
-    
+
+
     /**
      * After all tests are run, print the results.
      */
     @AfterClass
     public static void printResults() {
-        System.out.printf("  Raw validation time:      %6dms {%d}\n", rvTime, rvErrors);
-        System.out.printf("  Raw parsing time:         %6dms {%d}\n", rpTime, rpDescs);
-        System.out.printf("  First parse and validate: %6dms {%d}\n", fpavTime, fpavErrors);
+        // Print them to a file in CSV format
+        System.out.printf("  Raw validation time:      %6dms {%d}\n", rvTime / 1000000l,
rvErrors);
+        System.out.printf("  Raw parsing time:         %6dms {%d}\n", rpTime / 1000000l,
rpDescs);
+        System.out.printf("  First parse and validate: %6dms {%d}\n", fpavTime / 1000000l,
fpavErrors);
+        System.out.printf("  Multithreaded validation: %6dms \n", mtElapsed / 1000000l);
     }
-    
+
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/ApacheTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/ApacheTest.java?rev=956138&r1=956137&r2=956138&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/ApacheTest.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/ApacheTest.java
Fri Jun 18 22:02:16 2010
@@ -18,12 +18,17 @@
  */
 package org.apache.bval.bench;
 
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import javax.validation.Validation;
-import javax.validation.Validator;
-
+import javax.validation.ValidatorFactory;
 import org.apache.bval.jsr303.ApacheValidationProvider;
 import org.junit.AfterClass;
 
+
 /**
  * Tests Apache Bean Validation performance.
  * 
@@ -32,13 +37,27 @@ import org.junit.AfterClass;
 public class ApacheTest extends AbstractBenchTest {
 
     @Override
-    public Validator getValidatorInNewFactory() {
-        return Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory().getValidator();
+    public ValidatorFactory getValidatorFactory() {
+        return Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory();
     }
 
     @AfterClass
     public static void printValidatorName() {
         System.out.println("APACHE");
     }
+    
+    @AfterClass
+    public static void printResultsToFile() throws IOException {
+        FileWriter fw = new FileWriter("target/apache-results.txt", true);
+        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
+        fw.write(dateFormat.format(new Date()));
+        fw.write(" " + (rvTime / 1000000l));
+        fw.write(" " + (rpTime / 1000000l));
+        fw.write(" " + (fpavTime / 1000000l));
+        fw.write(" " + (mtElapsed / 1000000l));
+        fw.write("\n");
+        fw.flush();
+        fw.close(); 
+    }
 
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/HibernateTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/HibernateTest.java?rev=956138&r1=956137&r2=956138&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/HibernateTest.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/HibernateTest.java
Fri Jun 18 22:02:16 2010
@@ -18,12 +18,17 @@
  */
 package org.apache.bval.bench;
 
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import javax.validation.Validation;
-import javax.validation.Validator;
-
+import javax.validation.ValidatorFactory;
 import org.hibernate.validator.HibernateValidator;
 import org.junit.AfterClass;
 
+
 /**
  * Tests Hibernate Validator performance.
  * 
@@ -32,13 +37,27 @@ import org.junit.AfterClass;
 public class HibernateTest extends AbstractBenchTest {
 
     @Override
-    public Validator getValidatorInNewFactory() {
-        return Validation.byProvider(HibernateValidator.class).configure().buildValidatorFactory().getValidator();
+    public ValidatorFactory getValidatorFactory() {
+        return Validation.byProvider(HibernateValidator.class).configure().buildValidatorFactory();
     }
 
     @AfterClass
     public static void printValidatorName() {
         System.out.println("HIBERNATE");
     }
+    
+    @AfterClass
+    public static void printResultsToFile() throws IOException {
+        FileWriter fw = new FileWriter("target/hibernate-results.txt", true);
+        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
+        fw.write(dateFormat.format(new Date()));
+        fw.write(" " + (rvTime / 1000000l));
+        fw.write(" " + (rpTime / 1000000l));
+        fw.write(" " + (fpavTime / 1000000l));
+        fw.write(" " + (mtElapsed / 1000000l));
+        fw.write("\n");
+        fw.flush();
+        fw.close(); 
+    }
 
 }



Mime
View raw message