Skip to content

Commit 41a90b5

Browse files
committed
NIT Move the BSP-related JMH tests to the JmhTests suite
1 parent c2ce8cd commit 41a90b5

File tree

2 files changed

+30
-57
lines changed

2 files changed

+30
-57
lines changed

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

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -263,54 +263,6 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
263263
}
264264
}
265265

266-
for {
267-
useDirective <- Seq(None, Some("//> using jmh"))
268-
directiveString = useDirective.getOrElse("")
269-
jmhOptions = if (useDirective.isEmpty) Seq("--jmh") else Nil
270-
testMessage = useDirective match {
271-
case None => jmhOptions.mkString(" ")
272-
case Some(directive) => directive
273-
}
274-
}
275-
test(s"simple jmh ($testMessage)") {
276-
val inputs = TestInputs(
277-
os.rel / "benchmark.scala" ->
278-
s"""$directiveString
279-
|package bench
280-
|
281-
|import java.util.concurrent.TimeUnit
282-
|import org.openjdk.jmh.annotations._
283-
|
284-
|@BenchmarkMode(Array(Mode.AverageTime))
285-
|@OutputTimeUnit(TimeUnit.NANOSECONDS)
286-
|@Warmup(iterations = 1, time = 100, timeUnit = TimeUnit.MILLISECONDS)
287-
|@Measurement(iterations = 10, time = 100, timeUnit = TimeUnit.MILLISECONDS)
288-
|@Fork(0)
289-
|class Benchmarks {
290-
|
291-
| @Benchmark
292-
| def foo(): Unit = {
293-
| (1L to 10000000L).sum
294-
| }
295-
|
296-
|}
297-
|""".stripMargin
298-
)
299-
300-
withBsp(inputs, Seq(".", "--power") ++ jmhOptions) { (_, _, remoteServer) =>
301-
async {
302-
val buildTargetsResp = await(remoteServer.workspaceBuildTargets().asScala)
303-
val targets = buildTargetsResp.getTargets.asScala.map(_.getId).toSeq
304-
expect(targets.length == 2)
305-
306-
val compileResult =
307-
await(remoteServer.buildTargetCompile(new b.CompileParams(targets.asJava)).asScala)
308-
expect(compileResult.getStatusCode == b.StatusCode.OK)
309-
310-
}
311-
}
312-
}
313-
314266
test("diagnostics") {
315267
val inputs = TestInputs(
316268
os.rel / "Test.scala" ->

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package scala.cli.integration
22

3+
import ch.epfl.scala.bsp4j as b
34
import com.eed3si9n.expecty.Expecty.expect
45

56
import java.nio.file.Files
67

8+
import scala.async.Async.{async, await}
9+
import scala.concurrent.ExecutionContext.Implicits.global
10+
import scala.jdk.CollectionConverters.*
711
import scala.util.Properties
812

9-
class JmhTests extends ScalaCliSuite with JmhSuite {
10-
override def group: ScalaCliSuite.TestGroup = ScalaCliSuite.TestGroup.First
13+
class JmhTests extends ScalaCliSuite with JmhSuite with BspSuite {
14+
override def group: ScalaCliSuite.TestGroup = ScalaCliSuite.TestGroup.First
15+
override protected val extraOptions: Seq[String] = TestUtil.extraOptions
1116

1217
for {
1318
useDirective <- Seq(None, Some("//> using jmh"))
@@ -22,7 +27,7 @@ class JmhTests extends ScalaCliSuite with JmhSuite {
2227
// TODO extract running benchmarks to a separate scope, or a separate sub-command
2328
simpleBenchmarkingInputs(directiveString).fromRoot { root =>
2429
val res =
25-
os.proc(TestUtil.cli, "--power", TestUtil.extraOptions, ".", jmhOptions).call(cwd = root)
30+
os.proc(TestUtil.cli, "--power", extraOptions, ".", jmhOptions).call(cwd = root)
2631
val output = res.out.trim()
2732
expect(output.contains(expectedInBenchmarkingOutput))
2833
expect(output.contains(s"JMH version: ${Constants.jmhVersion}"))
@@ -31,14 +36,14 @@ class JmhTests extends ScalaCliSuite with JmhSuite {
3136

3237
test(s"compile ($testMessage)") {
3338
simpleBenchmarkingInputs(directiveString).fromRoot { root =>
34-
os.proc(TestUtil.cli, "compile", "--power", TestUtil.extraOptions, ".", jmhOptions)
39+
os.proc(TestUtil.cli, "compile", "--power", extraOptions, ".", jmhOptions)
3540
.call(cwd = root)
3641
}
3742
}
3843

3944
test(s"doc ($testMessage)") {
4045
simpleBenchmarkingInputs(directiveString).fromRoot { root =>
41-
val res = os.proc(TestUtil.cli, "doc", "--power", TestUtil.extraOptions, ".", jmhOptions)
46+
val res = os.proc(TestUtil.cli, "doc", "--power", extraOptions, ".", jmhOptions)
4247
.call(cwd = root, stderr = os.Pipe)
4348
expect(!res.err.trim().contains("Error"))
4449
}
@@ -47,11 +52,27 @@ class JmhTests extends ScalaCliSuite with JmhSuite {
4752
test(s"setup-ide ($testMessage)") {
4853
// TODO fix setting jmh via a reload & add tests for it
4954
simpleBenchmarkingInputs(directiveString).fromRoot { root =>
50-
os.proc(TestUtil.cli, "setup-ide", "--power", TestUtil.extraOptions, ".", jmhOptions)
55+
os.proc(TestUtil.cli, "setup-ide", "--power", extraOptions, ".", jmhOptions)
5156
.call(cwd = root)
5257
}
5358
}
5459

60+
test(s"bsp ($testMessage)") {
61+
withBsp(simpleBenchmarkingInputs(directiveString), Seq(".", "--power") ++ jmhOptions) {
62+
(_, _, remoteServer) =>
63+
async {
64+
val buildTargetsResp = await(remoteServer.workspaceBuildTargets().asScala)
65+
val targets = buildTargetsResp.getTargets.asScala.map(_.getId).toSeq
66+
expect(targets.length == 2)
67+
68+
val compileResult =
69+
await(remoteServer.buildTargetCompile(new b.CompileParams(targets.asJava)).asScala)
70+
expect(compileResult.getStatusCode == b.StatusCode.OK)
71+
72+
}
73+
}
74+
}
75+
5576
test(s"package ($testMessage)") {
5677
// TODO make package with --jmh build an artifact that actually runs benchmarks
5778
val expectedMessage = "Placeholder main method"
@@ -84,7 +105,7 @@ class JmhTests extends ScalaCliSuite with JmhSuite {
84105
test(s"export ($testMessage)") {
85106
simpleBenchmarkingInputs(directiveString).fromRoot { root =>
86107
// TODO add proper support for JMH export, we're checking if it doesn't fail the command for now
87-
os.proc(TestUtil.cli, "export", "--power", TestUtil.extraOptions, ".", jmhOptions)
108+
os.proc(TestUtil.cli, "export", "--power", extraOptions, ".", jmhOptions)
88109
.call(cwd = root)
89110
}
90111
}
@@ -102,7 +123,7 @@ class JmhTests extends ScalaCliSuite with JmhSuite {
102123
} test(s"should not compile when jmh is explicitly disabled ($testMessage)") {
103124
simpleBenchmarkingInputs(directiveString).fromRoot { root =>
104125
val res =
105-
os.proc(TestUtil.cli, "compile", "--power", TestUtil.extraOptions, ".", jmhOptions)
126+
os.proc(TestUtil.cli, "compile", "--power", extraOptions, ".", jmhOptions)
106127
.call(cwd = root, check = false)
107128
expect(res.exitCode == 1)
108129
}
@@ -128,7 +149,7 @@ class JmhTests extends ScalaCliSuite with JmhSuite {
128149
} test(s"should use the passed jmh version ($testMessage)") {
129150
simpleBenchmarkingInputs(directiveString).fromRoot { root =>
130151
val res =
131-
os.proc(TestUtil.cli, "run", "--power", TestUtil.extraOptions, ".", jmhOptions)
152+
os.proc(TestUtil.cli, "run", "--power", extraOptions, ".", jmhOptions)
132153
.call(cwd = root)
133154
val output = res.out.trim()
134155
expect(output.contains(expectedInBenchmarkingOutput))

0 commit comments

Comments
 (0)