Skip to content

Commit b7cafd8

Browse files
authored
Merge pull request #2884 from dotty-staging/benchmarks
Remove Bench or A generic JHM benchmark interface
2 parents 9018807 + 762db5e commit b7cafd8

File tree

5 files changed

+237
-142
lines changed

5 files changed

+237
-142
lines changed

bench/src/main/scala/Benchmarks.scala

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package dotty.tools.benchmarks
2+
3+
import dotty.tools.dotc._
4+
import core.Contexts.Context
5+
6+
import org.openjdk.jmh.results.RunResult
7+
import org.openjdk.jmh.runner.Runner
8+
import org.openjdk.jmh.runner.options.OptionsBuilder
9+
import org.openjdk.jmh.annotations._
10+
import org.openjdk.jmh.results.format._
11+
import java.util.concurrent.TimeUnit
12+
13+
import java.io.{File, FileOutputStream, BufferedWriter, FileWriter}
14+
import scala.collection.JavaConversions._
15+
import scala.io.Source
16+
17+
object Bench {
18+
val COMPILE_OPTS_FILE = "compile.txt"
19+
20+
def main(args: Array[String]): Unit = {
21+
storeCompileOptions(args)
22+
23+
val libs = System.getenv("BOOTSTRAP_APPEND")
24+
25+
val opts = new OptionsBuilder()
26+
.jvmArgsPrepend(s"-Xbootclasspath/a:$libs")
27+
.mode(Mode.AverageTime)
28+
.timeUnit(TimeUnit.MICROSECONDS)
29+
.forks(5)
30+
.warmupIterations(5)
31+
.measurementIterations(10)
32+
.resultFormat(ResultFormatType.CSV)
33+
.result("result.csv")
34+
.build
35+
36+
val runner = new Runner(opts) // full access to all JMH features, you can also provide a custom output Format here
37+
runner.run() // actually run the benchmarks
38+
39+
removeCompileOptions
40+
}
41+
42+
def removeCompileOptions: Unit = new File(COMPILE_OPTS_FILE).delete()
43+
44+
def storeCompileOptions(args: Array[String]): Unit = {
45+
val file = new File(COMPILE_OPTS_FILE)
46+
val bw = new BufferedWriter(new FileWriter(file))
47+
bw.write(args.mkString("\n"))
48+
bw.close()
49+
}
50+
51+
def readCompileOptions: Seq[String] =
52+
Source.fromFile(COMPILE_OPTS_FILE).getLines.toSeq
53+
}
54+
55+
@State(Scope.Benchmark)
56+
class CompilerOptions {
57+
var opts: Array[String] = null
58+
59+
@Setup
60+
def prepare: Unit = {
61+
opts = Bench.readCompileOptions.to[Array]
62+
}
63+
}
64+
65+
class Worker extends Driver {
66+
override def newCompiler(implicit ctx: Context): Compiler = new Compiler
67+
68+
@Benchmark
69+
def compile(state: CompilerOptions): Unit = {
70+
val res = process(state.opts)
71+
if (res.hasErrors) throw new Exception("compilation failed")
72+
}
73+
}

bench/templates/launch.mustache

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/bin/sh
2+
#/*--------------------------------------------------------------------------
3+
# * Copyright 2012 Taro L. Saito
4+
# *
5+
# * Licensed under the Apache License, Version 2.0 (the "License");
6+
# * you may not use this file except in compliance with the License.
7+
# * You may obtain a copy of the License at
8+
# *
9+
# * http://www.apache.org/licenses/LICENSE-2.0
10+
# *
11+
# * Unless required by applicable law or agreed to in writing, software
12+
# * distributed under the License is distributed on an "AS IS" BASIS,
13+
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# * See the License for the specific language governing permissions and
15+
# * limitations under the License.
16+
# *--------------------------------------------------------------------------*/
17+
18+
if [ -z "$PROG_HOME" ] ; then
19+
## resolve links - $0 may be a link to PROG_HOME
20+
PRG="$0"
21+
22+
# need this for relative symlinks
23+
while [ -h "$PRG" ] ; do
24+
ls=`ls -ld "$PRG"`
25+
link=`expr "$ls" : '.*-> \(.*\)$'`
26+
if expr "$link" : '/.*' > /dev/null; then
27+
PRG="$link"
28+
else
29+
PRG="`dirname "$PRG"`/$link"
30+
fi
31+
done
32+
33+
saveddir=`pwd`
34+
35+
PROG_HOME=`dirname "$PRG"`/..
36+
37+
# make it fully qualified
38+
PROG_HOME=`cd "$PROG_HOME" && pwd`
39+
40+
cd "$saveddir"
41+
fi
42+
43+
44+
cygwin=false
45+
mingw=false
46+
darwin=false
47+
case "`uname`" in
48+
CYGWIN*) cygwin=true
49+
;;
50+
MINGW*) mingw=true
51+
;;
52+
Darwin*) darwin=true
53+
if [ -z "$JAVA_VERSION" ] ; then
54+
JAVA_VERSION="CurrentJDK"
55+
else
56+
echo "Using Java version: $JAVA_VERSION" 1>&2
57+
fi
58+
if [ -z "$JAVA_HOME" ] ; then
59+
JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
60+
fi
61+
JAVACMD="`which java`"
62+
;;
63+
esac
64+
65+
# Resolve JAVA_HOME from javac command path
66+
if [ -z "$JAVA_HOME" ]; then
67+
javaExecutable="`which javac`"
68+
if [ -n "$javaExecutable" -a -f "$javaExecutable" -a ! "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
69+
# readlink(1) is not available as standard on Solaris 10.
70+
readLink=`which readlink`
71+
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
72+
javaExecutable="`readlink -f \"$javaExecutable\"`"
73+
javaHome="`dirname \"$javaExecutable\"`"
74+
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
75+
JAVA_HOME="$javaHome"
76+
export JAVA_HOME
77+
fi
78+
fi
79+
fi
80+
81+
82+
if [ -z "$JAVACMD" ] ; then
83+
if [ -n "$JAVA_HOME" ] ; then
84+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
85+
# IBM's JDK on AIX uses strange locations for the executables
86+
JAVACMD="$JAVA_HOME/jre/sh/java"
87+
else
88+
JAVACMD="$JAVA_HOME/bin/java"
89+
fi
90+
else
91+
JAVACMD="`which java`"
92+
fi
93+
fi
94+
95+
if [ ! -x "$JAVACMD" ] ; then
96+
echo "Error: JAVA_HOME is not defined correctly."
97+
echo " We cannot execute $JAVACMD"
98+
exit 1
99+
fi
100+
101+
if [ -z "$JAVA_HOME" ] ; then
102+
echo "Warning: JAVA_HOME environment variable is not set."
103+
fi
104+
105+
CLASSPATH_SUFFIX=""
106+
# Path separator used in EXTRA_CLASSPATH
107+
PSEP=":"
108+
109+
# For Cygwin, switch paths to Windows-mixed format before running java
110+
if $cygwin; then
111+
[ -n "$PROG_HOME" ] &&
112+
PROG_HOME=`cygpath -am "$PROG_HOME"`
113+
[ -n "$JAVA_HOME" ] &&
114+
JAVA_HOME=`cygpath -am "$JAVA_HOME"`
115+
CLASSPATH_SUFFIX=";"
116+
PSEP=";"
117+
fi
118+
119+
# For Migwn, ensure paths are in UNIX format before anything is touched
120+
if $mingw ; then
121+
[ -n "$PROG_HOME" ] &&
122+
PROG_HOME="`(cd "$PROG_HOME"; pwd -W | sed 's|/|\\\\|g')`"
123+
[ -n "$JAVA_HOME" ] &&
124+
JAVA_HOME="`(cd "$JAVA_HOME"; pwd -W | sed 's|/|\\\\|g')`"
125+
CLASSPATH_SUFFIX=";"
126+
PSEP=";"
127+
fi
128+
129+
130+
PROG_NAME={{{PROG_NAME}}}
131+
PROG_VERSION={{{PROG_VERSION}}}
132+
PROG_REVISION={{{PROG_REVISION}}}
133+
134+
# add default libraries for compilation
135+
export BOOTSTRAP_APPEND="{{{EXPANDED_CLASSPATH}}}${CLASSPATH_SUFFIX}"
136+
137+
eval exec "\"$JAVACMD\"" \
138+
{{{JVM_OPTS}}} \
139+
${JAVA_OPTS} \
140+
{{^EXPANDED_CLASSPATH}}
141+
-cp "'{{{EXTRA_CLASSPATH}}}${PROG_HOME}/lib/*${CLASSPATH_SUFFIX}'" \
142+
{{/EXPANDED_CLASSPATH}}
143+
{{#EXPANDED_CLASSPATH}}
144+
-cp "'{{{EXTRA_CLASSPATH}}}{{{EXPANDED_CLASSPATH}}}${CLASSPATH_SUFFIX}'" \
145+
{{/EXPANDED_CLASSPATH}}
146+
{{{MAIN_CLASS}}} \"\$@\"
147+
exit $?

bench/test/dotty/tools/benchmarks/Benchmarks.scala

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

project/Build.scala

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import dotty.tools.sbtplugin.DottyIDEPlugin.autoImport._
2020
import org.scalajs.sbtplugin.ScalaJSPlugin
2121
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
2222

23+
import pl.project13.scala.sbt.JmhPlugin
24+
import JmhPlugin.JmhKeys.Jmh
25+
2326
/* In sbt 0.13 the Build trait would expose all vals to the shell, where you
2427
* can use them in "set a := b" like expressions. This re-exposes them.
2528
*/
@@ -822,42 +825,19 @@ object Build {
822825
)))
823826

824827
lazy val `dotty-bench` = project.in(file("bench")).
825-
dependsOn(`dotty-compiler` % "compile->test").
828+
dependsOn(`dotty-compiler`).
826829
settings(commonNonBootstrappedSettings).
827830
settings(
828-
baseDirectory in (Test,run) := (baseDirectory in `dotty-compiler`).value,
829-
830-
libraryDependencies += "com.storm-enroute" %% "scalameter" % "0.6" % Test,
831-
832-
fork in Test := true,
833-
parallelExecution in Test := false,
834-
835-
// http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/sbt-setting-jvm-boot-paramaters-for-scala
836-
javaOptions ++= {
837-
val attList = (dependencyClasspath in Runtime).value
838-
val bin = (packageBin in Compile).value
839-
840-
// put the Scala {library, reflect, compiler} in the classpath
841-
val path = for {
842-
file <- attList.map(_.data)
843-
path = file.getAbsolutePath
844-
prefix = if (path.endsWith(".jar")) "p" else "a"
845-
} yield "-Xbootclasspath/" + prefix + ":" + path
846-
// dotty itself needs to be in the bootclasspath
847-
val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList
848-
// System.err.println("BOOTPATH: " + fullpath)
849-
850-
val ci_build = // propagate if this is a ci build
851-
if (sys.props.isDefinedAt(JENKINS_BUILD))
852-
List(s"-D$JENKINS_BUILD=${sys.props(JENKINS_BUILD)}")
853-
else if (sys.props.isDefinedAt(DRONE_MEM))
854-
List("-Xmx" + sys.props(DRONE_MEM))
855-
else
856-
List()
857-
val res = agentOptions ::: ci_build ::: fullpath
858-
println("Running with javaOptions: " + res)
859-
res
860-
}
831+
mainClass in (Jmh, run) := Some("dotty.tools.benchmarks.Bench") // custom main for jmh:run
832+
).
833+
enablePlugins(JmhPlugin).
834+
settings(packSettings).
835+
settings(
836+
publishArtifact := false,
837+
packMain := Map("bench" -> "dotty.tools.benchmarks.Bench"),
838+
packGenerateWindowsBatFile := false,
839+
packExpandedClasspath := true,
840+
packBashTemplate := baseDirectory.value + "/templates/launch.mustache"
861841
)
862842

863843
// Depend on dotty-library so that sbt projects using dotty automatically

0 commit comments

Comments
 (0)