All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nohope.test.stress.result.StressScenarioResult Maven / Gradle / Ivy

The newest version!
package org.nohope.test.stress.result;

import org.nohope.test.stress.InvocationException;
import org.nohope.test.stress.result.metrics.StressMetrics;
import org.nohope.test.stress.util.Measurement;
import org.nohope.test.stress.util.Memory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author Ketoth Xupack
 * @since 2015-04-29 15:05
 */
public class StressScenarioResult {

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

        public ActionStats(final Map> timesPerThread,
                           final Map> errorStats,
                           final String actionName) {
            this.timesPerThread = timesPerThread;
            this.errorStats = errorStats;
            this.actionName = actionName;
        }

        public Map> getTimesPerThread() {
            return timesPerThread;
        }

        public Map> getErrorStats() {
            return errorStats;
        }

        public String getActionName() {
            return actionName;
        }
    }

    private final Collection accumulators = new ArrayList<>();
    private final Collection metrics = new ArrayList<>();

    private final long startNanos;
    private final long endNanos;
    private final int threadsNumber;
    private final int cycleCount;

    private final Memory memoryStart;
    private final Memory memoryEnd;

    public StressScenarioResult(final int threadsNumber,
                                final int cycleCount,
                                final long startNanos,
                                final long endNanos,
                                final Collection accumulators,
                                final Collection metrics,
                                final Memory memoryStart,
                                final Memory memoryEnd) {
        this.startNanos = startNanos;
        this.endNanos = endNanos;
        this.threadsNumber = threadsNumber;
        this.cycleCount = cycleCount;
        this.accumulators.addAll(accumulators);
        this.metrics.addAll(metrics);
        this.memoryStart = memoryStart;
        this.memoryEnd = memoryEnd;
    }

    public  T interpret(final Interpreter interpreter) {
        return interpreter.interpret(this);
    }

    public List interpret(final Interpreter... interpreters) {
        final List ret = new ArrayList<>(interpreters.length);
        for (final Interpreter interpreter : interpreters) {
            ret.add(interpreter.interpret(this));
        }
        return ret;
    }

    public void visitErrors(final ErrorProcessor processor) {
        for (final ActionStats accumulator : accumulators) {
            final String name = accumulator.getActionName();
            for (final Entry> e: accumulator.getErrorStats().entrySet()) {
                for (final InvocationException m : e.getValue()) {
                    processor.process(name, e.getKey(), m.getCause(), m.getStartNanos(), m.getEndNanos());
                }
            }
        }
    }

    public void visitResult(final ResultProcessor processor) {
        for (final ActionStats accumulator : accumulators) {
            final String name = accumulator.getActionName();
            for (final Entry> e : accumulator.getTimesPerThread().entrySet()) {
                for (final Measurement m : e.getValue()) {
                    processor.process(name, e.getKey(), m.getStartNanos(), m.getEndNanos());
                }
            }
        }
    }

    public void visitMetrics(final MetricsProcessor processor) {
        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 getMetrics() {
        return Collections.unmodifiableCollection(metrics);
    }

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

    @FunctionalInterface
    public interface ResultProcessor {
        void process(final String name, final long threadId, long startNanos, long endNanos);
    }

    @FunctionalInterface
    public interface ErrorProcessor {
        void process(final String name, final long threadId, final Throwable e, long startNanos, long endNanos);
    }

    @FunctionalInterface
    public interface MetricsProcessor {
        void process(final StressMetrics metric);
    }

    @FunctionalInterface
    public interface Interpreter {
        T interpret(final StressScenarioResult result);
    }
}