From 8b41ea4270402c8b098e7a13366b896b5dd79a93 Mon Sep 17 00:00:00 2001
From: Christian Wulf <chw@informatik.uni-kiel.de>
Date: Tue, 3 Jun 2014 22:18:20 +0200
Subject: [PATCH] added StopWatchTest

---
 .../ThroughputTimestampAnalysis.java          | 15 +++++---
 .../framework/concurrent/WorkerThread.java    | 22 +++++------
 .../java/teetime/util/StatisticsUtil.java     | 10 +++--
 .../ThroughputTimestampAnalysisTest.java      |  6 +--
 src/test/java/teetime/util/StopWatchTest.java | 37 +++++++++++++++++++
 5 files changed, 67 insertions(+), 23 deletions(-)
 create mode 100644 src/test/java/teetime/util/StopWatchTest.java

diff --git a/src/main/java/teetime/examples/throughput/ThroughputTimestampAnalysis.java b/src/main/java/teetime/examples/throughput/ThroughputTimestampAnalysis.java
index 178d35f6..4c813011 100644
--- a/src/main/java/teetime/examples/throughput/ThroughputTimestampAnalysis.java
+++ b/src/main/java/teetime/examples/throughput/ThroughputTimestampAnalysis.java
@@ -38,7 +38,7 @@ import teetime.stage.basic.ObjectProducer;
 
 /**
  * @author Christian Wulf
- * 
+ *
  * @since 1.10
  */
 public class ThroughputTimestampAnalysis extends Analysis {
@@ -74,7 +74,8 @@ public class ThroughputTimestampAnalysis extends Analysis {
 		@SuppressWarnings("unchecked")
 		final NoopFilter<TimestampObject>[] noopFilters = new NoopFilter[numNoopFilters];
 		// create stages
-		final ObjectProducer<TimestampObject> objectProducer = new ObjectProducer<TimestampObject>(this.numInputObjects, this.inputObjectCreator);
+		final ObjectProducer<TimestampObject> objectProducer = new ObjectProducer<TimestampObject>(
+				this.numInputObjects, this.inputObjectCreator);
 		final StartTimestampFilter startTimestampFilter = new StartTimestampFilter();
 		for (int i = 0; i < noopFilters.length; i++) {
 			noopFilters[i] = new NoopFilter<TimestampObject>();
@@ -133,10 +134,12 @@ public class ThroughputTimestampAnalysis extends Analysis {
 		final long schedulingOverheadInNs = this.workerThread.computeSchedulingOverheadInNs();
 		final int size = this.workerThread.getSchedulingOverheadsInNs().size();
 		System.out.println("scheduling overhead times: " + size);
-		System.out.println("SchedulingOverhead: " + TimeUnit.NANOSECONDS.toMillis(schedulingOverheadInNs) + " ms");
-		System.out.println("avg overhead of iteration: "
-				+ TimeUnit.NANOSECONDS.toMillis(schedulingOverheadInNs / (size / 2)) + " ms");
-		System.out.println("ExecutedUnsuccessfullyCount: " + this.workerThread.getExecutedUnsuccessfullyCount());
+		if (size > 0) {
+			System.out.println("SchedulingOverhead: " + TimeUnit.NANOSECONDS.toMillis(schedulingOverheadInNs) + " ms");
+			System.out.println("avg overhead of iteration: "
+					+ TimeUnit.NANOSECONDS.toMillis(schedulingOverheadInNs * 2 / size) + " ms");
+			System.out.println("ExecutedUnsuccessfullyCount: " + this.workerThread.getExecutedUnsuccessfullyCount());
+		}
 	}
 
 	public int getNumNoopFilters() {
diff --git a/src/main/java/teetime/framework/concurrent/WorkerThread.java b/src/main/java/teetime/framework/concurrent/WorkerThread.java
index db5abd01..61181fc1 100644
--- a/src/main/java/teetime/framework/concurrent/WorkerThread.java
+++ b/src/main/java/teetime/framework/concurrent/WorkerThread.java
@@ -25,7 +25,7 @@ import teetime.util.StopWatch;
 
 /**
  * @author Christian Wulf
- * 
+ *
  * @since 1.10
  */
 public class WorkerThread extends Thread {
@@ -64,7 +64,7 @@ public class WorkerThread extends Thread {
 
 		while (this.stageScheduler.isAnyStageActive()) {
 			iterations++;
-			this.iterationStopWatch.start();
+//			this.iterationStopWatch.start();
 
 			final IStage stage = this.stageScheduler.get();
 
@@ -77,13 +77,13 @@ public class WorkerThread extends Thread {
 			}
 			this.stageScheduler.determineNextStage(stage, executedSuccessfully);
 
-			this.iterationStopWatch.end();
-			final long schedulingOverhead = this.iterationStopWatch.getDurationInNs() - stage.getLastDuration();
-			schedulingOverheadInNs += schedulingOverhead;
-			if ((iterations % 10000) == 0) {
-				this.schedulingOverheadsInNs.add(schedulingOverheadInNs);
-				schedulingOverheadInNs = 0;
-			}
+//			this.iterationStopWatch.end();
+//			final long schedulingOverhead = this.iterationStopWatch.getDurationInNs() - stage.getLastDuration();
+//			schedulingOverheadInNs += schedulingOverhead;
+//			if ((iterations % 10000) == 0) {
+//				this.schedulingOverheadsInNs.add(schedulingOverheadInNs);
+//				schedulingOverheadInNs = 0;
+//			}
 		}
 
 		this.stopWatch.end();
@@ -165,7 +165,7 @@ public class WorkerThread extends Thread {
 
 	/**
 	 * If not set, this thread will run infinitely.
-	 * 
+	 *
 	 * @param terminationPolicyToUse
 	 */
 	public void setTerminationPolicy(final StageTerminationPolicy terminationPolicyToUse) {
@@ -190,7 +190,7 @@ public class WorkerThread extends Thread {
 
 	/**
 	 * Uses the last half of values to compute the scheduling overall overhead in ns
-	 * 
+	 *
 	 * @since 1.10
 	 */
 	public long computeSchedulingOverheadInNs() {
diff --git a/src/main/java/teetime/util/StatisticsUtil.java b/src/main/java/teetime/util/StatisticsUtil.java
index dce702c4..6c664294 100644
--- a/src/main/java/teetime/util/StatisticsUtil.java
+++ b/src/main/java/teetime/util/StatisticsUtil.java
@@ -67,9 +67,7 @@ public class StatisticsUtil {
 		final long avgDurInNs = sumInNs / (timestampObjects.size() / 2);
 		System.out.println("avg duration: " + TimeUnit.NANOSECONDS.toMicros(avgDurInNs) + " µs");
 
-		for (final Entry<Double, Long> entry : quintileValues.entrySet()) {
-			System.out.println((entry.getKey() * 100) + " % : " + TimeUnit.NANOSECONDS.toMicros(entry.getValue()) + " µs");
-		}
+		printQuintiles(quintileValues);
 
 		final long confidenceWidthInNs = StatisticsUtil.calculateConfidenceWidth(sortedDurationsInNs, avgDurInNs);
 
@@ -78,6 +76,12 @@ public class StatisticsUtil {
 				+ TimeUnit.NANOSECONDS.toMicros(avgDurInNs + confidenceWidthInNs) + " µs]");
 	}
 
+	public static void printQuintiles(final Map<Double, Long> quintileValues) {
+		for (final Entry<Double, Long> entry : quintileValues.entrySet()) {
+			System.out.println((entry.getKey() * 100) + " % : " + TimeUnit.NANOSECONDS.toMicros(entry.getValue()) + " µs");
+		}
+	}
+
 	public static long calculateConfidenceWidth(final List<Long> durations, final long avgDurInNs) {
 		final double z = 1.96; // for alpha = 0.05
 		final double variance = MathUtil.getVariance(durations, avgDurInNs);
diff --git a/src/test/java/teetime/examples/throughput/ThroughputTimestampAnalysisTest.java b/src/test/java/teetime/examples/throughput/ThroughputTimestampAnalysisTest.java
index d940bd1b..d134990f 100644
--- a/src/test/java/teetime/examples/throughput/ThroughputTimestampAnalysisTest.java
+++ b/src/test/java/teetime/examples/throughput/ThroughputTimestampAnalysisTest.java
@@ -29,12 +29,12 @@ import teetime.util.StopWatch;
 
 /**
  * @author Christian Wulf
- * 
+ *
  * @since 1.10
  */
 public class ThroughputTimestampAnalysisTest {
 
-	private static final int NUM_OBJECTS_TO_CREATE = 100000;
+	private static final int NUM_OBJECTS_TO_CREATE = 50000;
 
 	@Before
 	public void before() {
@@ -49,7 +49,7 @@ public class ThroughputTimestampAnalysisTest {
 	 * <li>SchedulingOverhead: 12629 ms
 	 * <li>ExecutedUnsuccessfullyCount: 80300001
 	 * </ul>
-	 * 
+	 *
 	 * QueuePipes:
 	 * <ul>
 	 * <li>SchedulingOverhead: 11337 ms
diff --git a/src/test/java/teetime/util/StopWatchTest.java b/src/test/java/teetime/util/StopWatchTest.java
new file mode 100644
index 00000000..8923df35
--- /dev/null
+++ b/src/test/java/teetime/util/StopWatchTest.java
@@ -0,0 +1,37 @@
+package teetime.util;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Test;
+
+public class StopWatchTest {
+
+	private static final int NUM_ITERATIONS = 1000000;
+
+	@Test
+	public void testNanotime() throws Exception {
+		StopWatch iterationStopWatch = new StopWatch();
+
+		List<Long> durationsInNs = new ArrayList<Long>(NUM_ITERATIONS);
+		for (int i = 0; i < NUM_ITERATIONS; i++) {
+			iterationStopWatch.start();
+			fib(BigInteger.valueOf(10l));
+			iterationStopWatch.end();
+			durationsInNs.add(iterationStopWatch.getDurationInNs());
+		}
+
+		Map<Double, Long> quintiles = StatisticsUtil.calculateQuintiles(durationsInNs);
+		StatisticsUtil.printQuintiles(quintiles);
+	}
+
+	public static BigInteger fib(final BigInteger n) {
+		if (n.compareTo(BigInteger.ONE) == -1 || n.compareTo(BigInteger.ONE) == 0) {
+			return n;
+		} else {
+			return fib(n.subtract(BigInteger.ONE)).add(fib(n.subtract(BigInteger.ONE).subtract(BigInteger.ONE)));
+		}
+	}
+}
-- 
GitLab