JMHProcessor.kt 6.18 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
8
9
10
11
import se.lth.cs.bcgen.BCBenchmarkPackage
import java.io.File
import java.io.FileReader
import java.io.Reader
import java.io.Writer
12

13
14
15
/**
 * A Class for processing JMH benchmark data
 */
16
class JMHProcessor {
17

18
    fun process(file: File): List<JMHRecord> {
Noric Couderc's avatar
Noric Couderc committed
19
        return process(FileReader(file))
20
21
    }

22
23
24
25
26
27
28
29
    data class JMHRecord(val seed : Int,
                         val size : Int,
                         val baseStructureSize : Int,
                         val collection : String,
                         val datastructure : String,
                         val best : String,
                         val methodSelection : String? = null) {

Noric Couderc's avatar
Noric Couderc committed
30
        fun toList() : List<String> {
31
            return listOf(collection, seed.toString(), size.toString() , baseStructureSize.toString(), datastructure, best)
Noric Couderc's avatar
Noric Couderc committed
32
        }
33

34
35
        fun generateSyntheticBenchmark() : BCBenchmarkPackage<*>? {
            var syntheticBenchmark : BCBenchmarkPackage<*>? = null
36
            val dataStructure = getClassFromSimpleName(datastructure)
37

38
            if (collection == "List") {
39
                syntheticBenchmark = BCBenchmarkPackage.LIST(seed, size, baseStructureSize, dataStructure as MutableList<Object>?)
40
                return syntheticBenchmark
41
42
43
            }

            if (collection == "Map") {
44
                syntheticBenchmark = BCBenchmarkPackage.MAP(seed, size, baseStructureSize, dataStructure as MutableMap<Any, Any>?)
45
                return syntheticBenchmark
46
47
48
            }

            if (collection == "Set") {
49
                syntheticBenchmark = BCBenchmarkPackage.SET(seed, size, baseStructureSize, dataStructure as MutableSet<Object>?)
50
                return syntheticBenchmark
51
52
53
54
55
56
57
58
            }
            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
59
    }
60

61
62
63
64
65
66
67
    private val selectedColumns = listOf(
            "Benchmark",
            "Param: seed",
            "Param: applicationSize",
            "Param: baseStructureSize"
    )

68
69
70
    /**
     * Reads CSV data from a Reader and produces a list of JMH Record data
     */
71
    fun process(reader : Reader): List<JMHRecord> {
72
        var parser = CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader())
Noric Couderc's avatar
Noric Couderc committed
73
        // We are grouping the parameters by any parameter except the data structure name (which we want)
74

75
        // We group the records by our selected columns
76
77
78
79
        val seedsToRecords = parser.records.groupBy { record ->
            selectedColumns.map { column -> record.get(column) }
        }

80
        return seedsToRecords.values.flatMap { records ->
Noric Couderc's avatar
Noric Couderc committed
81
            val interfaceName = records[0].get("Benchmark").let { processBenchmarkName(it) }
82
            val seed = records[0].get("Param: seed")
Noric Couderc's avatar
Noric Couderc committed
83
                    .let { it.toInt()}
84
            val size = records[0].get("Param: applicationSize")
85
                    .let { Integer.parseInt(it)}
86
87
            val baseStructureSize = records[0].get("Param: baseStructureSize")
                    .let { Integer.parseInt(it)}
88
89
90
91
92
93
94
95
96

            val methodSelection =
                    if (records[0].isSet("Param: methodSelectionStrategyId")) {
                        records[0].get("Param: methodSelectionStrategyId")
                    } else {
                        null
                    }


97
            val best = getBestDataStructure(records)
98

99
100
101
102
            records.map{
                JMHRecord(seed, size, baseStructureSize,
                    interfaceName, it.get("Param: datastructureName"), best!!,
                    methodSelection)
103
            }
104
105
        }
    }
106

107
108
109
110
111
112
113
    /**
     * 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()
    }

114
115
116
117
118
119
    private fun getBestDataStructure(records : List<CSVRecord>): String? {
        // Precondition
        // All records must have
        // - same seed
        // - same benchmark (List, Map, etc)
        // - same application size
120
        // - same base structure size
121
122
123
124
        assert(records.all { record ->
            selectedColumns.map{ record.get(it)} ==
            selectedColumns.map{ records[0].get(it)}})

Noric Couderc's avatar
Noric Couderc committed
125
126
        val maxScore = records.maxBy { getWorstScore(it) }
        return maxScore!!.get("Param: datastructureName")
127
128
    }

Noric Couderc's avatar
Noric Couderc committed
129
    class JMHProcessorException(override val message: String?) : Exception(message)
130
131
132
133
134
135
136
137
138

    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
    }
139
140
141
142

    /**
     * Prints the given records to a file
     */
143
    fun print(writer : Writer, records : List<JMHRecord>) {
144
        val printer = CSVPrinter(writer, CSVFormat.DEFAULT.withFirstRecordAsHeader())
145
        printer.printRecord("Interface", "Seed", "Size", "BaseStructureSize", "Best")
146
        for (record in records) {
Noric Couderc's avatar
Noric Couderc committed
147
            printer.printRecord(record.toList())
148
149
150
        }
        writer.close()
    }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

    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
        }
    }
175
}