Skip to content

Commit aca542d

Browse files
authored
Merge pull request #53 from retronym/topic/profile
Add SBT command to collect profiles
2 parents 775d9b8 + 0163301 commit aca542d

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

build.sbt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import sbt.complete.DefaultParsers.OptSpace
2+
import sbt.complete.Parser
3+
14
name := "compiler-benchmark"
25

36
version := "1.0-SNAPSHOT"
@@ -93,6 +96,41 @@ addCommandAlias("hot", "compilation/jmh:run HotScalacBenchmark -foe true")
9396

9497
addCommandAlias("cold", "compilation/jmh:run ColdScalacBenchmark -foe true")
9598

99+
100+
def profParser(s: State): Parser[String] = {
101+
import Parser._
102+
token("prof" ~> OptSpace) flatMap { _ => matched(s.combinedParser)} map (_.trim)
103+
}
104+
105+
commands += Command.custom((s: State) => Command.applyEffect(profParser(s))((line: String) => {
106+
val flameGraphOpts = s"--minwidth,1,--colors,java,--cp,--width,1800"
107+
abstract class Profiler(val name: String) {
108+
def command(outDir: File): String
109+
}
110+
111+
object basic extends Profiler("basic") {
112+
def command(outDir: File): String = "-jvmArgs -Xprof -prof hs_comp -prof hs_gc -prof stack -prof hs_rt"
113+
}
114+
object jfr extends Profiler("jfr") {
115+
def command(outDir: File): String = s"-prof jmh.extras.JFR:dir=${outDir.getAbsolutePath};flameGraphOpts=$flameGraphOpts;verbose=true'"
116+
}
117+
object async extends Profiler("async") {
118+
def command(outDir: File): String = s"-prof jmh.extras.Async:dir=${outDir.getAbsolutePath};flameGraphOpts=$flameGraphOpts;verbose=true;event=cpu" // + ";simpleName=true" TODO add this after upgrading next sbt-jmh release
119+
}
120+
object perfNorm extends Profiler("perfNorm") {
121+
def command(outDir: File): String = "-prof perfnorm"
122+
}
123+
124+
val profs = List(perfNorm, basic, async, jfr)
125+
val commands: List[String] = profs.flatMap { (prof: Profiler) =>
126+
val outDir = file(s"target/profile-${prof.name}")
127+
IO.createDirectory(outDir)
128+
List(line + " -jvmArgs -Dsun.reflect.inflationThreshold=0 " + prof.command(outDir) + s" -o ${(outDir / "jmh.log").getAbsolutePath} -rf json -rff ${(outDir / "result.json").getAbsolutePath}", BasicCommandStrings.FailureWall)
129+
}
130+
s.copy(remainingCommands = BasicCommandStrings.ClearOnFailure :: commands ++ s.remainingCommands)
131+
}))
132+
133+
96134
def addJmh(project: Project): Project = {
97135
// IntelliJ SBT project import doesn't like sbt-jmh's default setup, which results the prod and test
98136
// output paths overlapping. This is because sbt-jmh declares the `jmh` config as extending `test`, but

infrastructure/src/main/java/scala/bench/ScalacBenchmarkRunner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ else return new OptionsBuilder()
6767

6868
public static void main(String[] args) throws Exception {
6969
CommandLineOptions opts = new CommandLineOptions(args);
70-
// Support `-h`, copied from org.openjdk.jmh.Main. Could also add -l/lp/lprof/lrf.
71-
if (opts.shouldHelp()) {
72-
opts.showHelp();
70+
// Support `-h/-l/lp/lprof/lrf`.
71+
if (opts.shouldHelp() || opts.shouldList() || opts.shouldListProfilers() || opts.shouldListResultFormats() || opts.shouldListWithParams()) {
72+
org.openjdk.jmh.Main.main(args);
7373
return;
7474
}
7575
new Runner(setCorpusVersion(opts)).run();

infrastructure/src/main/java/scala/bench/UploadingRunner.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
public class UploadingRunner {
1717
public static void main(String[] args) throws Exception {
1818
Options opts = ScalacBenchmarkRunner.setCorpusVersion(new CommandLineOptions(args));
19-
Runner runner = new Runner(opts, new UploadingOutputFormat(createOutputFormat(opts)));
19+
OutputFormat outputFormat = createOutputFormat(opts);
20+
if (opts.getProfilers().size() == 0) {
21+
outputFormat = new UploadingOutputFormat(outputFormat);
22+
}
23+
Runner runner = new Runner(opts, outputFormat);
2024
runner.run();
2125
}
2226

project/plugins.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
logLevel := Level.Warn
33

44
// sbt-jmh plugin - pulls in JMH dependencies too
5-
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.25")
5+
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.0")
66

77
// sbt-dotty plugin - to support `scalaVersion := "0.x"`
88
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.1.2")

0 commit comments

Comments
 (0)