io.hyperfoil.api.statistics.SessionStatistics Maven / Gradle / Ivy
package io.hyperfoil.api.statistics;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.stream.Stream;
import io.hyperfoil.api.config.Phase;
import io.hyperfoil.api.session.Session;
/**
* This instance holds common statistics shared between all {@link Session sessions} (in given phase) driven by the same executor.
*/
public class SessionStatistics implements Iterable {
private Phase[] phases;
private int[] stepIds;
private Map[] maps;
private int size;
public SessionStatistics() {
phases = new Phase[4];
stepIds = new int[4];
maps = new Map[4];
}
@Override
public Iterator iterator() {
return new It();
}
public Statistics getOrCreate(Phase phase, int stepId, String name, long startTime) {
for (int i = 0; i < size; ++i) {
if (stepIds[i] == stepId && phases[i] == phase) {
Statistics s = maps[i].get(name);
if (s == null) {
s = new Statistics(startTime);
maps[i].put(name, s);
}
return s;
}
}
if (size == stepIds.length) {
phases = Arrays.copyOf(phases, size * 2);
stepIds = Arrays.copyOf(stepIds, size * 2);
maps = Arrays.copyOf(maps, size * 2);
}
phases[size] = phase;
stepIds[size] = stepId;
Statistics s = new Statistics(startTime);
HashMap map = new HashMap<>();
map.put(name, s);
maps[size] = map;
++size;
return s;
}
public int size() {
return size;
}
public Phase phase(int index) {
return phases[index];
}
public int step(int index) {
return stepIds[index];
}
public Map stats(int index) {
return maps[index];
}
public Stream