Commit e1555158 authored by Noric Couderc's avatar Noric Couderc

Documented main for JMH trace benchmarks

parent 9bea7070
......@@ -12,32 +12,48 @@ import se.lth.cs.bcgen.BCBenchmarkPackage;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
System.out.println("Benchmarking traces");
try {
List<Options> benchmarkSpecs = prepareOptions("test-dacapo.csv"); System.out.println("Options created");
/*
List<Options> benchmarkSpecs = prepareOptions("test-dacapo.csv");
System.out.println("Options created");
for (Options o : benchmarkSpecs) {
Runner runner = new Runner(o);
runner.run();
}*/
Runner runner = new Runner(benchmarkSpecs.stream().findFirst().get());
runner.run();
}
} catch (RunnerException | IOException | ClassNotFoundException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
}
/**
* Takes a file containing traces to benchmark and creates
* options for running them.
* It loads the data and groups the benchmarks by interface,
* then serializes each group of benchmarks to a file,
* which is then passed as a parameter to the JMH benchmark
* which will do the reloading of the list of benchmarks
* and run one with a specific index.
*
* (We do this because JMH does not allow giving data to a
* benchmark, except strings, so we have to serialize and
* pass the file name as parameter)
* @param fileName: The file from which to load the traces
* @return
* @throws IOException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
private static List<Options> prepareOptions(String fileName)
throws IOException, IllegalAccessException, InstantiationException,
ClassNotFoundException {
......@@ -46,31 +62,36 @@ public class Main {
tl.readCsv(new FileReader(new File("test-dacapo.csv")), false)
.collect(Collectors.toList());
FileOutputStream fo = new FileOutputStream("serialized-benchmarks");
ObjectOutputStream out = new ObjectOutputStream(fo);
out.writeObject(benchmarks);
Map<String, List<BCBenchmarkPackage<?>>> benchmarksByInterface =
benchmarks.stream().collect(Collectors.groupingBy(
x -> x.getInterfaceIdentifier()
));
Map<String, List<Integer>> indexes = new HashMap<>();
int index = 0;
for (BCBenchmarkPackage<?> b : benchmarks) {
String id = b.getInterfaceIdentifier();
if (indexes.containsKey(id)) {
indexes.get(id).add(index);
} else {
indexes.put(id, new ArrayList<>());
indexes.get(id).add(index);
}
index++;
// Because you cannot pass any data (except strings) to JMH
// We'll serialize this data that is expensive to compute
// and pass the file name to JMH so it can load it.
for (Map.Entry<String, List<BCBenchmarkPackage<?>>> e : benchmarksByInterface.entrySet()) {
String label = e.getKey();
FileOutputStream fo = new FileOutputStream(String.format("%s-serialized-benchmarks", label));
ObjectOutputStream out = new ObjectOutputStream(fo);
out.writeObject(e.getValue());
}
List<Options> result = new ArrayList<>();
for (Map.Entry<String, List<Integer>> e : indexes.entrySet()) {
for (Map.Entry<String, List<BCBenchmarkPackage<?>>> e : benchmarksByInterface.entrySet()) {
// This is the class we want to use for current benchmark run
String benchmarkName = e.getKey() + "TraceBenchmark";
Stream<Integer> benchmarkIds = e.getValue().stream();
String[] benchmarkIdsStrings = benchmarkIds.map(Object::toString)
.toArray(String[]::new);
// The range of benchmarks to run (in the traces)
// We just have 0,1,2,3,... to the number of benchmarks for this
// interface in the traces (the benchmark object will run the correct
// benchmark)
IntStream benchmarkIds = IntStream.range(0, e.getValue().size());
String[] benchmarkIdsStrings = new String[e.getValue().size()];
// Because in Java you can't map an IntStream to a generic stream...
benchmarkIds.forEach(
i -> benchmarkIdsStrings[i] = Integer.toString(i)
);
Options o = new OptionsBuilder()
.mode(Mode.SingleShotTime)
......@@ -78,10 +99,12 @@ public class Main {
.shouldFailOnError(true)
.measurementTime(new TimeValue(250, TimeUnit.MILLISECONDS))
.warmupTime(new TimeValue(250, TimeUnit.MILLISECONDS))
.result(benchmarkName + ".csv")
// Which file to use to load the list of benchmark to run
.param("traceFileName", String.format("%s-serialized-benchmarks", e.getKey()))
// The array of indexes for each benchmark (arrays in the list serialized above)
.param("traceId", benchmarkIdsStrings)
.param("traceFileName", "serialized-benchmarks")
.include(benchmarkName)
.result(e.getKey() + "-benchmarks-results.csv")
.build();
result.add(o);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment