package com.palantir.atlasdb.performance;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.immutables.value.Value;
import org.openjdk.jmh.results.RunResult;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

public class PerformanceResults {

    private final Collection<RunResult> results;

    public PerformanceResults(Collection<RunResult> results) {
        this.results = results;
    }

    public void writeToFile(File file) throws IOException {
        try (BufferedWriter fout = new BufferedWriter(new FileWriter(file))) {
            List<ImmutablePerformanceResult> newResults = results.stream().map(r -> {
                String[] benchmarkParts = r.getParams().getBenchmark().split("\\.");
                String benchmarkSuite = benchmarkParts[benchmarkParts.length - 2];
                String benchmarkName = benchmarkParts[benchmarkParts.length - 1];
                return ImmutablePerformanceResult.builder()
                        .date(System.currentTimeMillis())
                        .suite(benchmarkSuite)
                        .benchmark(benchmarkName)
                        .backend(r.getParams().getParam((BenchmarkParam.BACKEND.getKey())))
                        .samples(r.getPrimaryResult().getStatistics().getN())
                        .std(r.getPrimaryResult().getStatistics().getStandardDeviation())
                        .mean(r.getPrimaryResult().getStatistics().getMean())
                        .units(r.getParams().getTimeUnit())
                        .build();
            }).collect(Collectors.toList());
            new ObjectMapper().writeValue(fout, newResults);
        }
    }

    @JsonDeserialize(as = ImmutablePerformanceResult.class)
    @JsonSerialize(as = ImmutablePerformanceResult.class)
    @Value.Immutable
    static abstract class PerformanceResult {
        public abstract long date();
        public abstract String suite();
        public abstract String benchmark();
        public abstract String backend();
        public abstract long samples();
        public abstract double std();
        public abstract double mean();
        public abstract TimeUnit units();
    }

}

@@ -139,24 +139,56 @@
 OException;%0A
+import java.lang.reflect.Field;%0A
 import java.
@@ -200,24 +200,24 @@
 Collection;%0A
-
 import java.
@@ -300,16 +300,54 @@
 lectors;
+%0Aimport java.util.stream.DoubleStream;
 %0A%0Aimport
@@ -416,16 +416,94 @@
 nResult;
+%0Aimport org.openjdk.jmh.util.Multiset;%0Aimport org.openjdk.jmh.util.Statistics;
 %0A%0Aimport
@@ -677,16 +677,56 @@
 rialize;
+%0Aimport com.google.common.collect.Lists;
 %0A%0Apublic
@@ -1041,24 +1041,76 @@
 r(file))) %7B%0A
+            long time = System.currentTimeMillis();%0A
             
@@ -1527,34 +1527,12 @@
 ate(
-System.currentTimeMillis()
+time
 )%0A  
@@ -1960,24 +1960,66 @@
 .getMean())%0A
+                        .data(getData(r))%0A
             
@@ -2219,24 +2219,859 @@
    %7D%0A    %7D%0A%0A
+    private List%3CDouble%3E getData(RunResult result) %7B%0A        return result.getBenchmarkResults().stream()%0A                .flatMap(b -%3E getRawResults(b.getPrimaryResult().getStatistics()).stream())%0A                .collect(Collectors.toList());%0A    %7D%0A%0A    private List%3CDouble%3E getRawResults(Statistics statistics) %7B%0A        try %7B%0A            Field f = statistics.getClass().getDeclaredField(%22values%22);%0A            f.setAccessible(true);%0A            Multiset%3CDouble%3E rawResults = (Multiset%3CDouble%3E) f.get(statistics);%0A            return rawResults.entrySet().stream()%0A                    .flatMap(e -%3E DoubleStream.of(e.getKey()).limit(e.getValue()).boxed())%0A                    .collect(Collectors.toList());%0A        %7D catch (NoSuchFieldException %7C IllegalAccessException e) %7B%0A            return Lists.newArrayList();%0A        %7D%0A    %7D%0A%0A
     @JsonDes
@@ -3480,24 +3480,24 @@
 uble std();%0A
-
         publ
@@ -3519,24 +3519,69 @@
 ble mean();%0A
+        public abstract List%3CDouble%3E data();%0A
         publ

collect all data values