package com.oracle.truffle.tools.warmup.impl;
import java.util.List;
class Results {
final List<Long> samples;
final double epsilon;
final double warmupCost;
final int bestI;
final int peakStartI;
final long bestT;
final long peakStartT;
final long warmupTime;
final String location;
Results(String location, List<Long> samples, double epsilon) {
this.location = location;
this.samples = samples;
bestT = bestTime(samples);
bestI = bestIteration(samples, bestT);
this.epsilon = epsilon != 0.0 ? epsilon : epsilon(samples, bestT, bestI);
peakStartI = peakStartI(samples, bestT, this.epsilon);
peakStartT = peakStartT(samples, peakStartI);
warmupTime = warmupTime(samples, peakStartI, bestT);
warmupCost = (double) warmupTime / bestT;
}
private static double epsilon(List<Long> samples, long peak, int bestIteration) {
long sampleSum = 0;
for (int i = bestIteration; i < samples.size(); i++) {
sampleSum += samples.get(i);
}
final double avg = (double) sampleSum / (samples.size() - bestIteration);
return (avg / peak);
}
private static int bestIteration(List<Long> samples, long peak) {
for (int i = 0; i < samples.size(); i++) {
final Long sample = samples.get(i);
if (sample == peak) {
return i;
}
}
throw new AssertionError("Should not reach here.");
}
private static long peakStartT(List<Long> samples, int peakStartI) {
long peak = 0;
for (int i = 0; i < peakStartI; i++) {
peak += samples.get(i);
}
return peak;
}
private static Long bestTime(List<Long> samples) {
return samples.stream().min(Long::compareTo).get();
}
private static int peakStartI(List<Long> samples, long peak, double epsilon) {
for (int i = 0; i < samples.size(); i++) {
if (samples.get(i) < peak * epsilon) {
return i;
}
}
throw new AssertionError("Should not reach here.");
}
private static long warmupTime(List<Long> samples, int peakStartI, long peak) {
long warmup = 0;
for (int i = 0; i < peakStartI; i++) {
warmup += samples.get(i) - peak;
}
return warmup;
}
}