Skip to content

Commit ccd1ceb

Browse files
committed
NIT extract a helper trait for JMH tests
1 parent af0e350 commit ccd1ceb

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package scala.cli.integration
2+
3+
trait JmhSuite { _: ScalaCliSuite =>
4+
protected lazy val simpleInputs: TestInputs = TestInputs(
5+
os.rel / "benchmark.scala" ->
6+
s"""package bench
7+
|
8+
|import java.util.concurrent.TimeUnit
9+
|import org.openjdk.jmh.annotations._
10+
|
11+
|@BenchmarkMode(Array(Mode.AverageTime))
12+
|@OutputTimeUnit(TimeUnit.NANOSECONDS)
13+
|@Warmup(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
14+
|@Measurement(iterations = 10, time = 100, timeUnit = TimeUnit.MILLISECONDS)
15+
|@Fork(0)
16+
|class Benchmarks {
17+
|
18+
| @Benchmark
19+
| def foo(): Unit = {
20+
| (1L to 10000000L).sum
21+
| }
22+
|
23+
|}
24+
|""".stripMargin
25+
)
26+
protected lazy val expectedInOutput = """Result "bench.Benchmarks.foo":"""
27+
}

modules/integration/src/test/scala/scala/cli/integration/JmhTests.scala

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,12 @@ import java.nio.file.Files
66

77
import scala.util.Properties
88

9-
class JmhTests extends ScalaCliSuite {
9+
class JmhTests extends ScalaCliSuite with JmhSuite {
1010
override def group: ScalaCliSuite.TestGroup = ScalaCliSuite.TestGroup.First
1111

12-
lazy val inputs: TestInputs = TestInputs(
13-
os.rel / "benchmark.scala" ->
14-
s"""package bench
15-
|
16-
|import java.util.concurrent.TimeUnit
17-
|import org.openjdk.jmh.annotations._
18-
|
19-
|@BenchmarkMode(Array(Mode.AverageTime))
20-
|@OutputTimeUnit(TimeUnit.NANOSECONDS)
21-
|@Warmup(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
22-
|@Measurement(iterations = 10, time = 100, timeUnit = TimeUnit.MILLISECONDS)
23-
|@Fork(0)
24-
|class Benchmarks {
25-
|
26-
| @Benchmark
27-
| def foo(): Unit = {
28-
| (1L to 10000000L).sum
29-
| }
30-
|
31-
|}
32-
|""".stripMargin
33-
)
34-
lazy val expectedInOutput = """Result "bench.Benchmarks.foo":"""
35-
3612
test("simple") {
3713
// TODO extract running benchmarks to a separate scope, or a separate sub-command
38-
inputs.fromRoot { root =>
14+
simpleInputs.fromRoot { root =>
3915
val res =
4016
os.proc(TestUtil.cli, "--power", TestUtil.extraOptions, ".", "--jmh").call(cwd = root)
4117
val output = res.out.trim()
@@ -44,14 +20,14 @@ class JmhTests extends ScalaCliSuite {
4420
}
4521

4622
test("compile") {
47-
inputs.fromRoot { root =>
23+
simpleInputs.fromRoot { root =>
4824
os.proc(TestUtil.cli, "compile", "--power", TestUtil.extraOptions, ".", "--jmh")
4925
.call(cwd = root)
5026
}
5127
}
5228

5329
test("doc") {
54-
inputs.fromRoot { root =>
30+
simpleInputs.fromRoot { root =>
5531
val res = os.proc(TestUtil.cli, "doc", "--power", TestUtil.extraOptions, ".", "--jmh")
5632
.call(cwd = root, stderr = os.Pipe)
5733
expect(!res.err.trim().contains("Error"))
@@ -60,7 +36,7 @@ class JmhTests extends ScalaCliSuite {
6036

6137
test("setup-ide") {
6238
// TODO fix setting jmh via a reload & add tests for it
63-
inputs.fromRoot { root =>
39+
simpleInputs.fromRoot { root =>
6440
os.proc(TestUtil.cli, "setup-ide", "--power", TestUtil.extraOptions, ".", "--jmh")
6541
.call(cwd = root)
6642
}
@@ -69,7 +45,7 @@ class JmhTests extends ScalaCliSuite {
6945
test("package") {
7046
// TODO make package with --jmh build an artifact that actually runs benchmarks
7147
val expectedMessage = "Placeholder main method"
72-
inputs
48+
simpleInputs
7349
.add(os.rel / "Main.scala" -> s"""@main def main: Unit = println("$expectedMessage")""")
7450
.fromRoot { root =>
7551
val launcherName = {
@@ -96,7 +72,7 @@ class JmhTests extends ScalaCliSuite {
9672
}
9773

9874
test("export") {
99-
inputs.fromRoot { root =>
75+
simpleInputs.fromRoot { root =>
10076
// TODO add proper support for JMH export, we're checking if it doesn't fail the command for now
10177
os.proc(TestUtil.cli, "export", "--power", TestUtil.extraOptions, ".", "--jmh")
10278
.call(cwd = root)

0 commit comments

Comments
 (0)