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 org.openjdk.jmh.results.RunResult;
import org.openjdk.jmh.util.Statistics;

public class PerformanceResults {

    private static final double CONFIDENCE_INTERVAL = 0.975;

    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))) {
            fout.write("date, suite, benchmark, backend, trials, mean, error, units\n");
            for (RunResult r : results) {
                Statistics stats = r.getPrimaryResult().getStatistics();
                String[] benchmarkParts = r.getParams().getBenchmark().split("\\.");
                String benchmarkSuite = benchmarkParts[benchmarkParts.length - 2];
                String benchmarkName = benchmarkParts[benchmarkParts.length - 1];
                fout.write(
                        String.format("%d, %s, %s, %s, %s, %s, %s, %s\n",
                                System.currentTimeMillis(),
                                benchmarkSuite,
                                benchmarkName,
                                r.getParams().getParam(BenchmarkParam.BACKEND.getKey()),
                                stats.getN(),
                                stats.getMean(),
                                stats.getMeanErrorAt(CONFIDENCE_INTERVAL),
                                r.getParams().getTimeUnit())
                );
            }
        }
    }
}

@@ -171,16 +171,52 @@
 lection;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -255,144 +255,42 @@
 lt;%0A
-import org.openjdk.jmh.util.Statistics;%0A%0Apublic class PerformanceResults %7B%0A%0A    private static final double CONFIDENCE_INTERVAL = 0.975;
+%0Apublic class PerformanceResults %7B
 %0A%0A  
@@ -639,27 +639,12 @@
 nd, 
-trials, mean, error
+runs
 , un
@@ -698,81 +698,8 @@
 ) %7B%0A
-                Statistics stats = r.getPrimaryResult().getStatistics();%0A
     
@@ -1023,34 +1023,28 @@
 %25s, %25s, %25s, 
-%25s, %25s, 
+%5B
 %25s
+%5D
 , %25s%5Cn%22,%0A   
@@ -1320,22 +1320,37 @@
     
-stats.getN(),%0A
+getResultsFromRun(r)%0A        
     
@@ -1381,208 +1381,582 @@
     
+.
 st
-ats.getMean(),%0A                                stats.getMeanErrorAt(CONFIDENCE_INTERVAL),%0A                                r.getParams().getTimeUnit())%0A                );%0A            %7D%0A        %7D%0A    %7D
+ream()%0A                                        .map(d -%3E Double.toString(d))%0A                                        .collect(Collectors.joining(%22 %22)),%0A                                r.getParams().getTimeUnit())%0A                );%0A            %7D%0A        %7D%0A    %7D%0A%0A    private Collection%3CDouble%3E getResultsFromRun(RunResult r) %7B%0A        return r.getBenchmarkResults()%0A                .stream()%0A                .flatMap(rb -%3E rb.getIterationResults().stream())%0A                .map(ir -%3E ir.getPrimaryResult().getScore())%0A                .collect(Collectors.toList());%0A    %7D%0A
 %0A%7D%0A

include raw output in output results