jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r985904 - in /jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance: AbstractTest.java PerformanceTestSuite.java
Date Mon, 16 Aug 2010 12:50:13 GMT
Author: jukka
Date: Mon Aug 16 12:50:12 2010
New Revision: 985904

URL: http://svn.apache.org/viewvc?rev=985904&view=rev
Log:
JCR-2699: Improve read/write concurrency

Improve the test infrastructure to better handle background jobs

Modified:
    jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/AbstractTest.java
    jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/PerformanceTestSuite.java

Modified: jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/AbstractTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/AbstractTest.java?rev=985904&r1=985903&r2=985904&view=diff
==============================================================================
--- jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/AbstractTest.java
(original)
+++ jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/AbstractTest.java
Mon Aug 16 12:50:12 2010
@@ -16,8 +16,13 @@
  */
 package org.apache.jackrabbit.performance;
 
+import java.util.LinkedList;
+import java.util.List;
+
 import javax.jcr.Credentials;
 import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 
 /**
  * Abstract base class for individual performance benchmarks.
@@ -28,34 +33,155 @@ public abstract class AbstractTest {
 
     private Credentials credentials;
 
-    public void beforeSuite() throws Exception {
-    }
+    private List<Session> sessions;
 
-    public void beforeTest() throws Exception {
-    }
+    private List<Thread> threads;
 
-    public abstract void runTest() throws Exception;
+    private volatile boolean running;
 
-    public void afterTest() throws Exception {
-    }
+    /**
+     * Prepares this performance benchmark.
+     *
+     * @param repository the repository to use
+     * @param credentials credentials of a user with write access
+     * @throws Exception if the benchmark can not be prepared
+     */
+    public void setUp(Repository repository, Credentials credentials)
+            throws Exception {
+        this.repository = repository;
+        this.credentials = credentials;
+        this.sessions = new LinkedList<Session>();
+        this.threads = new LinkedList<Thread>();
 
-    public void afterSuite() throws Exception {
+        this.running = true;
+
+        beforeSuite();
     }
 
-    public Repository getRepository() {
-        return repository;
+    /**
+     * Executes a single iteration of this test.
+     *
+     * @return number of milliseconds spent in this iteration
+     * @throws Exception if an error occurs
+     */
+    public long execute() throws Exception {
+        beforeTest();
+        try {
+            long start = System.currentTimeMillis();
+            runTest();
+            return System.currentTimeMillis() - start;
+        } finally {
+            afterTest();
+        }
+    }
+    /**
+     * Cleans up after this performance benchmark.
+     *
+     * @throws Exception if the benchmark can not be cleaned up
+     */
+    public void tearDown() throws Exception {
+        afterSuite();
+
+        this.running = false;
+        for (Thread thread : threads) {
+            thread.join();
+        }
+
+        for (Session session : sessions) {
+            if (session.isLive()) {
+                session.logout();
+            }
+        }
+
+        this.threads = null;
+        this.sessions = null;
+        this.credentials = null;
+        this.repository = null;
+    }
+
+    /**
+     * Run before any iterations of this test get executed. Subclasses can
+     * override this method to set up static test content.
+     *
+     * @throws Exception if an error occurs
+     */
+    protected void beforeSuite() throws Exception {
+    }
+
+    protected void beforeTest() throws Exception {
+    }
+
+    protected abstract void runTest() throws Exception;
+
+    protected void afterTest() throws Exception {
+    }
+
+    /**
+     * Run after all iterations of this test have been executed. Subclasses can
+     * override this method to clean up static test content.
+     *
+     * @throws Exception if an error occurs
+     */
+    protected void afterSuite() throws Exception {
     }
 
-    void setRepository(Repository repository) {
-        this.repository = repository;
+    protected Repository getRepository() {
+        return repository;
     }
 
-    public Credentials getCredentials() {
+    protected Credentials getCredentials() {
         return credentials;
     }
 
-    void setCredentials(Credentials credentials) {
-        this.credentials = credentials;
+    /**
+     * Returns a new reader session that will be automatically closed once
+     * all the iterations of this test have been executed.
+     *
+     * @return reader session
+     */
+    protected Session loginReader() {
+        try {
+            Session session = repository.login();
+            sessions.add(session);
+            return session;
+        } catch (RepositoryException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Returns a new writer session that will be automatically closed once
+     * all the iterations of this test have been executed.
+     *
+     * @return writer session
+     */
+    protected Session loginWriter() {
+        try {
+            Session session = repository.login(credentials);
+            sessions.add(session);
+            return session;
+        } catch (RepositoryException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Adds a background thread that repeatedly executes the given job
+     * until all the iterations of this test have been executed.
+     *
+     * @param job background job
+     */
+    protected void addBackgroundJob(final Runnable job) {
+        Thread thread = new Thread() {
+            @Override
+            public void run() {
+                while (running) {
+                    job.run();
+                }
+            }
+        };
+        thread.start();
+        threads.add(thread);
     }
 
     public String toString() {

Modified: jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/PerformanceTestSuite.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/PerformanceTestSuite.java?rev=985904&r1=985903&r2=985904&view=diff
==============================================================================
--- jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/PerformanceTestSuite.java
(original)
+++ jackrabbit/trunk/test/performance/base/src/main/java/org/apache/jackrabbit/performance/PerformanceTestSuite.java
Mon Aug 16 12:50:12 2010
@@ -46,31 +46,23 @@ public class PerformanceTestSuite {
     public DescriptiveStatistics runTest(AbstractTest test) throws Exception {
         DescriptiveStatistics statistics = new DescriptiveStatistics();
 
-        test.setRepository(repository);
-        test.setCredentials(credentials);
-
-        test.beforeSuite();
-
-        // Run a few iterations to warm up the system
-        long warmupEnd = System.currentTimeMillis() + warmup * 1000;
-        while (System.currentTimeMillis() < warmupEnd) {
-            test.beforeTest();
-            test.runTest();
-            test.afterTest();
+        test.setUp(repository, credentials);
+        try {
+            // Run a few iterations to warm up the system
+            long warmupEnd = System.currentTimeMillis() + warmup * 1000;
+            while (System.currentTimeMillis() < warmupEnd) {
+                test.execute();
+            }
+
+            // Run test iterations, and capture the execution times
+            long runtimeEnd = System.currentTimeMillis() + runtime * 1000;
+            while (System.currentTimeMillis() < runtimeEnd) {
+                statistics.addValue(test.execute());
+            }
+        } finally {
+            test.tearDown();
         }
 
-        // Run test iterations, and capture the execution times
-        long runtimeEnd = System.currentTimeMillis() + runtime * 1000;
-        while (System.currentTimeMillis() < runtimeEnd) {
-            test.beforeTest();
-            long start = System.currentTimeMillis();
-            test.runTest();
-            statistics.addValue(System.currentTimeMillis() - start);
-            test.afterTest();
-        }
-
-        test.afterSuite();
-
         return statistics;
     }
 



Mime
View raw message