Commit 85d93391 authored by Noric Couderc's avatar Noric Couderc

Refactoring: Deleted unnecessary methods

Had a complicated way of getting the interface type of a collection
class, now the InterfaceName enum type does the conversion.

It is less general, but it's simpler.
parent 5ce6df38
......@@ -3,5 +3,24 @@ package se.lth.cs;
public enum InterfaceName {
LIST,
MAP,
SET
SET;
public static InterfaceName fromCollectionType(String collectionType) {
switch (collectionType) {
case "java.util.ArrayList" :
case "java.util.LinkedList" :
case "java.util.Vector" :
return LIST;
case "java.util.HashMap":
case "java.util.TreeMap":
case "java.util.LinkedHashMap" :
return MAP;
case "java.util.HashSet":
case "java.util.TreeSet":
case "java.util.LinkedHashSet":
return SET;
default: throw new RuntimeException(
"Invalid collection type: " + collectionType);
}
}
}
......@@ -84,12 +84,13 @@ class TraceLoader {
fun readCsvWithMetadata(reader: Reader, tsv: Boolean) : List<Pair<TraceData, BCBenchmarkPackage<out Any>>> {
return readCsvData(reader, tsv).map {
val traceKey = it.location + ":" + it.object_id
val candidates = getClassForName(it.target_type)?.let { it1 -> getCandidates(it1) }
val targetClass = Class.forName(it.target_type)
val interfaceName = InterfaceName.fromCollectionType(it.target_type)
Pair(it, generateSyntheticBenchmark(
traceKey,
candidates!!.first,
interfaceName,
it.methods,
candidates!!.second!!
targetClass
))
}
}
......@@ -107,26 +108,23 @@ class TraceLoader {
for (kvp in grouped.entries) {
// Sometimes, you cannot recreate the class.
// ignore it and report
var initClass = getClassForName(kvp.value.first()["target_type"])
if (initClass == null) {
System.err.println(String.format("Class: '%s' is not part of 'java.util'",
kvp.key.right))
continue
} else {
val candidates = getCandidates(initClass!!)
var interfaceCandidates = candidates.first
initClass = candidates.second
var initClass = Class.forName(kvp.value.first()["target_type"])
if (initClass.name.contains("java.util")) {
val target_type = kvp.value.first()["target_type"]
val traceKey = String.format("%s:%d",
kvp.key.left,
Integer.parseInt(kvp.key.right))
val app = generateSyntheticBenchmark(
traceKey,
interfaceCandidates,
InterfaceName.fromCollectionType(target_type),
kvp.value.map { it["method"] },
initClass!!)
apps.add(app)
} else {
System.err.println(String.format("Class: '%s' is not part of 'java.util'",
kvp.key.right))
continue
}
}
return apps.stream()
......@@ -136,12 +134,11 @@ class TraceLoader {
// We generate an ID we know will be unique for each AggregatedTraceData
val traceInfo = td.location + ":" + td.methods.hashCode()
// TODO: There's probably a better way than re-parsing data.
val interfaceName = getClassForName(td.target_type)?.let { getCandidates(it) }
.let { it?.first?.first() }
val interfaceName = InterfaceName.fromCollectionType(td.target_type)
return when (interfaceName) {
"java.util.List" -> ListSyntheticBenchmarkGenerator().createApplications(traceInfo, td.methods).toList()
"java.util.Map" -> MapSyntheticBenchmarkGenerator().createApplications(traceInfo, td.methods).toList()
"java.util.Set" -> SetSyntheticBenchmarkGenerator().createApplications(traceInfo, td.methods).toList()
InterfaceName.LIST -> ListSyntheticBenchmarkGenerator().createApplications(traceInfo, td.methods).toList()
InterfaceName.MAP -> MapSyntheticBenchmarkGenerator().createApplications(traceInfo, td.methods).toList()
InterfaceName.SET -> SetSyntheticBenchmarkGenerator().createApplications(traceInfo, td.methods).toList()
else -> throw RuntimeException("Invalid interface ID: $interfaceName")
}
}
......@@ -151,8 +148,7 @@ class TraceLoader {
* 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
* @param traces: A list of traces
* @return a stream of benchmarks were each trace has several benchmarks (one for each possible replacement)
*/
fun benchmarksFromTraces(traces : List<AggregatedTraceData>) : Stream<Pair<AggregatedTraceData, BCBenchmarkPackage<*>>>? {
......@@ -188,12 +184,9 @@ class TraceLoader {
private fun generateSyntheticBenchmark(
traceKey : String,
interfaceCandidates: List<String>,
interfaceName: InterfaceName,
rows : List<String>,
initClass: Class<*>): BCBenchmarkPackage<out Any> {
val interfaceName = interfaceCandidates
.first()
.removePrefix("java.util.")
val methods = rows.stream()
.map { r -> r.replace("\"", "") }
......@@ -202,9 +195,9 @@ class TraceLoader {
.collect(Collectors.toList())
val app : BCBenchmarkPackage<*> = when (interfaceName) {
"Set" -> BCBenchmarkPackage.SET(traceKey, methods, initClass.newInstance() as Set<Any>)
"Map" -> BCBenchmarkPackage.MAP(traceKey, methods, initClass.newInstance() as Map<Any, Any>)
"List" -> BCBenchmarkPackage.LIST(traceKey, methods, initClass.newInstance() as List<Any>)
InterfaceName.SET -> BCBenchmarkPackage.SET(traceKey, methods, initClass.newInstance() as Set<Any>)
InterfaceName.MAP -> BCBenchmarkPackage.MAP(traceKey, methods, initClass.newInstance() as Map<Any, Any>)
InterfaceName.LIST -> BCBenchmarkPackage.LIST(traceKey, methods, initClass.newInstance() as List<Any>)
else -> throw IllegalArgumentException(
String.format("The class '%s' is not supported by the current generator",
initClass.name)
......@@ -214,24 +207,4 @@ class TraceLoader {
System.out.println("Generated benchmark " + app.getBenchmarkIdentifier())
return app
}
private fun getCandidates(initClass: Class<*>): Pair<List<String>, Class<*>?> {
var initClass1 = initClass
var interfaceCandidates = initClass1.interfaces.map { it.name }
.filter { "java.util.Set" == it || "java.util.Map" == it || "java.util.List" == it }
while (interfaceCandidates.isEmpty()) {
initClass1 = initClass1.superclass
interfaceCandidates = initClass1.interfaces.map { it.name }
.filter { "java.util.Set" == it || "java.util.Map" == it || "java.util.List" == it }
}
return Pair(interfaceCandidates, initClass)
}
private fun getClassForName(name : String) : Class<*>? {
return if (name.contains("java.util"))
Class.forName(name)
else
null
}
}
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