-
-
Notifications
You must be signed in to change notification settings - Fork 287
Support JMH for scala 2.12 #295 #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,15 +4,15 @@ import java.net.URLClassLoader | |
|
|
||
| import scala.annotation.tailrec | ||
| import scala.collection.JavaConverters._ | ||
|
|
||
| import org.openjdk.jmh.generators.core.{ BenchmarkGenerator => JMHGenerator, FileSystemDestination } | ||
| import org.openjdk.jmh.generators.core.{FileSystemDestination, GeneratorSource, BenchmarkGenerator => JMHGenerator} | ||
| import org.openjdk.jmh.generators.asm.ASMGeneratorSource | ||
| import org.openjdk.jmh.runner.{ Runner, RunnerException } | ||
| import org.openjdk.jmh.runner.options.{ Options, OptionsBuilder } | ||
|
|
||
| import org.openjdk.jmh.generators.reflection.RFGeneratorSource | ||
| import org.openjdk.jmh.runner.{Runner, RunnerException} | ||
| import org.openjdk.jmh.runner.options.{Options, OptionsBuilder} | ||
| import java.net.URI | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import java.nio.file.{Files, FileSystems, Path} | ||
| import java.nio.file.{FileSystems, Files, Path, Paths} | ||
|
|
||
| import io.bazel.rulesscala.jar.JarCreator | ||
|
|
||
|
|
@@ -27,7 +27,14 @@ import io.bazel.rulesscala.jar.JarCreator | |
| */ | ||
| object BenchmarkGenerator { | ||
|
|
||
| case class BenchmarkGeneratorArgs( | ||
| private sealed trait GeneratorType | ||
|
|
||
| private case object ReflectionGenerator extends GeneratorType | ||
|
|
||
| private case object AsmGenerator extends GeneratorType | ||
|
|
||
| private case class BenchmarkGeneratorArgs( | ||
| generatorType: GeneratorType, | ||
| inputJar: Path, | ||
| resultSourceJar: Path, | ||
| resultResourceJar: Path, | ||
|
|
@@ -37,6 +44,7 @@ object BenchmarkGenerator { | |
| def main(argv: Array[String]): Unit = { | ||
| val args = parseArgs(argv) | ||
| generateJmhBenchmark( | ||
| args.generatorType, | ||
| args.resultSourceJar, | ||
| args.resultResourceJar, | ||
| args.inputJar, | ||
|
|
@@ -47,17 +55,18 @@ object BenchmarkGenerator { | |
| private def parseArgs(argv: Array[String]): BenchmarkGeneratorArgs = { | ||
| if (argv.length < 3) { | ||
| System.err.println( | ||
| "Usage: BenchmarkGenerator INPUT_JAR RESULT_JAR RESOURCE_JAR [CLASSPATH_ELEMENT] [CLASSPATH_ELEMENT...]" | ||
| "Usage: BenchmarkGenerator GENERATOR_TYPE INPUT_JAR RESULT_JAR RESOURCE_JAR [CLASSPATH_ELEMENT] [CLASSPATH_ELEMENT...]" | ||
| ) | ||
| System.exit(1) | ||
| } | ||
| val fs = FileSystems.getDefault | ||
|
|
||
| BenchmarkGeneratorArgs( | ||
| fs.getPath(argv(0)), | ||
| if ("asm".equalsIgnoreCase(argv(0))) AsmGenerator else ReflectionGenerator, | ||
| fs.getPath(argv(1)), | ||
| fs.getPath(argv(2)), | ||
| argv.slice(3, argv.length).map { s => fs.getPath(s) }.toList | ||
| fs.getPath(argv(3)), | ||
| argv.slice(4, argv.length).map { s => fs.getPath(s) }.toList | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -88,13 +97,13 @@ object BenchmarkGenerator { | |
| } | ||
|
|
||
| // Courtesy of Doug Tangren (https://groups.google.com/forum/#!topic/simple-build-tool/CYeLHcJjHyA) | ||
| private def withClassLoader[A](cp: Seq[Path])(f: => A): A = { | ||
| private def withClassLoader[A](cp: Seq[Path])(f: ClassLoader => A): A = { | ||
| val originalLoader = Thread.currentThread.getContextClassLoader | ||
| val jmhLoader = classOf[JMHGenerator].getClassLoader | ||
| val classLoader = new URLClassLoader(cp.map(_.toUri.toURL).toArray, jmhLoader) | ||
| try { | ||
| Thread.currentThread.setContextClassLoader(classLoader) | ||
| f | ||
| f(classLoader) | ||
| } finally { | ||
| Thread.currentThread.setContextClassLoader(originalLoader) | ||
| } | ||
|
|
@@ -119,6 +128,7 @@ object BenchmarkGenerator { | |
| } | ||
|
|
||
| private def generateJmhBenchmark( | ||
| generatorType: GeneratorType, | ||
| sourceJarOut: Path, | ||
| resourceJarOut: Path, | ||
| benchmarkJarPath: Path, | ||
|
|
@@ -131,17 +141,33 @@ object BenchmarkGenerator { | |
| tmpResourceDir.toFile.mkdir() | ||
| tmpSourceDir.toFile.mkdir() | ||
|
|
||
| withClassLoader(benchmarkJarPath :: classpath) { | ||
| val source = new ASMGeneratorSource | ||
| val destination = new FileSystemDestination(tmpResourceDir.toFile, tmpSourceDir.toFile) | ||
| val generator = new JMHGenerator | ||
|
|
||
| collectClassesFromJar(benchmarkJarPath).foreach { path => | ||
| // this would fail due to https://github.com/bazelbuild/rules_scala/issues/295 | ||
| // let's throw a useful message instead | ||
| sys.error("jmh in rules_scala doesn't work with Java 8 bytecode: https://github.com/bazelbuild/rules_scala/issues/295") | ||
| source.processClass(Files.newInputStream(path)) | ||
| withClassLoader(benchmarkJarPath :: classpath) { isolatedClassLoader => | ||
|
|
||
| val source: GeneratorSource = generatorType match { | ||
| case AsmGenerator => | ||
| val generatorSource = new ASMGeneratorSource | ||
| try { | ||
| generatorSource.processClasses(collectClassesFromJar(benchmarkJarPath).map(_.toFile).asJavaCollection) | ||
| } catch { | ||
| case _: ArrayIndexOutOfBoundsException => | ||
| // this would fail due to https://github.com/bazelbuild/rules_scala/issues/295 | ||
| // let's throw a useful message instead | ||
| sys.error("jmh in rules_scala doesn't work with Java 8 bytecode: https://github.com/bazelbuild/rules_scala/issues/295") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we change the error to say asm mode does not work with java 8 bytecode and instead suggest using reflection? |
||
| } | ||
| generatorSource | ||
|
|
||
| case ReflectionGenerator => | ||
| val generatorSource = new RFGeneratorSource | ||
| generatorSource.processClasses( | ||
| collectClassesFromJar(benchmarkJarPath) | ||
| .flatMap(classByPath(_, isolatedClassLoader)) | ||
| .asJavaCollection | ||
| ) | ||
| generatorSource | ||
| } | ||
|
|
||
| val generator = new JMHGenerator | ||
| val destination = new FileSystemDestination(tmpResourceDir.toFile, tmpSourceDir.toFile) | ||
| generator.generate(source, destination) | ||
| generator.complete(source, destination) | ||
| if (destination.hasErrors) { | ||
|
|
@@ -156,6 +182,29 @@ object BenchmarkGenerator { | |
| } | ||
| } | ||
|
|
||
| private def classByPath(path: Path, cl: ClassLoader): Option[Class[_]] = { | ||
| val separator = path.getFileSystem.getSeparator | ||
| var s = path.toString | ||
| .stripPrefix(separator) | ||
| .stripSuffix(".class") | ||
| .replace(separator, ".") | ||
|
|
||
| var index = -1 | ||
| do { | ||
| s = s.substring(index + 1) | ||
| try { | ||
| return Some(Class.forName(s, false, cl)) | ||
| } catch { | ||
| case _: ClassNotFoundException => | ||
| // ignore and try next one | ||
| index = s.indexOf('.') | ||
| } | ||
| } while (index != -1) | ||
|
|
||
| log(s"Failed to find class for path $path") | ||
| None | ||
| } | ||
|
|
||
| private def log(str: String): Unit = { | ||
| System.err.println(s"JMH benchmark generation: $str") | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we error if it is not asm or reflection?