jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1215361 - in /jmeter/trunk: bin/jmeter.properties src/core/org/apache/jmeter/samplers/SampleResult.java test/src/org/apache/jmeter/samplers/TestSampleResult.java xdocs/changes.xml xdocs/usermanual/listeners.xml
Date Sat, 17 Dec 2011 00:16:06 GMT
Author: sebb
Date: Sat Dec 17 00:16:06 2011
New Revision: 1215361

URL: http://svn.apache.org/viewvc?rev=1215361&view=rev
Log:
Bug 52333 - Reduce overhead in calculating SampleResult#nanoTimeOffset
Use a background thread to calculate the offset instead of doing it each sample

Modified:
    jmeter/trunk/bin/jmeter.properties
    jmeter/trunk/src/core/org/apache/jmeter/samplers/SampleResult.java
    jmeter/trunk/test/src/org/apache/jmeter/samplers/TestSampleResult.java
    jmeter/trunk/xdocs/changes.xml
    jmeter/trunk/xdocs/usermanual/listeners.xml

Modified: jmeter/trunk/bin/jmeter.properties
URL: http://svn.apache.org/viewvc/jmeter/trunk/bin/jmeter.properties?rev=1215361&r1=1215360&r2=1215361&view=diff
==============================================================================
--- jmeter/trunk/bin/jmeter.properties (original)
+++ jmeter/trunk/bin/jmeter.properties Sat Dec 17 00:16:06 2011
@@ -439,6 +439,10 @@ sampleresult.timestamp.start=true
 # Whether to use System.nanoTime() - otherwise only use System.currentTimeMillis()
 #sampleresult.useNanoTime=true
 
+# Use a background thread to calculate the nanoTime offset
+# Set this to <= 0 to disable the background thread
+#sampleresult.nanoThreadSleep=5000
+
 #---------------------------------------------------------------------------
 # Upgrade property
 #---------------------------------------------------------------------------

Modified: jmeter/trunk/src/core/org/apache/jmeter/samplers/SampleResult.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/samplers/SampleResult.java?rev=1215361&r1=1215360&r2=1215361&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/samplers/SampleResult.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/samplers/SampleResult.java Sat Dec 17 00:16:06
2011
@@ -210,6 +210,10 @@ public class SampleResult implements Ser
     static final boolean USENANOTIME
     = JMeterUtils.getPropDefault("sampleresult.useNanoTime", true);  // $NON-NLS-1$
 
+    // How long between checks of nanotime; default 5000ms; set to <=0 to disable the
thread
+    private static final long NANOTHREAD_SLEEP = 
+            JMeterUtils.getPropDefault("sampleresult.nanoThreadSleep", 5000);  // $NON-NLS-1$;
+
     static {
         if (startTimeStamp) {
             log.info("Note: Sample TimeStamps are START times");
@@ -218,32 +222,47 @@ public class SampleResult implements Ser
         }
         log.info("sampleresult.default.encoding is set to " + DEFAULT_ENCODING);
         log.info("sampleresult.useNanoTime="+USENANOTIME);
+        log.info("sampleresult.nanoThreadSleep="+NANOTHREAD_SLEEP);
+
+        if (USENANOTIME && NANOTHREAD_SLEEP > 0) {
+            NanoOffset nanoOffset = new NanoOffset();
+            nanoOffset.setDaemon(true);
+            nanoOffset.setName("NanoOffset");
+            nanoOffset.start();
+        }
     }
 
-    // Allow read access by test code
-    transient final long nanoTimeOffset;
 
-    transient final boolean useNanoTime; // Allow test code to change the default
+    private transient final long nanoTimeOffset;
+
+    // Allow testcode access to the settings
+    transient final boolean useNanoTime;
+    
+    transient final long nanoThreadSleep;
     
     private long initOffset(){
         if (useNanoTime){
-            return System.currentTimeMillis() - sampleNsClockInMs();
+            return nanoThreadSleep > 0 ? NanoOffset.getNanoOffset() : System.currentTimeMillis()
- sampleNsClockInMs();
         } else {
             return Long.MIN_VALUE;
         }
     }
 
     public SampleResult() {
-        time = 0;
-        useNanoTime = USENANOTIME;
-        nanoTimeOffset = initOffset();
+        this(USENANOTIME, NANOTHREAD_SLEEP);
     }
 
     // Allow test code to change the default useNanoTime setting
     SampleResult(boolean nanoTime) {
-        time = 0;
-        useNanoTime = nanoTime;
-        nanoTimeOffset = initOffset();
+        this(nanoTime, NANOTHREAD_SLEEP);
+    }
+
+    // Allow test code to change the default useNanoTime and nanoThreadSleep settings
+    SampleResult(boolean nanoTime, long nanoThreadSleep) {
+        this.time = 0;
+        this.useNanoTime = nanoTime;
+        this.nanoThreadSleep = nanoThreadSleep;
+        this.nanoTimeOffset = initOffset();
     }
 
     /**
@@ -252,8 +271,7 @@ public class SampleResult implements Ser
      * @param res existing sample result
      */
     public SampleResult(SampleResult res) {
-        useNanoTime = USENANOTIME;
-        nanoTimeOffset = initOffset();
+        this();
         allThreads = res.allThreads;//OK
         assertionResults = res.assertionResults;// TODO ??
         bytes = res.bytes;
@@ -306,8 +324,7 @@ public class SampleResult implements Ser
      *            create the sample finishing now, else starting now
      */
     protected SampleResult(long elapsed, boolean atend) {
-        useNanoTime = USENANOTIME;
-        nanoTimeOffset = initOffset();
+        this();
         long now = currentTimeInMillis();
         if (atend) {
             setTimes(now - elapsed, now);
@@ -357,8 +374,7 @@ public class SampleResult implements Ser
      * @param elapsed
      */
     public SampleResult(long stamp, long elapsed) {
-        useNanoTime = USENANOTIME;
-        nanoTimeOffset = initOffset();
+        this();
         stampAndTime(stamp, elapsed);
     }
 
@@ -1246,4 +1262,36 @@ public class SampleResult implements Ser
         this.bodySize = bodySize;
     }
 
+    private static class NanoOffset extends Thread {
+
+        private static volatile long nanoOffset = 
+                // Make sure we start with a reasonable value
+                System.currentTimeMillis() - SampleResult.sampleNsClockInMs();
+        
+        static long getNanoOffset() {
+            return nanoOffset;
+        }
+
+        @Override
+        public void run() {
+            // Wait longer than a clock pulse (generally 10-15ms)
+            getOffset(30L); // Catch an early clock pulse to reduce slop.
+            while(true) {
+                getOffset(NANOTHREAD_SLEEP); // Can now afford to wait a bit longer between
checks
+            }
+            
+        }
+
+        private void getOffset(long wait) {
+            try {
+                Thread.sleep(wait);
+                long clock = System.currentTimeMillis();
+                long nano = SampleResult.sampleNsClockInMs();
+                nanoOffset = clock - nano;
+            } catch (InterruptedException ignore) {
+                // ignored
+            }
+        }
+        
+    }
 }

Modified: jmeter/trunk/test/src/org/apache/jmeter/samplers/TestSampleResult.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/samplers/TestSampleResult.java?rev=1215361&r1=1215360&r2=1215361&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/samplers/TestSampleResult.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/samplers/TestSampleResult.java Sat Dec 17 00:16:06
2011
@@ -144,18 +144,34 @@ public class TestSampleResult extends Te
             testSubResults(true, 0);
         }
 
+        public void testSubResultsTrueThread() throws Exception {
+            testSubResults(true, 500L, 0);
+        }
+
         public void testSubResultsFalse() throws Exception {
             testSubResults(false, 0);
         }
 
+        public void testSubResultsFalseThread() throws Exception {
+            testSubResults(false, 500L, 0);
+        }
+
         public void testSubResultsTruePause() throws Exception {
             testSubResults(true, 100);
         }
 
+        public void testSubResultsTruePauseThread() throws Exception {
+            testSubResults(true, 500L, 100);
+        }
+
         public void testSubResultsFalsePause() throws Exception {
             testSubResults(false, 100);
         }
 
+        public void testSubResultsFalsePauseThread() throws Exception {
+            testSubResults(false, 500L, 100);
+        }
+
         // temp test case for exploring settings
         public void xtestUntilFail() throws Exception {
             while(true) {
@@ -165,12 +181,19 @@ public class TestSampleResult extends Te
         }
 
         private void testSubResults(boolean nanoTime, long pause) throws Exception {
+            testSubResults(nanoTime, 0L, pause); // Don't use nanoThread
+        }
+
+        private void testSubResults(boolean nanoTime, long nanoThreadSleep, long pause) throws
Exception {
             // This test tries to emulate a http sample, with two
             // subsamples, representing images that are downloaded for the
             // page representing the first sample.
 
             // Sample that will get two sub results, simulates a web page load 
-            SampleResult parent = new SampleResult(nanoTime);            
+            SampleResult parent = new SampleResult(nanoTime, nanoThreadSleep);          
 
+
+            assertEquals(nanoTime, parent.useNanoTime);
+            assertEquals(nanoThreadSleep, parent.nanoThreadSleep);
 
             long beginTest = parent.currentTimeInMillis();
 

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1215361&r1=1215360&r2=1215361&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml (original)
+++ jmeter/trunk/xdocs/changes.xml Sat Dec 17 00:16:06 2011
@@ -235,6 +235,7 @@ Loads any additional properties found in
 <li>Bug 51093 - when loading a selection previously stored by "Save Selection As",
show the file name in the blue window bar</li>
 <li>Bug 50086 - Password fields not Hidden in JMS Publisher, JMS Subscriber, Mail Reader
sampler, SMTP sampler and Database Configuration</li>
 <li>Added DiskStore remote sample sender: like Hold, but saves samples to disk until
end of test.</li>
+<li>Bug 52333 - Reduce overhead in calculating SampleResult#nanoTimeOffset</li>
 </ul>
 
 <h2>Non-functional changes</h2>

Modified: jmeter/trunk/xdocs/usermanual/listeners.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/usermanual/listeners.xml?rev=1215361&r1=1215360&r2=1215361&view=diff
==============================================================================
--- jmeter/trunk/xdocs/usermanual/listeners.xml (original)
+++ jmeter/trunk/xdocs/usermanual/listeners.xml Sat Dec 17 00:16:06 2011
@@ -157,6 +157,12 @@ The full set of properties that affect r
 # Put the start time stamp in logs instead of the end
 sampleresult.timestamp.start=true
 
+# Whether to use System.nanoTime() - otherwise only use System.currentTimeMillis()
+#sampleresult.useNanoTime=true
+
+# Use a background thread to calculate the nanoTime offset
+# Set this to <= 0 to disable the background thread
+#sampleresult.nanoThreadSleep=5000
 
 # legitimate values: none, first, all
 #jmeter.save.saveservice.assertion_results=none



Mime
View raw message