usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [1/2] git commit: Revamped without logging hack and cleaning up runner to properly use executor service
Date Mon, 02 Dec 2013 15:04:45 GMT
Updated Branches:
  refs/heads/two-dot-o a022fb174 -> c5aadde3c


Revamped without logging hack and cleaning up runner to properly use executor service


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/c5aadde3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/c5aadde3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/c5aadde3

Branch: refs/heads/two-dot-o
Commit: c5aadde3c3d068fb824c8deb5b19c8d2193a44ee
Parents: ddf0f35
Author: Alex Karasulu <akarasulu@apache.org>
Authored: Mon Dec 2 16:24:06 2013 +0200
Committer: Dave Johnson <dmjohnson@apigee.com>
Committed: Mon Dec 2 09:59:37 2013 -0500

----------------------------------------------------------------------
 .../apache/usergrid/perftest/NoopPerftest.java  |   6 +-
 .../usergrid/perftest/PerftestModule.java       |   7 +-
 .../usergrid/perftest/PerftestRunner.java       | 152 ++++++++-----------
 3 files changed, 72 insertions(+), 93 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c5aadde3/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/NoopPerftest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/NoopPerftest.java
b/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/NoopPerftest.java
index 7a71815..220ab37 100644
--- a/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/NoopPerftest.java
+++ b/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/NoopPerftest.java
@@ -19,14 +19,14 @@
  */
 package org.apache.usergrid.perftest;
 
-import org.apache.usergrid.perftest.logging.Log;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * A performance test that does nothing.
  */
 public class NoopPerftest implements Perftest {
-    @Log Logger log;
+    private static final Logger LOG = LoggerFactory.getLogger( NoopPerftest.class );
 
 
     @Override
@@ -49,6 +49,6 @@ public class NoopPerftest implements Perftest {
 
     @Override
     public void call() {
-        log.info( "This performance test tests nothing" );
+        LOG.info( "This performance test tests nothing" );
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c5aadde3/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestModule.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestModule.java
b/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestModule.java
index 9f8581b..923226e 100644
--- a/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestModule.java
+++ b/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestModule.java
@@ -6,13 +6,10 @@
  */
 package org.apache.usergrid.perftest;
 
-import org.apache.usergrid.perftest.logging.Slf4jTypeListener;
 import org.apache.usergrid.perftest.rest.*;
 import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
-import com.google.inject.matcher.Matchers;
 import com.google.inject.servlet.ServletModule;
 import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
-import org.apache.usergrid.perfteststats.CallStats;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -23,14 +20,14 @@ public class PerftestModule extends ServletModule {
 
 
     protected void configureServlets() {
-        bindListener( Matchers.any(), new Slf4jTypeListener() );
-
         // Hook Jersey into Guice Servlet
         bind( GuiceContainer.class );
 
         // Hook Jackson into Jersey as the POJO <-> JSON mapper
         bind( JacksonJsonProvider.class ).asEagerSingleton();
 
+        bind( ResultsLog.class ).to( ResultsLogImpl.class );
+
         bind( CallStats.class );
         bind( PerftestRunner.class );
         bind( TestModuleLoader.class );

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/c5aadde3/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestRunner.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestRunner.java
b/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestRunner.java
index a8bd831..daafd37 100644
--- a/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestRunner.java
+++ b/stack/corepersistence/perftest/src/main/java/org/apache/usergrid/perftest/PerftestRunner.java
@@ -1,19 +1,17 @@
 package org.apache.usergrid.perftest;
 
 
-import com.netflix.blitz4j.LoggingConfiguration;
-import org.apache.usergrid.perftest.logging.Log;
 import org.apache.usergrid.perftest.rest.CallStatsSnapshot;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
 import com.google.inject.Singleton;
 import com.netflix.config.DynamicLongProperty;
 import com.netflix.config.DynamicPropertyFactory;
-import org.apache.usergrid.perfteststats.CallStats;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 
 
@@ -21,17 +19,18 @@ import java.util.concurrent.TimeUnit;
  * Invokes a Perftest based on a CallSpec.
  */
 @Singleton
-public class PerftestRunner {
-    @Log
-    Logger log;
+public class PerftestRunner implements Runnable {
+    private static final Logger LOG = LoggerFactory.getLogger( PerftestRunner.class );
 
 
     private final TestModuleLoader loader;
     private final Injector injector;
     private final Object lock = new Object();
-    private DynamicLongProperty sleepToStop = DynamicPropertyFactory.getInstance().getLongProperty(
"sleep.to.stop", 100 );
+    private DynamicLongProperty sleepToStop =
+            DynamicPropertyFactory.getInstance().getLongProperty( "sleep.to.stop", 100 );
     private CallStats stats;
-    private List<Thread> threads = new ArrayList<Thread>();
+    private Perftest test;
+    private ExecutorService executorService;
     private boolean stopSignal = false;
     private boolean running = false;
     private boolean needsReset = false;
@@ -50,45 +49,20 @@ public class PerftestRunner {
 
     public void setup() {
         synchronized ( lock ) {
-            this.threads.clear();
-            this.stopSignal = false;
-            this.running = false;
-            this.startTime = 0;
-            this.stopTime = 0;
-
-            this.stats = injector.getInstance( CallStats.class );
-            final Perftest test = loader.getChildInjector().getInstance(Perftest.class);
-
-            final long delay = test.getDelayBetweenCalls();
-            threads = new ArrayList<Thread>( test.getThreadCount() );
-            for ( int ii = 0; ii < test.getThreadCount(); ii++ ) {
-                threads.add( new Thread( new Runnable() {
-                    @Override
-                    public void run() {
-                        while( ( ! stopSignal ) && ( stats.getCallCount() < test.getCallCount()
) ) {
-                            long startTime = System.nanoTime();
-                            test.call();
-                            long endTime = System.nanoTime();
-                            stats.callOccurred( test, startTime, endTime, TimeUnit.NANOSECONDS
);
-
-                            if ( delay > 0 )
-                            {
-                                try {
-                                    Thread.sleep( delay );
-                                } catch ( InterruptedException e ) {
-                                    log.error( "Thread was interrupted.", e );
-                                }
-                            }
-
-                            synchronized ( lock ) {
-                                lock.notifyAll();
-                            }
-                        }
-                    }
-                }) );
+            stopSignal = false;
+            running = false;
+            startTime = 0;
+            stopTime = 0;
+
+            if ( stats != null )
+            {
+                stats.reset();
             }
 
-            this.needsReset = false;
+            stats = injector.getInstance( CallStats.class );
+            test = loader.getChildInjector().getInstance( Perftest.class );
+            executorService = Executors.newFixedThreadPool( test.getThreadCount() );
+            needsReset = false;
         }
     }
 
@@ -128,8 +102,9 @@ public class PerftestRunner {
             stopSignal = false;
             startTime = System.nanoTime();
             running = true;
-            for ( Thread t : threads ) {
-                t.start();
+
+            for ( int ii = 0; ii < test.getThreadCount(); ii++) {
+                executorService.execute( this );
             }
         }
 
@@ -137,21 +112,18 @@ public class PerftestRunner {
         new Thread( new Runnable() {
             @Override
             public void run() {
-                while ( threadsRunning() )
-                {
-                    synchronized ( lock )
-                    {
-                        try {
-                            lock.wait( sleepToStop.get() );
-                            log.info( "woke up running = {}", PerftestRunner.this.running
);
-                            lock.notifyAll();
-                        } catch (InterruptedException e) {
-                            log.error( "Thread interrupted while sleeping", e );
-                        }
+                try {
+                    while ( executorService.awaitTermination( sleepToStop.get(), TimeUnit.MILLISECONDS
) ) {
+                        LOG.info( "woke up running = {}", PerftestRunner.this.running );
                     }
                 }
+                catch ( InterruptedException e ) {
+                    LOG.error( "Got interrupted while monitoring executor service.", e );
+                }
+
+                stats.stop();
 
-                log.info( "COORDINATOR THREAD: all threads have died." );
+                LOG.info( "COORDINATOR THREAD: all threads have died." );
                 PerftestRunner.this.running = false;
                 PerftestRunner.this.needsReset = true;
                 stopTime = System.nanoTime();
@@ -160,38 +132,48 @@ public class PerftestRunner {
     }
 
 
-    private boolean threadsRunning()
-    {
-        boolean anyAlive = false;
-
-        try {
-            Thread.sleep( sleepToStop.get() );
-        }
-        catch ( InterruptedException e ) {
-            log.error( "Thread was interrupted.", e );
-        }
-
-        for ( Thread t : threads )
-        {
-            anyAlive |= t.isAlive();
-        }
-
-        return anyAlive;
-    }
-
-
     public void stop() {
         synchronized ( lock ) {
             stopSignal = true;
-            boolean anyAlive = false;
 
-            do {
-                anyAlive |= threadsRunning();
-            } while( anyAlive );
+            try {
+                while ( executorService.awaitTermination( sleepToStop.get(), TimeUnit.MILLISECONDS
) ) {
+                    LOG.info( "woke up: running = {}", PerftestRunner.this.running );
+                }
+            }
+            catch ( InterruptedException e ) {
+                LOG.error( "Got interrupted while monitoring executor service.", e );
+            }
 
             running = false;
+            stats.stop();
             stopTime = System.nanoTime();
             needsReset = true;
         }
     }
+
+    @Override
+    public void run() {
+        int delay = test.getDelayBetweenCalls();
+
+        while( ( ! stopSignal ) && ( stats.getCallCount() < test.getCallCount()
) ) {
+            long startTime = System.nanoTime();
+            test.call();
+            long endTime = System.nanoTime();
+            stats.callOccurred( test, startTime, endTime, TimeUnit.NANOSECONDS );
+
+            if ( delay > 0 )
+            {
+                try {
+                    Thread.sleep( delay );
+                } catch ( InterruptedException e ) {
+                    LOG.error( "Thread was interrupted.", e );
+                }
+            }
+
+            synchronized ( lock ) {
+                lock.notifyAll();
+            }
+        }
+    }
 }


Mime
View raw message