From d459ac605ca4167e49a43e4b6ae5e4b17390d065 Mon Sep 17 00:00:00 2001 From: Ketoth Xupack Date: Sun, 10 May 2015 19:07:26 +0600 Subject: [PATCH] test-utils-stress: metricsInterval introduced --- .../test/stress/PreparedStressTest.java | 8 ++- .../org/nohope/test/stress/StressTest.java | 43 +++++++++++----- .../test/stress/util/MetricsAccumulator.java | 10 +++- .../test/stresstooltest/StressTestTest.java | 49 ++++++++++--------- 4 files changed, 72 insertions(+), 38 deletions(-) diff --git a/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/PreparedStressTest.java b/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/PreparedStressTest.java index 5c97d0b..ce20bd7 100644 --- a/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/PreparedStressTest.java +++ b/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/PreparedStressTest.java @@ -5,6 +5,7 @@ import org.nohope.test.stress.util.Memory; import org.nohope.test.stress.util.MetricsAccumulator; +import java.time.Duration; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -18,17 +19,20 @@ public final class PreparedStressTest { private final Iterable executors; private final Collection actionStatsAccumulators; private final List threads; + private final Duration metricsInterval; PreparedStressTest(final int threadsNumber, final int cycleCount, final Iterable executors, final Collection actionStatsAccumulators, - final List threads) { + final List threads, + final Duration metricsInterval) { this.threadsNumber = threadsNumber; this.cycleCount = cycleCount; this.executors = executors; this.actionStatsAccumulators = actionStatsAccumulators; this.threads = threads; + this.metricsInterval = metricsInterval; } public int getThreadsNumber() { @@ -52,7 +56,7 @@ public List getThreads() { } public StressScenarioResult perform() throws InterruptedException { - final MetricsAccumulator metrics = new MetricsAccumulator(); + final MetricsAccumulator metrics = new MetricsAccumulator(metricsInterval); metrics.start(); final Memory memoryStart = Memory.getCurrent(); diff --git a/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/StressTest.java b/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/StressTest.java index c3522dc..c3d4367 100644 --- a/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/StressTest.java +++ b/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/StressTest.java @@ -5,6 +5,7 @@ import org.nohope.test.stress.functors.Call; import org.nohope.test.stress.functors.Get; +import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -20,15 +21,22 @@ * @since 2013-12-26 21:39 */ public final class StressTest { - private StressTest() { + private final Builder builder; + + private StressTest(final Builder builder) { + this.builder = builder; + } + + public static StressTest instance() { + return new Builder().build(); } /** * This method runs {@code threadsNumber}, each performing {@code cycleCount} * invocations of {@code scenario} */ - public static PreparedStressTest prepare(final int threadsNumber, final int cycleCount, - final Scenario scenario) { + public PreparedStressTest prepare(final int threadsNumber, final int cycleCount, + final Scenario scenario) { final Map accumulators = new ConcurrentHashMap<>(16, 0.75f, threadsNumber); final Function accumulatorLoader = ActionStatsAccumulator::new; final Function accumulatorGetter = @@ -56,7 +64,8 @@ public static PreparedStressTest prepare(final int threadsNumber, final int cycl } return new PreparedStressTest(threadsNumber, cycleCount, - Collections.emptyList(), accumulators.values(), threads); + Collections.emptyList(), accumulators.values(), threads, + builder.metricsInterval); } /** @@ -68,10 +77,10 @@ public static PreparedStressTest prepare(final int threadsNumber, final int cycl * {@link MeasureProvider#call(String, Call)} in separate thread pools sized to * {@code actionThreadPoolSize}. */ - public static PreparedStressTest prepare(final int coordinatorThreadsCount, - final int cyclesPerCoordinatorCount, - final int actionThreadPoolSize, - final Scenario scenario) { + public PreparedStressTest prepare(final int coordinatorThreadsCount, + final int cyclesPerCoordinatorCount, + final int actionThreadPoolSize, + final Scenario scenario) { final Map executors = new ConcurrentHashMap<>(16, 0.75f, coordinatorThreadsCount); final Function executorLoader = name -> { @@ -113,8 +122,20 @@ public static PreparedStressTest prepare(final int coordinatorThreadsCount, } return new PreparedStressTest(actionThreadPoolSize, cycleCount, - executors.values(), - accumulators.values(), - threads); + executors.values(), accumulators.values(), + threads, builder.metricsInterval); + } + + public static class Builder { + private Duration metricsInterval = Duration.ofSeconds(2); + + public Builder metricsInterval(Duration metricsInterval) { + this.metricsInterval = metricsInterval; + return this; + } + + public StressTest build() { + return new StressTest(this); + } } } diff --git a/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/util/MetricsAccumulator.java b/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/util/MetricsAccumulator.java index 94cb014..2f4104f 100644 --- a/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/util/MetricsAccumulator.java +++ b/projects/test-utils/test-utils-stress/src/main/java/org/nohope/test/stress/util/MetricsAccumulator.java @@ -2,6 +2,7 @@ import org.nohope.test.stress.result.metrics.StressMetrics; +import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -15,6 +16,12 @@ public class MetricsAccumulator { private final List metrics = new ArrayList<>(); private final ScheduledExecutorService scheduledThreadPoolExecutor = Executors.newSingleThreadScheduledExecutor(); + private final Duration period; + + public MetricsAccumulator(final Duration period) { + this.period = period; + } + private void storeMetric() { metrics.add(StressMetrics.get()); } @@ -24,7 +31,8 @@ public List getMetrics() { } public void start() { - scheduledThreadPoolExecutor.scheduleAtFixedRate(this::storeMetric, 0, 2, TimeUnit.SECONDS); + scheduledThreadPoolExecutor.scheduleAtFixedRate(this::storeMetric, 0, + period.toMillis(), TimeUnit.MILLISECONDS); } public void stop() { diff --git a/projects/test-utils/test-utils-stress/src/test/java/org/nohope/test/stresstooltest/StressTestTest.java b/projects/test-utils/test-utils-stress/src/test/java/org/nohope/test/stresstooltest/StressTestTest.java index 16506ed..0e3f48f 100644 --- a/projects/test-utils/test-utils-stress/src/test/java/org/nohope/test/stresstooltest/StressTestTest.java +++ b/projects/test-utils/test-utils-stress/src/test/java/org/nohope/test/stresstooltest/StressTestTest.java @@ -25,11 +25,13 @@ public class StressTestTest { @Ignore("for manual tests only") public void roughTest() throws InterruptedException { final SimpleStressResult m1 = - StressTest.prepare(50, 1000, p -> p.call("test1", () -> Thread.sleep(10))) - .perform().interpret(new SimpleInterpreter()); + StressTest.instance() + .prepare(50, 1000, p -> p.call("test1", () -> Thread.sleep(10))) + .perform().interpret(new SimpleInterpreter()); final SimpleStressResult m2 = - StressTest.prepare(50, 1000, p -> p.call("test2", () -> Thread.sleep(10))) - .perform().interpret(new SimpleInterpreter()); + StressTest.instance() + .prepare(50, 1000, p -> p.call("test2", () -> Thread.sleep(10))) + .perform().interpret(new SimpleInterpreter()); System.err.println(m1); System.err.println(); @@ -37,11 +39,11 @@ public void roughTest() throws InterruptedException { System.err.println("------------------------------------------------"); final SimpleStressResult m3 = - StressTest.prepare(50, 1000, p -> { + StressTest.instance().prepare(50, 1000, p -> { p.call("test1", () -> Thread.sleep(10)); }).perform().interpret(new SimpleInterpreter()); final SimpleStressResult m4 = - StressTest.prepare(50, 1000, p -> { + StressTest.instance().prepare(50, 1000, p -> { p.call("test1", () -> Thread.sleep(10)); }).perform().interpret(new SimpleInterpreter()); @@ -53,7 +55,7 @@ public void roughTest() throws InterruptedException { @Test public void counts() throws InterruptedException { { - StressScenarioResult scenarioResult = StressTest.prepare(2, 100, p -> { + StressScenarioResult scenarioResult = StressTest.instance().prepare(2, 100, p -> { p.call("test", () -> { if (p.getOperationNumber() >= 100) { //System.err.println(p.getOperationNumber()); @@ -104,14 +106,13 @@ public void counts() throws InterruptedException { { final SimpleStressResult m = - StressTest.prepare(2, 100, p -> { - p.call("test", () -> { - if (p.getOperationNumber() >= 100) { - throw new IllegalStateException(); - } - Thread.sleep(1); - }); - }).perform().interpret(new SimpleInterpreter()); + StressTest.instance().prepare(2, 100, p -> + p.call("test", () -> { + if (p.getOperationNumber() >= 100) { + throw new IllegalStateException(); + } + Thread.sleep(1); + })).perform().interpret(new SimpleInterpreter()); final Map results = m.getResults(); assertNotNull(m.toString()); @@ -139,21 +140,21 @@ public void counts() throws InterruptedException { { final SimpleStressResult m2 = - StressTest.prepare(2, 100, p -> p.call("test", () -> Thread.sleep(10))) - .perform().interpret(new SimpleInterpreter()); + StressTest.instance().prepare(2, 100, p -> p.call("test", () -> Thread.sleep(10))) + .perform().interpret(new SimpleInterpreter()); assertNotNull(m2.toString()); assertTrue(m2.getRuntime() >= 1); assertTrue(m2.getApproxThroughput() <= 200); final SimpleStressResult m3 = - StressTest.prepare(2, 100, p -> p.call("test", () -> Thread.sleep(10))) + StressTest.instance().prepare(2, 100, p -> p.call("test", () -> Thread.sleep(10))) .perform().interpret(new SimpleInterpreter()); assertNotNull(m3.toString()); assertTrue(m3.getRuntime() >= 1); assertTrue(m3.getApproxThroughput() <= 200); final SimpleStressResult m4 = - StressTest.prepare(2, 100, p -> p.get("test", () -> { + StressTest.instance().prepare(2, 100, p -> p.get("test", () -> { Thread.sleep(10); return null; })).perform().interpret(new SimpleInterpreter()); @@ -164,7 +165,7 @@ public void counts() throws InterruptedException { { final SimpleStressResult m2 = - StressTest.prepare(2, 100, 2, p -> { + StressTest.instance().prepare(2, 100, 2, p -> { p.call("test", () -> Thread.sleep(10)); }).perform().interpret(new SimpleInterpreter()); assertNotNull(m2.toString()); @@ -173,7 +174,7 @@ public void counts() throws InterruptedException { assertNotNull(m2.toString()); final SimpleStressResult m3 = - StressTest.prepare(2, 100, 2, p -> { + StressTest.instance().prepare(2, 100, 2, p -> { p.call("test", () -> Thread.sleep(10)); }).perform().interpret(new SimpleInterpreter()); assertNotNull(m3.toString()); @@ -189,7 +190,7 @@ public void pooled() throws InterruptedException { { final AtomicLong atomic = new AtomicLong(); final SimpleStressResult result = - StressTest.prepare(500, 100, p -> { + StressTest.instance().prepare(500, 100, p -> { p.get("test1", () -> { long old; while (true) { @@ -211,7 +212,7 @@ public void pooled() throws InterruptedException { { final AtomicLong atomic = new AtomicLong(); final SimpleStressResult result = - StressTest.prepare(10, 100, 5, p -> { + StressTest.instance().prepare(10, 100, 5, p -> { p.get("test1", () -> { long old; while (true) { @@ -231,7 +232,7 @@ public void pooled() throws InterruptedException { @Test public void sortedOutput() throws InterruptedException { final SimpleStressResult result = - StressTest.prepare(10, 1, p -> { + StressTest.instance().prepare(10, 1, p -> { p.call("action4", () -> Thread.sleep(10)); p.call("action1", () -> Thread.sleep(10)); p.call("action2", () -> Thread.sleep(10));