JMHProcessor.kt 2.3 KB
Newer Older
1
2
3
4
5
6
7
package se.lth.cs

import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
import java.io.File
import java.io.FileReader
import java.io.Reader
8
import java.lang.Exception
9
10

class JMHProcessor {
11

12
    fun processFile(filename: String): List<List<String?>> {
13
14
15
        return processReader(FileReader(File(filename)))
    }

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

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

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

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

    class JMHProcessorException(override val message: String?) : Exception(message) { }

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