Commit 1a399edf authored by Noric Couderc's avatar Noric Couderc

Implemented loading benchmarks from traces with comparison benchmarks

Originally, when loading a trace, only one benchmark was created
but there's now a function to also generate benchmarks so that
we can run them and make comparisons
parent 1b2b364b
......@@ -19,4 +19,13 @@ public class ListSyntheticBenchmarkGenerator implements SyntheticBenchmarkGenera
return listSyntheticBenchmarks;
}
@Override
public List<BCBenchmarkPackage<?>> createApplications(String traceKey, List<String> trace) {
List<BCBenchmarkPackage<?>> listSyntheticBenchmarks = new ArrayList<>();
listSyntheticBenchmarks.add(BCBenchmarkPackage.LIST(traceKey, trace, new ArrayList<>()));
listSyntheticBenchmarks.add(BCBenchmarkPackage.LIST(traceKey, trace, new LinkedList<>()));
listSyntheticBenchmarks.add(BCBenchmarkPackage.LIST(traceKey, trace, new Vector<>()));
return listSyntheticBenchmarks;
}
}
......@@ -19,4 +19,16 @@ public class MapSyntheticBenchmarkGenerator implements SyntheticBenchmarkGenerat
return mapSyntheticBenchmarks;
}
@Override
public List<BCBenchmarkPackage<?>> createApplications(String traceKey, List<String> trace) {
List<BCBenchmarkPackage<?>> mapSyntheticBenchmarks = new ArrayList<>();
mapSyntheticBenchmarks.add(BCBenchmarkPackage.MAP(traceKey, trace, new HashMap<>()));
mapSyntheticBenchmarks.add(BCBenchmarkPackage.MAP(traceKey, trace, new TreeMap<>()));
mapSyntheticBenchmarks.add(BCBenchmarkPackage.MAP(traceKey, trace, new IdentityHashMap<>()));
mapSyntheticBenchmarks.add(BCBenchmarkPackage.MAP(traceKey, trace, new LinkedHashMap<>()));
mapSyntheticBenchmarks.add(BCBenchmarkPackage.MAP(traceKey, trace, new WeakHashMap<>()));
return mapSyntheticBenchmarks;
}
}
......@@ -20,4 +20,15 @@ public class SetSyntheticBenchmarkGenerator implements SyntheticBenchmarkGenerat
return setSyntheticBenchmarks;
}
@Override
public List<BCBenchmarkPackage<?>> createApplications(String traceKey, List<String> trace) {
List<BCBenchmarkPackage<?>> setSyntheticBenchmarks = new ArrayList<>();
setSyntheticBenchmarks.add(BCBenchmarkPackage.SET(traceKey, trace, new HashSet<>()));
setSyntheticBenchmarks.add(BCBenchmarkPackage.SET(traceKey, trace, new TreeSet<>()));
setSyntheticBenchmarks.add(BCBenchmarkPackage.SET(traceKey, trace, new LinkedHashSet<>()));
setSyntheticBenchmarks.add(BCBenchmarkPackage.SET(traceKey, trace, new ConcurrentSkipListSet<>()));
setSyntheticBenchmarks.add(BCBenchmarkPackage.SET(traceKey, trace, new CopyOnWriteArraySet<>()));
return setSyntheticBenchmarks;
}
}
......@@ -14,4 +14,6 @@ public interface SyntheticBenchmarkGenerator {
* @return
*/
List<BCBenchmarkPackage<?>> createApplications(int seed, int number, int size);
List<BCBenchmarkPackage<?>> createApplications(String traceKey, List<String> trace);
}
......@@ -4,11 +4,15 @@ import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
import org.apache.commons.csv.CSVRecord
import org.apache.commons.lang3.tuple.ImmutablePair
import se.lth.cs.SyntheticBenchmarkGeneration.ListSyntheticBenchmarkGenerator
import se.lth.cs.SyntheticBenchmarkGeneration.MapSyntheticBenchmarkGenerator
import se.lth.cs.SyntheticBenchmarkGeneration.SetSyntheticBenchmarkGenerator
import java.io.Reader
import se.lth.cs.bcgen.*
import java.io.FileInputStream
import java.io.InputStream
import java.io.ObjectInputStream
import java.lang.RuntimeException
import java.util.*
import java.util.stream.Collector
import java.util.stream.Collectors
......@@ -53,6 +57,33 @@ class TraceLoader {
return apps.stream()
}
private fun expandBenchmark(b : BCBenchmarkPackage<*>): List<BCBenchmarkPackage<*>>? {
val traceInfo = b.benchmarkIdentifier.split(":")
// TODO: There's probably a better way than re-parsing data.
val traceKey = listOf<String>(traceInfo[1], traceInfo[2]).joinToString(":")
return when (b.interfaceIdentifier) {
"List" -> return ListSyntheticBenchmarkGenerator().createApplications(traceKey, b.trace).toList()
"Map" -> return MapSyntheticBenchmarkGenerator().createApplications(traceKey, b.trace).toList()
"Set" -> return SetSyntheticBenchmarkGenerator().createApplications(traceKey, b.trace).toList()
else -> throw RuntimeException("Invalid interface ID: " + b.interfaceIdentifier)
}
}
/**
* Creates a stream of benchmarks to run, based
* on CSV data that represents traces
* (Creates benchmarks of each type of collection in the interface
* to allow comparison)
* @param r: CSV reader (see readCsv)
* @param tsv: If the data is in CSV format
* @return a stream of benchmarks were each trace has several benchmarks (one for each possible replacement)
*/
fun benchmarksFromTraces(r : Reader, tsv : Boolean) : Stream<BCBenchmarkPackage<*>> {
return readCsv(r, tsv).flatMap {
expandBenchmark(it)?.stream()
}
}
fun readCsvToList(r : Reader, tsv: Boolean): List<BCBenchmarkPackage<*>> {
return readCsv(r, tsv).collect(Collectors.toList())
}
......
......@@ -61,14 +61,30 @@ public class SyntheticBenchmarkRunnerTest {
"4,2,loc2,java.util.ArrayList,boolean add(E),2\n" +
"5,3,loc3,java.util.HashSet,boolean add(E),1\n" +
"6,3,loc3,java.util.HashSet,boolean add(E),2\n";
Stream<BCBenchmarkPackage<?>> benchmarks = tl.readCsv(
Stream<BCBenchmarkPackage<?>> benchmarks = tl.benchmarksFromTraces(
new StringReader(text), false);
SyntheticBenchmarkRunner runner = new SyntheticBenchmarkRunner();
List<TrainingSetValue> results = runner.runBenchmarks(benchmarks.collect(Collectors.toList()));
Assert.assertEquals(3, results.size());
Assert.assertEquals("java.util.LinkedList", results.get(0).getBestDataStructure());
Assert.assertEquals("java.util.ArrayList", results.get(1).getBestDataStructure());
Assert.assertEquals("java.util.HashSet", results.get(2).getBestDataStructure());
Assert.assertEquals(11, results.size());
// All benchmarks that have the same ID must have the same best data structure
for (TrainingSetValue t1 : results) {
// We also want to check there's no benchmark
// left alone
boolean noSamePlan = true;
for (TrainingSetValue t2 : results) {
boolean samePlan = t1.getSyntheticBenchmark().getPlanIdentifier()
.equals(t2.getSyntheticBenchmark().getPlanIdentifier());
if (samePlan) {
noSamePlan = false;
Assert.assertEquals(
t1.getBestDataStructure(),
t2.getBestDataStructure()
);
}
}
Assert.assertFalse(noSamePlan);
}
}
}
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