Skip to content

Commit

Permalink
test-utils-stress: metricsInterval introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
KetothXupack committed May 10, 2015
1 parent 79d0b11 commit d459ac6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,17 +19,20 @@ public final class PreparedStressTest {
private final Iterable<ExecutorService> executors;
private final Collection<ActionStatsAccumulator> actionStatsAccumulators;
private final List<Thread> threads;
private final Duration metricsInterval;

PreparedStressTest(final int threadsNumber,
final int cycleCount,
final Iterable<ExecutorService> executors,
final Collection<ActionStatsAccumulator> actionStatsAccumulators,
final List<Thread> threads) {
final List<Thread> 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() {
Expand All @@ -52,7 +56,7 @@ public List<Thread> getThreads() {
}

public StressScenarioResult perform() throws InterruptedException {
final MetricsAccumulator metrics = new MetricsAccumulator();
final MetricsAccumulator metrics = new MetricsAccumulator(metricsInterval);
metrics.start();

final Memory memoryStart = Memory.getCurrent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<MeasureProvider> scenario) {
public PreparedStressTest prepare(final int threadsNumber, final int cycleCount,
final Scenario<MeasureProvider> scenario) {
final Map<String, ActionStatsAccumulator> accumulators = new ConcurrentHashMap<>(16, 0.75f, threadsNumber);
final Function<String, ActionStatsAccumulator> accumulatorLoader = ActionStatsAccumulator::new;
final Function<String, ActionStatsAccumulator> accumulatorGetter =
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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<PooledMeasureProvider> scenario) {
public PreparedStressTest prepare(final int coordinatorThreadsCount,
final int cyclesPerCoordinatorCount,
final int actionThreadPoolSize,
final Scenario<PooledMeasureProvider> scenario) {
final Map<String, ExecutorService> executors = new ConcurrentHashMap<>(16, 0.75f, coordinatorThreadsCount);

final Function<String, ExecutorService> executorLoader = name -> {
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,12 @@ public class MetricsAccumulator {
private final List<StressMetrics> 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());
}
Expand All @@ -24,7 +31,8 @@ public List<StressMetrics> getMetrics() {
}

public void start() {
scheduledThreadPoolExecutor.scheduleAtFixedRate(this::storeMetric, 0, 2, TimeUnit.SECONDS);
scheduledThreadPoolExecutor.scheduleAtFixedRate(this::storeMetric, 0,
period.toMillis(), TimeUnit.MILLISECONDS);
}

public void stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ 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();
System.err.println(m2);
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());

Expand All @@ -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());
Expand Down Expand Up @@ -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<String, SimpleActionResult> results = m.getResults();
assertNotNull(m.toString());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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));
Expand Down

0 comments on commit d459ac6

Please sign in to comment.