JMHProcessor.kt 5.47 KB
Newer Older
1
2
3
4
package se.lth.cs

import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
5
import org.apache.commons.csv.CSVPrinter
6
import org.apache.commons.csv.CSVRecord
7
import java.io.*
8
import java.lang.Exception
9
import se.lth.cs.bcgen.*
10
11

class JMHProcessor {
12

13
    fun process(file: File): List<JMHRecord> {
Noric Couderc's avatar
Noric Couderc committed
14
        return process(FileReader(file))
15
16
    }

17
    data class JMHRecord(val seed : Int, val size : Int, val baseStructureSize : Int, val collection : String, val datastructure : String, val best : String) {
Noric Couderc's avatar
Noric Couderc committed
18
        fun toList() : List<String> {
19
            return listOf(collection, seed.toString(), size.toString() , baseStructureSize.toString(), datastructure, best)
Noric Couderc's avatar
Noric Couderc committed
20
        }
21

22
23
        fun generateSyntheticBenchmark() : BCBenchmarkPackage<*>? {
            var syntheticBenchmark : BCBenchmarkPackage<*>? = null
24
25
            val dataStructure = getClassFromSimpleName(datastructure)
            if (collection == "List") {
26
                syntheticBenchmark = BCBenchmarkPackage.LIST(seed, size, baseStructureSize, dataStructure as MutableList<Object>?)
27
                return syntheticBenchmark
28
29
30
            }

            if (collection == "Map") {
31
                syntheticBenchmark = BCBenchmarkPackage.MAP(seed, size, baseStructureSize, dataStructure as MutableMap<Any, Any>?)
32
                return syntheticBenchmark
33
34
35
            }

            if (collection == "Set") {
36
                syntheticBenchmark = BCBenchmarkPackage.SET(seed, size, baseStructureSize, dataStructure as MutableSet<Object>?)
37
                return syntheticBenchmark
38
39
40
41
42
43
44
45
            }
            return null
        }

        private fun getClassFromSimpleName(name : String) : Any {
            val className = "java.util.$name"
            return Class.forName(className).getConstructor().newInstance()
        }
Noric Couderc's avatar
Noric Couderc committed
46
    }
47

48
49
50
51
52
53
54
    private val selectedColumns = listOf(
            "Benchmark",
            "Param: seed",
            "Param: applicationSize",
            "Param: baseStructureSize"
    )

55
    fun process(reader : Reader): List<JMHRecord> {
56
        var parser = CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader())
Noric Couderc's avatar
Noric Couderc committed
57
        // We are grouping the parameters by any parameter except the data structure name (which we want)
58

59
        // We group the records by our selected columns
60
61
62
63
        val seedsToRecords = parser.records.groupBy { record ->
            selectedColumns.map { column -> record.get(column) }
        }

64
        return seedsToRecords.values.flatMap { records ->
Noric Couderc's avatar
Noric Couderc committed
65
            val interfaceName = records[0].get("Benchmark").let { processBenchmarkName(it) }
66
            val seed = records[0].get("Param: seed")
Noric Couderc's avatar
Noric Couderc committed
67
                    .let { it.toInt()}
68
            val size = records[0].get("Param: applicationSize")
69
                    .let { Integer.parseInt(it)}
70
71
            val baseStructureSize = records[0].get("Param: baseStructureSize")
                    .let { Integer.parseInt(it)}
72
            val best = getBestDataStructure(records)
73
74
75
76
            records.map{

                JMHRecord(seed, size, baseStructureSize, interfaceName, it.get("Param: datastructureName"), best!!)
            }
77
78
        }
    }
79

80
81
82
83
84
85
86
    /**
     * Get the "worst" running time for a JMH benchmark record
     */
    private fun getWorstScore(record : CSVRecord): Double {
        return record.get("Score").toDouble() - record.get("Score Error (99.9%)").toDouble()
    }

87
88
89
90
91
92
    private fun getBestDataStructure(records : List<CSVRecord>): String? {
        // Precondition
        // All records must have
        // - same seed
        // - same benchmark (List, Map, etc)
        // - same application size
93
        // - same base structure size
94
95
96
97
        assert(records.all { record ->
            selectedColumns.map{ record.get(it)} ==
            selectedColumns.map{ records[0].get(it)}})

Noric Couderc's avatar
Noric Couderc committed
98
99
        val maxScore = records.maxBy { getWorstScore(it) }
        return maxScore!!.get("Param: datastructureName")
100
101
    }

Noric Couderc's avatar
Noric Couderc committed
102
    class JMHProcessorException(override val message: String?) : Exception(message)
103
104
105
106
107
108
109
110
111

    fun processBenchmarkName(benchmark : String) : String {
        val options = listOf("List", "Map", "Set")
        val name = benchmark.findAnyOf(options)?.second
        if (name.isNullOrBlank()) {
            throw JMHProcessorException("Benchmark name does not contain any of $options")
        }
        return name
    }
112
113
114
115

    /**
     * Prints the given records to a file
     */
116
    fun print(writer : Writer, records : List<JMHRecord>) {
117
        val printer = CSVPrinter(writer, CSVFormat.DEFAULT.withFirstRecordAsHeader())
118
        printer.printRecord("Interface", "Seed", "Size", "BaseStructureSize", "Best")
119
        for (record in records) {
Noric Couderc's avatar
Noric Couderc committed
120
            printer.printRecord(record.toList())
121
122
123
        }
        writer.close()
    }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

    companion object {
        /**
         * Static method giving the expected CSV header
         * @return A string for the expected CSV header
         */
        fun getExpectedHeader() : String {
            val header =
                    listOf(
                            "Benchmark",
                            "Mode",
                            "Threads",
                            "Samples",
                            "Score",
                            "Score Error (99.9%)",
                            "Unit",
                            "Param: applicationSize",
                            "Param: baseStructureSize",
                            "Param: datastructureName",
                            "Param: seed"
                    ).map { "\"$it\""}.joinToString(",")
            return header
        }
    }
148
}