JMHProcessor.kt 3.84 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

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

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

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

25
26
27
28
29
        val selectedColumns = listOf(
                "Benchmark",
                "Param: seed",
                "Param: applicationSize"
        )
30
31
32
33
34
35

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

        return seedsToRecords.values.map { records ->
Noric Couderc's avatar
Noric Couderc committed
36
            val interfaceName = records[0].get("Benchmark").let { processBenchmarkName(it) }
37
            val seed = records[0].get("Param: seed")
38
                    .let { Integer.parseInt(it)}
39
            val size = records[0].get("Param: applicationSize")
40
                    .let { Integer.parseInt(it)}
41
42
43
44
45
46
47
48
49
50
51
            // 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
52
53
            // listOf(interfaceName, seed, size, bestScore)
            JMHRecord(seed, size, interfaceName, bestScore)
54
55
        }
    }
56

Noric Couderc's avatar
Noric Couderc committed
57
    class JMHProcessorException(override val message: String?) : Exception(message)
58
59
60
61
62
63
64
65
66

    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
    }
67
68
69
70

    /**
     * Prints the given records to a file
     */
71
    fun print(writer : Writer, records : List<JMHRecord>) {
72
73
74
        val printer = CSVPrinter(writer, CSVFormat.DEFAULT.withFirstRecordAsHeader())
        printer.printRecord("Interface", "Seed", "Size", "Best")
        for (record in records) {
Noric Couderc's avatar
Noric Couderc committed
75
            printer.printRecord(record.toList())
76
77
78
        }
        writer.close()
    }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

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