Skip to content

Commit 62a0c28

Browse files
author
Antoine Brunner
committed
Add input facilities to benchmarks and start making serious benchmarks
1 parent 013b93c commit 62a0c28

File tree

7 files changed

+118
-92
lines changed

7 files changed

+118
-92
lines changed

bench-run/inputs/concat.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sizes:0 0,0 5,0 10,0 15,0 20,0 25,0 30,5 0,5 5,5 10,5 15,5 20,5 25,5 30,10 0,10 5,10 10,10 15,10 20,10 25,10 30,15 0,15 5,15 10,15 15,15 20,15 25,15 30,20 0,20 5,20 10,20 15,20 20,20 25,20 30,25 0,25 5,25 10,25 15,25 20,25 25,25 30,30 0,30 5,30 10,30 15,30 20,30 25,30 30

bench-run/outputs/concat.csv

Whitespace-only changes.
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package dotty.tools.benchmarks
22

3-
import dotty.tools.dotc._
4-
import core.Contexts.Context
5-
import dotty.tools.FatalError
6-
import reporting._
7-
83
import org.openjdk.jmh.results.RunResult
94
import org.openjdk.jmh.runner.Runner
105
import org.openjdk.jmh.runner.options.OptionsBuilder
116
import org.openjdk.jmh.annotations._
127
import org.openjdk.jmh.results.format._
138
import java.util.concurrent.TimeUnit
149

15-
import scala.runtime.DynamicTuple
10+
import scala.io.Source
1611

1712
object Bench {
1813
def main(args: Array[String]): Unit = {
@@ -23,9 +18,9 @@ object Bench {
2318
val forks = if (intArgs.length > 2) intArgs(2).toInt else 1
2419

2520
val benchmarks = if (args1.length > 0) args1(0) else ".*"
26-
val outputFile = if (args1.length > 1) args1(1) else "output.csv"
21+
val outputFile = if (args1.length > 2) args1(2) else "output.csv"
2722

28-
val opts = new OptionsBuilder()
23+
var builder = new OptionsBuilder()
2924
.shouldFailOnError(true)
3025
.jvmArgs("-Xms2G", "-Xmx2G")
3126
.mode(Mode.AverageTime)
@@ -35,10 +30,21 @@ object Bench {
3530
.forks(forks)
3631
.include(benchmarks)
3732
.resultFormat(ResultFormatType.CSV)
38-
.result("results/" ++ outputFile)
39-
.build
33+
.result(outputFile)
34+
35+
if (args1.length > 1) {
36+
for ((param, values) <- paramsFromFile(args1(1)))
37+
builder = builder.param(param, values: _*)
38+
}
39+
40+
val runner = new Runner(builder.build) // full access to all JMH features, you can also provide a custom output Format here
41+
runner.run // actually run the benchmarks
42+
}
4043

41-
val runner = new Runner(opts) // full access to all JMH features, you can also provide a custom output Format here
42-
runner.run() // actually run the benchmarks
44+
def paramsFromFile(file: String): Array[(String, Array[String])] = {
45+
Source.fromFile(file).getLines.toArray.map { l =>
46+
val Array(param, values) = l split ':'
47+
(param, values split ',')
48+
}
4349
}
4450
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dotty.tools.benchmarks.tuples
2+
3+
import org.openjdk.jmh.annotations._
4+
import scala.runtime.DynamicTuple
5+
6+
@State(Scope.Thread)
7+
class ApplyBenchmarks {
8+
@Param(Array("1 0"))
9+
var sizeAndIndex: String = _
10+
var tuple: NonEmptyTuple = _
11+
var index: Int = _
12+
13+
@Setup
14+
def setup(): Unit = {
15+
val size = sizeAndIndex.split(' ')(0).toInt
16+
index = sizeAndIndex.split(' ')(1).toInt
17+
tuple = "elem" *: ()
18+
19+
for (i <- 1 until size)
20+
tuple = "string" *: tuple
21+
}
22+
23+
@Benchmark
24+
def baseline(): Unit = {}
25+
26+
@Benchmark
27+
def normal(): Any = {
28+
tuple(index)
29+
}
30+
31+
@Benchmark
32+
def inlined(): Any = {
33+
DynamicTuple.dynamicApply(tuple, index)
34+
}
35+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package dotty.tools.benchmarks.tuples
2+
3+
import org.openjdk.jmh.annotations._
4+
import org.openjdk.jmh.infra.Blackhole
5+
import scala.runtime.DynamicTuple
6+
7+
@State(Scope.Thread)
8+
class Concat {
9+
@Param(Array("0 0"))
10+
var sizes: String = _
11+
var tuple1: Tuple = _
12+
var tuple2: Tuple = _
13+
var array1: Array[Object] = _
14+
var array2: Array[Object] = _
15+
16+
def tupleOfSize(n: Int): Tuple = {
17+
var t: Tuple = ()
18+
for (i <- 1 to n)
19+
t = "elem" *: t
20+
t
21+
}
22+
23+
@Setup
24+
def setup(): Unit = {
25+
val size1 = sizes.split(' ')(0).toInt
26+
val size2 = sizes.split(' ')(1).toInt
27+
tuple1 = tupleOfSize(size1)
28+
tuple1 = tupleOfSize(size2)
29+
array1 = Array.fill(size1)("elem")
30+
array2 = Array.fill(size2)("elem")
31+
}
32+
33+
@Benchmark
34+
def baseline(): Unit = {}
35+
36+
@Benchmark
37+
def normal(): Tuple = {
38+
tuple1 ++ tuple2
39+
}
40+
41+
@Benchmark
42+
def inlined(): Tuple = {
43+
DynamicTuple.dynamicConcat(tuple1, tuple2)
44+
}
45+
46+
// This part is here to try and measure the overhead of tranforming tuples to arrays, then concatenating
47+
// the array, and then transforming back to a tuple
48+
@Benchmark
49+
def toArray(bh: Blackhole): Unit = {
50+
bh.consume(DynamicTuple.dynamicToArray(tuple1))
51+
bh.consume(DynamicTuple.dynamicToArray(tuple2))
52+
}
53+
54+
@Benchmark
55+
def arrayConcat(): Array[Object] = {
56+
array1 ++ array2
57+
}
58+
59+
@Benchmark
60+
def fromArray(bh: Blackhole): Unit = {
61+
bh.consume(DynamicTuple.dynamicFromArray[Tuple](array1))
62+
bh.consume(DynamicTuple.dynamicFromArray[Tuple](array2))
63+
}
64+
}

bench-run/src/main/scala/tuples/Tuples.scala

Lines changed: 0 additions & 79 deletions
This file was deleted.

bench/src/main/scala/Benchmarks.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ object Bench {
3030
val iterations = if (intArgs.length > 1) intArgs(1).toInt else 20
3131
val forks = if (intArgs.length > 2) intArgs(2).toInt else 1
3232

33-
3433
import File.{ separator => sep }
3534

3635
val args2 = args1.map { arg =>

0 commit comments

Comments
 (0)