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

import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
5
6
import org.apache.commons.csv.CSVPrinter
import java.io.*
7
import java.lang.Exception
8
9

class JMHProcessor {
10

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

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

19
20
21
22
23
        val selectedColumns = listOf(
                "Benchmark",
                "Param: seed",
                "Param: applicationSize"
        )
24
25
26
27
28
29

        val seedsToRecords = parser.records.groupBy { record ->
            selectedColumns.map { column -> record.get(column) }
        }

        return seedsToRecords.values.map { records ->
Noric Couderc's avatar
Noric Couderc committed
30
            val interfaceName = records[0].get("Benchmark").let { processBenchmarkName(it) }
31
32
            val seed = records[0].get("Param: seed")
            val size = records[0].get("Param: applicationSize")
33
34
35
36
37
38
39
40
41
42
43
44
            // We need to group the runs by data structure size too.
            // We match the size with the higest score found
            val recordsByBaseSize =
                    records.groupBy { it.get("Param: baseStructureSize") }
                            .mapValues { (k, v) -> v.maxBy { it.get("Score") }}
            // We count the number of times the data structure has won
            // (Computing a histogram of the data structure names)
            val bestScoreHist =
                    recordsByBaseSize.values.groupBy { it!!.get("Param: datastructureName") }
                            .mapValues { (k, v) -> v.size}
            val bestScore = bestScoreHist.maxBy { (k, v) -> v }!!.key
            listOf(interfaceName, seed, size, bestScore)
45
46
        }
    }
47

Noric Couderc's avatar
Noric Couderc committed
48
    class JMHProcessorException(override val message: String?) : Exception(message)
49
50
51
52
53
54
55
56
57

    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
    }
58
59
60
61
62
63
64
65
66
67
68
69

    /**
     * Prints the given records to a file
     */
    fun print(writer : Writer, records : List<List<String>>) {
        val printer = CSVPrinter(writer, CSVFormat.DEFAULT.withFirstRecordAsHeader())
        printer.printRecord("Interface", "Seed", "Size", "Best")
        for (record in records) {
            printer.printRecord(record)
        }
        writer.close()
    }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

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