Skip to content

Commit 9197528

Browse files
author
Antoine Brunner
committed
Add new configuration for runtime benchmarks
1 parent c900e88 commit 9197528

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package dotty.tools.benchmarks
2+
3+
import dotty.tools.dotc._
4+
import core.Contexts.Context
5+
import dotty.tools.FatalError
6+
import reporting._
7+
8+
import org.openjdk.jmh.results.RunResult
9+
import org.openjdk.jmh.runner.Runner
10+
import org.openjdk.jmh.runner.options.OptionsBuilder
11+
import org.openjdk.jmh.annotations._
12+
import org.openjdk.jmh.results.format._
13+
import java.util.concurrent.TimeUnit
14+
15+
import scala.runtime.DynamicTuple
16+
17+
object Bench {
18+
def main(args: Array[String]): Unit = {
19+
val (intArgs, args1) = args.span(x => try { x.toInt; true } catch { case _: Throwable => false } )
20+
21+
val warmup = if (intArgs.length > 0) intArgs(0).toInt else 30
22+
val iterations = if (intArgs.length > 1) intArgs(1).toInt else 20
23+
val forks = if (intArgs.length > 2) intArgs(2).toInt else 1
24+
25+
val opts = new OptionsBuilder()
26+
.shouldFailOnError(true)
27+
.jvmArgs("-Xms2G", "-Xmx2G")
28+
.mode(Mode.AverageTime)
29+
.timeUnit(TimeUnit.NANOSECONDS)
30+
.warmupIterations(warmup)
31+
.measurementIterations(iterations)
32+
.forks(forks)
33+
.build
34+
35+
val runner = new Runner(opts) // full access to all JMH features, you can also provide a custom output Format here
36+
runner.run() // actually run the benchmarks
37+
}
38+
}
39+
40+
@State(Scope.Thread)
41+
class ConsConcatBenchmarks {
42+
@Param(Array("10", "20"))
43+
var size: Int = _
44+
var tuple: Tuple = _
45+
46+
@Setup
47+
def setup(): Unit = {
48+
tuple = ()
49+
50+
for (i <- 1 to size)
51+
tuple = "string" *: tuple
52+
}
53+
54+
@Benchmark
55+
def baseline(): Unit = {}
56+
57+
// Cons benchmarks
58+
@Benchmark
59+
def tupleCons(): Tuple = {
60+
"string" *: tuple
61+
}
62+
63+
@Benchmark
64+
def dynamicTupleCons(): Tuple = {
65+
DynamicTuple.dynamicCons("string", tuple)
66+
}
67+
68+
// Concat benchmarks
69+
@Benchmark
70+
def tupleConcat(): Tuple = {
71+
tuple ++ tuple
72+
}
73+
74+
@Benchmark
75+
def dynamicTupleConcat(): Tuple = {
76+
DynamicTuple.dynamicConcat(tuple, tuple)
77+
}
78+
79+
// Interesting behaviour : I expect the two following to be the same because of TupleOptimizations.scala
80+
@Benchmark
81+
def tupleConcat2(): Tuple = {
82+
("string", "string") ++ ("string", "string")
83+
}
84+
85+
@Benchmark
86+
def tupleNoConcat(): Tuple = {
87+
("string", "string", "string", "string")
88+
}
89+
}
90+
91+
@State(Scope.Thread)
92+
class ApplyBenchmarks {
93+
val size: Int = 100
94+
95+
val tuple: NonEmptyTuple = {
96+
var t = ()
97+
for (i <- 1 to size)
98+
t = "string" *: t
99+
t.asInstanceOf[NonEmptyTuple]
100+
}
101+
102+
@Param(Array("1", "10", "25", "50", "75", "99"))
103+
var index: Int = _
104+
105+
// Apply benchmarks, doesn't work for some reason
106+
// @Benchmark
107+
// def tupleApply(): Unit = {
108+
// DynamicTuple.dynamicApply(tuple, index)
109+
// }
110+
}

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ val `tasty-core-bootstrapped` = Build.`tasty-core-bootstrapped`
1919
val `tasty-core-scala2` = Build.`tasty-core-scala2`
2020
val `dotty-tastydoc` = Build.`dotty-tastydoc`
2121
val `dotty-tastydoc-input` = Build.`dotty-tastydoc-input`
22+
val `dotty-bench-run` = Build.`dotty-bench-run`
2223
val `scala-library` = Build.`scala-library`
2324
val `scala-compiler` = Build.`scala-compiler`
2425
val `scala-reflect` = Build.`scala-reflect`

project/Build.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@ object Build {
10421042

10431043
lazy val `dotty-bench` = project.in(file("bench")).asDottyBench(NonBootstrapped)
10441044
lazy val `dotty-bench-bootstrapped` = project.in(file("bench")).asDottyBench(Bootstrapped)
1045+
lazy val `dotty-bench-run` = project.in(file("bench-run")).asDottyBench(Bootstrapped)
10451046

10461047
lazy val `dotty-tastydoc` = project.in(file("tastydoc")).asDottyTastydoc(Bootstrapped)
10471048
lazy val `dotty-tastydoc-input` = project.in(file("tastydoc/input")).asDottyTastydocInput(Bootstrapped)

0 commit comments

Comments
 (0)