Skip to content

Commit

Permalink
test-utils-stress: style
Browse files Browse the repository at this point in the history
  • Loading branch information
KetothXupack committed Apr 29, 2015
1 parent 69c5098 commit 4d692c2
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author <a href="mailto:ketoth.xupack@gmail.com">Ketoth Xupack</a>
* @since 2013-12-29 18:19
*/
abstract public class AbstractMeasureData {
public abstract class AbstractMeasureData {
private final int threadId;
private final int operationNumber;

Expand All @@ -24,7 +24,7 @@ public final int getOperationNumber() {
return operationNumber;
}

abstract public <T> T get(final String name, final Get<T> getter) throws Exception;
public abstract <T> T get(final String name, final Get<T> getter) throws Exception;

abstract public void call(final String name, final Call call) throws Exception;
public abstract void call(final String name, final Call call) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Future<?> callFuture(final String name, final Call call) throws Exception
return poolLoader.apply(name).submit(() -> {
try {
calc.measure(getThreadId(), call);
} catch (final InvocationException ignored) { // already accounted
} catch (final InvocationException ignored) { // FIXME: already accounted?
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.nohope.test.stress;

import org.nohope.test.stress.result.StressScenarioResult;
import org.nohope.test.stress.result.StressScenarioResult.ActionStats;
import org.nohope.test.stress.util.Memory;
import org.nohope.test.stress.util.MetricsAccumulator;

Expand All @@ -9,7 +10,6 @@
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collector;

public final class PreparedStressTest {
Expand Down Expand Up @@ -77,12 +77,9 @@ public StressScenarioResult perform() throws InterruptedException {
final long overallEnd = System.nanoTime();
final Memory memoryEnd = Memory.getCurrent();

final Collection<StressScenarioResult.ActionStats> actionResults = actionStatsAccumulators
.parallelStream()
.map((accumulator) ->
new StressScenarioResult.ActionStats(accumulator.getTimesPerThread(),
accumulator.getErrorStats(), accumulator.getName()))
.collect(toList(actionStatsAccumulators.size()));
final Collection<ActionStats> actionResults = actionStatsAccumulators.parallelStream()
.map(acc -> new ActionStats(acc.getTimesPerThread(), acc.getErrorStats(), acc.getName()))
.collect(toList(actionStatsAccumulators.size()));

return new StressScenarioResult(
threadsNumber,
Expand All @@ -96,13 +93,11 @@ public StressScenarioResult perform() throws InterruptedException {
);
}

private static <T>
Collector<T, ?, List<T>> toList(int size) {
return Collector.of((Supplier<List<T>>) () -> new ArrayList(size),
List::add,
(left, right) -> {
left.addAll(right);
return left;
});
private static <T> Collector<T, ?, List<T>> toList(int size) {
return Collector.of(() -> new ArrayList(size), List::add,
(left, right) -> {
left.addAll(right);
return left;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ public final class StressTest {
private StressTest() {
}


/**
* This method runs {@code threadsNumber}, each performing {@code cycleCount}
* invokations of {@code scenario}
* invocations of {@code scenario}
*/
public static PreparedStressTest prepare(final int threadsNumber, final int cycleCount,
final Scenario<MeasureProvider> scenario) {
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 @@ -60,23 +59,19 @@ public static PreparedStressTest prepare(final int threadsNumber, final int cycl
Collections.emptyList(), accumulators.values(), threads);
}


/**
* <p>
* This method runs {@code coordinatorThreadsCount} threads, performing
* {@code cyclesPerCoordinatorCount} invokations of {@code scenario}
* </p>
*
* <p>
* Coordinator invokes each kind {@link MeasureProvider#get(String, Get)} or {@link MeasureProvider#call(String, Call)}
* in separate threadpools sized to {@code actionThreadPoolSize}.
* </p>
* This method runs {@code coordinatorThreadsCount} threads, performing
* {@code cyclesPerCoordinatorCount} invocations of {@code scenario}
* <p />
*
* Coordinator invokes each kind {@link MeasureProvider#get(String, Get)} or
* {@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) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@
* @since 2015-04-29 15:05
*/
public class StressScenarioResult {

@SuppressWarnings({"AssignmentToCollectionOrArrayFieldFromParameter", "ReturnOfCollectionOrArrayField"})
public static class ActionStats {
private final Map<Long, Collection<Measurement>> timesPerThread;
private final Map<Long, Collection<InvocationException>> errorStats;
private final String actionName;

public ActionStats(Map<Long, Collection<Measurement>> timesPerThread,
Map<Long, Collection<InvocationException>> errorStats, String actionName) {
Map<Long, Collection<InvocationException>> errorStats,
String actionName) {
this.timesPerThread = timesPerThread;
this.errorStats = errorStats;
this.actionName = actionName;
}


public Map<Long, Collection<Measurement>> getTimesPerThread() {
return timesPerThread;
}


public Map<Long, Collection<InvocationException>> getErrorStats() {
return errorStats;
}


public String getActionName() {
return actionName;
}
Expand Down Expand Up @@ -100,51 +100,41 @@ public void visitResult(final ResultProcessor processor) {
}

public void visitMetrics(final MetricsProcessor processor) {
for (final StressMetrics metric: metrics) {
processor.process(metric);
}
metrics.forEach(processor::process);
}

public long getStartNanos() {
return startNanos;
}


public long getEndNanos() {
return endNanos;
}


public int getThreadsNumber() {
return threadsNumber;
}


public int getCycleCount() {
return cycleCount;
}


public Memory getMemoryStart() {
return memoryStart;
}


public Memory getMemoryEnd() {
return memoryEnd;
}


public Collection<StressMetrics> getMetrics() {
return Collections.unmodifiableCollection(metrics);
}


public Collection<ActionStats> getActionStats() {
return Collections.unmodifiableCollection(accumulators);
}


@FunctionalInterface
public interface ResultProcessor {
void process(final String name, final long threadId, long startNanos, long endNanos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public boolean equals(final Object o) {
@Override
public int hashCode() {
int result = gcInfo.hashCode();
result = 31 * result + (int) (collectionCount ^ (collectionCount >>> 32));
result = 31 * result + (int) (collectionTime ^ (collectionTime >>> 32));
result = 31 * result + Long.hashCode(collectionCount);
result = 31 * result + Long.hashCode(collectionTime);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ public boolean equals(final Object o) {

@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(processCpuLoad);
result = (int) (temp ^ (temp >>> 32));
result = 31 * result + (int) (processCpuTime ^ (processCpuTime >>> 32));
int result = Long.hashCode(Double.doubleToLongBits(processCpuLoad));
result = 31 * result + Long.hashCode(processCpuTime);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,37 @@

import com.google.common.collect.Sets;
import org.nohope.test.stress.result.StressScenarioResult;
import org.nohope.test.stress.result.StressScenarioResult.ActionStats;
import org.nohope.test.stress.result.StressScenarioResult.ErrorProcessor;
import org.nohope.test.stress.result.StressScenarioResult.Interpreter;
import org.nohope.test.stress.result.StressScenarioResult.ResultProcessor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
*/
public class SimpleInterpreter implements StressScenarioResult.Interpreter<SimpleStressResult> {
public class SimpleInterpreter implements Interpreter<SimpleStressResult> {
@Override
public SimpleStressResult interpret(final StressScenarioResult result) {
final double runtime = result.getEndNanos()- result.getStartNanos();

final Map<String, SimpleActionResult> results = new HashMap<>();
for (final StressScenarioResult.ActionStats accumulator : result.getActionStats()) {

for (final ActionStats accumulator : result.getActionStats()) {
final MinMaxTotal minmax = new MinMaxTotal();
result.visitResult(minmax);

final Errors errors = new Errors();
result.visitError(errors);

final int numberOfThreads = Sets.union(minmax.threadIds, errors.threadIds).size();
final int numberOfThreads = Sets.union(minmax.getThreadIds(), errors.getThreadIds()).size();
results.put(accumulator.getActionName(), new SimpleActionResult(
accumulator.getActionName(),
accumulator.getTimesPerThread(),
errors.eStats,
errors.getErrorStats(),
numberOfThreads,
minmax.totalDeltaNanos,
minmax.minTimeNanos,
minmax.maxTimeNanos));
minmax.getTotalDeltaNanos(),
minmax.getMinTimeNanos(),
minmax.getMaxTimeNanos()));
}

return new SimpleStressResult(results,
Expand All @@ -46,36 +44,58 @@ public SimpleStressResult interpret(final StressScenarioResult result) {
result.getMemoryEnd());
}


private static class MinMaxTotal implements StressScenarioResult.ResultProcessor {
long maxTimeNanos = 0;
long minTimeNanos = Long.MAX_VALUE;
long totalDeltaNanos = 0L;
Set<Long> threadIds = new HashSet<>();
private static class MinMaxTotal implements ResultProcessor {
private long maxTimeNanos;
private long totalDeltaNanos;
private long minTimeNanos = Long.MAX_VALUE;
private final Set<Long> threadIds = new HashSet<>();

@Override
public void process(final String name, final long threadId, final long startNanos, final long endNanos) {
final long runtimeNanos = endNanos - startNanos;
totalDeltaNanos += runtimeNanos;
this.totalDeltaNanos = totalDeltaNanos + runtimeNanos;
if (maxTimeNanos < runtimeNanos) {
maxTimeNanos = runtimeNanos;
this.maxTimeNanos = runtimeNanos;
}
if (minTimeNanos > runtimeNanos) {
minTimeNanos = runtimeNanos;
this.minTimeNanos = runtimeNanos;
}
threadIds.add(threadId);
}
}

public long getMaxTimeNanos() {
return maxTimeNanos;
}

public long getTotalDeltaNanos() {
return totalDeltaNanos;
}

private static class Errors implements StressScenarioResult.ErrorProcessor {
final Map<Class<?>, Collection<Throwable>> eStats = new HashMap<>();
Set<Long> threadIds = new HashSet<>();
public long getMinTimeNanos() {
return minTimeNanos;
}

public Set<Long> getThreadIds() {
return Collections.unmodifiableSet(threadIds);
}
}

private static class Errors implements ErrorProcessor {
private final Map<Class<?>, Collection<Throwable>> eStats = new HashMap<>();
private final Set<Long> threadIds = new HashSet<>();

@Override
public void process(final String name, final long threadId, final Throwable e, final long startNanos, final long endNanos) {
eStats.computeIfAbsent(e.getClass(), x -> new ArrayList<>()).add(e);
threadIds.add(threadId);
}

public Map<Class<?>, Collection<Throwable>> getErrorStats() {
return Collections.unmodifiableMap(eStats);
}

public Set<Long> getThreadIds() {
return Collections.unmodifiableSet(threadIds);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public boolean equals(final Object o) {

@Override
public int hashCode() {
int result = (int) (startNanos ^ (startNanos >>> 32));
result = 31 * result + (int) (endNanos ^ (endNanos >>> 32));
int result = Long.hashCode(startNanos);
result = 31 * result + Long.hashCode(endNanos);
return result;
}

Expand Down

0 comments on commit 4d692c2

Please sign in to comment.