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

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

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

23
24
25
26
27
28
29
30
    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
31
        fun toList() : List<String> {
32
            return listOf(collection, seed.toString(), size.toString() , baseStructureSize.toString(), datastructure, best)
Noric Couderc's avatar
Noric Couderc committed
33
        }
34

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

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

            if (collection == "Map") {
47
48
49
                syntheticBenchmark = BCBenchmarkPackage.MAP(seed, size, baseStructureSize,
                        MethodSelectionType.fromString(methodSelection),
                        dataStructure as MutableMap<Any, Any>?)
50
                return syntheticBenchmark
51
52
53
            }

            if (collection == "Set") {
54
55
56
                syntheticBenchmark = BCBenchmarkPackage.SET(seed, size, baseStructureSize,
                        MethodSelectionType.fromString(methodSelection),
                        dataStructure as MutableSet<Object>?)
57
                return syntheticBenchmark
58
59
60
61
62
63
64
65
            }
            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
66
    }
67

68
69
70
71
72
73
74
    private val selectedColumns = listOf(
            "Benchmark",
            "Param: seed",
            "Param: applicationSize",
            "Param: baseStructureSize"
    )

75
76
77
    /**
     * Reads CSV data from a Reader and produces a list of JMH Record data
     */
78
    fun process(reader : Reader): List<JMHRecord> {
79
        var parser = CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader())
Noric Couderc's avatar
Noric Couderc committed
80
        // We are grouping the parameters by any parameter except the data structure name (which we want)
81

82
        // We group the records by our selected columns
83
84
85
86
        val seedsToRecords = parser.records.groupBy { record ->
            selectedColumns.map { column -> record.get(column) }
        }

87
        return seedsToRecords.values.flatMap { records ->
Noric Couderc's avatar
Noric Couderc committed
88
            val interfaceName = records[0].get("Benchmark").let { processBenchmarkName(it) }
89
            val seed = records[0].get("Param: seed")
Noric Couderc's avatar
Noric Couderc committed
90
                    .let { it.toInt()}
91
            val size = records[0].get("Param: applicationSize")
92
                    .let { Integer.parseInt(it)}
93
94
            val baseStructureSize = records[0].get("Param: baseStructureSize")
                    .let { Integer.parseInt(it)}
95
96
97
98
99
100
101
102
103

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


104
            val best = getBestDataStructure(records)
105

106
107
108
109
            records.map{
                JMHRecord(seed, size, baseStructureSize,
                    interfaceName, it.get("Param: datastructureName"), best!!,
                    methodSelection)
110
            }
111
112
        }
    }
113

114
115
116
117
118
119
120
    /**
     * 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()
    }

121
122
123
124
125
126
    private fun getBestDataStructure(records : List<CSVRecord>): String? {
        // Precondition
        // All records must have
        // - same seed
        // - same benchmark (List, Map, etc)
        // - same application size
127
        // - same base structure size
128
129
130
131
        assert(records.all { record ->
            selectedColumns.map{ record.get(it)} ==
            selectedColumns.map{ records[0].get(it)}})

Noric Couderc's avatar
Noric Couderc committed
132
133
        val maxScore = records.maxBy { getWorstScore(it) }
        return maxScore!!.get("Param: datastructureName")
134
135
    }

Noric Couderc's avatar
Noric Couderc committed
136
    class JMHProcessorException(override val message: String?) : Exception(message)
137
138
139
140
141
142
143
144
145

    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
    }
146
147
148
149

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

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