JMHProcessor.kt 5.35 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
10

class JMHProcessor {
11

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

16
    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
17
        fun toList() : List<String> {
18
            return listOf(collection, seed.toString(), size.toString() , baseStructureSize.toString(), datastructure, best)
Noric Couderc's avatar
Noric Couderc committed
19
        }
20
21
22
23
24

        fun generateApplication() : Application<*>? {
            var application : Application<*>? = null
            val dataStructure = getClassFromSimpleName(datastructure)
            if (collection == "List") {
25
                application = ListApplication(seed, size, baseStructureSize, dataStructure as MutableList<Int>?)
26
27
28
29
                return application
            }

            if (collection == "Map") {
30
                application = MapApplication(seed, size, baseStructureSize, dataStructure as MutableMap<Int, Int>?)
31
32
33
34
                return application
            }

            if (collection == "Set") {
35
                application = SetApplication(seed, size, baseStructureSize, dataStructure as MutableSet<Int>?)
36
37
38
39
40
41
42
43
44
                return application
            }
            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
45
    }
46

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

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

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

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

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

79
80
81
82
83
84
85
    /**
     * 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()
    }

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

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

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

    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
    }
111
112
113
114

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

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