
org.nohope.test.stress.ActionStatsAccumulator Maven / Gradle / Ivy
The newest version!
package org.nohope.test.stress;
import org.nohope.test.stress.functors.Call;
import org.nohope.test.stress.functors.Get;
import org.nohope.test.stress.util.Measurement;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Function;
/**
* @author ketoth xupack
* @since 2013-12-27 16:18
*/
final class ActionStatsAccumulator {
private static final Function> NEW_LIST = x -> new ConcurrentLinkedQueue<>();
private static final Function> NEW_QUEUE = c -> new ConcurrentLinkedQueue<>();
private final Map> timesPerThread = new ConcurrentHashMap<>();
private final Map> errorStats = new ConcurrentHashMap<>();
private final String name;
ActionStatsAccumulator(final String name) {
this.name = name;
}
public Map> getTimesPerThread() {
return timesPerThread;
}
public Map> getErrorStats() {
return errorStats;
}
public String getName() {
return name;
}
T measure(final long threadId, final Get invoke) throws InvocationException {
Measurement times = null;
final long start = System.nanoTime();
try {
final T result = invoke.get();
final long end = System.nanoTime();
times = Measurement.of(start, end);
return result;
} catch (final Exception e) {
final InvocationException ex = new InvocationException(e, start, System.nanoTime());
handleException(threadId, ex);
throw ex;
} finally {
if (times != null) {
timesPerThread.computeIfAbsent(threadId, NEW_LIST).add(times);
}
}
}
void measure(final long threadId, final Call call) throws InvocationException {
Measurement times = null;
final long start = System.nanoTime();
try {
call.call();
final long end = System.nanoTime();
times = Measurement.of(start, end);
} catch (final Exception e) {
final InvocationException ex = new InvocationException(e, start, System.nanoTime());
handleException(threadId, ex);
throw ex;
} finally {
if (times != null) {
timesPerThread.computeIfAbsent(threadId, NEW_LIST).add(times);
}
}
}
private void handleException(final long threadId, final InvocationException e) {
errorStats.computeIfAbsent(threadId, NEW_QUEUE).add(e);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy