-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic support for profiling JMH benchmarks (#187)
* Add poc of JMH support * update readme * update workflow
- Loading branch information
1 parent
ad1a7a2
commit 24374f5
Showing
13 changed files
with
369 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
object Dependencies { | ||
val collectionCompatVersion = "2.8.1" | ||
val jmhVersion = "1.36" | ||
val organizeImportsVersion = "0.6.0" | ||
val silencerVersion = "1.7.12" | ||
|
||
val zioVersion = "2.0.10" | ||
val zioVersion = "2.0.10" | ||
} |
1 change: 1 addition & 0 deletions
1
zio-profiling-jmh/src/main/resources/META-INF/services/org.openjdk.jmh.profile.Profiler
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
zio.profiling.jmh.JmhZioProfiler |
22 changes: 22 additions & 0 deletions
22
zio-profiling-jmh/src/main/scala/zio/profiling/jmh/BenchmarkUtils.scala
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package zio.profiling.jmh | ||
|
||
import zio._ | ||
import zio.profiling.sampling.SamplingProfilerSupervisor | ||
|
||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
object BenchmarkUtils { | ||
|
||
private[jmh] val runtimeRef: AtomicReference[Runtime.Scoped[SamplingProfilerSupervisor]] = new AtomicReference() | ||
|
||
def getRuntime(): Runtime[Any] = { | ||
val customRt = runtimeRef.get() | ||
if (customRt ne null) customRt else Runtime.default | ||
} | ||
|
||
def unsafeRun[E, A](zio: ZIO[Any, E, A]): A = | ||
Unsafe.unsafe { implicit unsafe => | ||
getRuntime().unsafe.run(zio).getOrThrowFiberFailure() | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
zio-profiling-jmh/src/main/scala/zio/profiling/jmh/JmhZioProfiler.scala
This file contains 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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package zio.profiling.jmh | ||
|
||
import org.openjdk.jmh.infra.{BenchmarkParams, IterationParams} | ||
import org.openjdk.jmh.profile.InternalProfiler | ||
import org.openjdk.jmh.results.{IterationResult, Result, TextResult} | ||
import org.openjdk.jmh.runner.IterationType | ||
import zio._ | ||
import zio.profiling.sampling.SamplingProfiler | ||
|
||
import java.io.{File, PrintWriter, StringWriter} | ||
import java.lang.System | ||
import java.nio.file.Files | ||
import java.util.Collections | ||
import java.{util => ju} | ||
import scala.annotation.unused | ||
|
||
class JmhZioProfiler(@unused initLine: String) extends InternalProfiler { | ||
private val outDir: File = new File(System.getProperty("user.dir")) | ||
private var trialOutDir: Option[File] = None | ||
private var warmupStarted: Boolean = false | ||
private var measurementStarted: Boolean = false | ||
private var measurementIterationCount: Int = 0 | ||
|
||
def this() = this("") | ||
|
||
def getDescription(): String = | ||
"zio-profiling profiler provider." | ||
|
||
def beforeIteration(benchmarkParams: BenchmarkParams, iterationParams: IterationParams): Unit = { | ||
if (trialOutDir.isEmpty) { | ||
createTrialOutDir(benchmarkParams); | ||
} | ||
|
||
if (iterationParams.getType() == IterationType.WARMUP) { | ||
if (!warmupStarted) { | ||
start() | ||
warmupStarted = true | ||
} | ||
} | ||
|
||
if (iterationParams.getType() == IterationType.MEASUREMENT) { | ||
if (!measurementStarted) { | ||
if (warmupStarted) { | ||
reset() | ||
} | ||
measurementStarted = true | ||
} | ||
} | ||
} | ||
|
||
def afterIteration( | ||
benchmarkParams: BenchmarkParams, | ||
iterationParams: IterationParams, | ||
result: IterationResult | ||
): ju.Collection[_ <: Result[_]] = { | ||
if (iterationParams.getType() == IterationType.MEASUREMENT) { | ||
measurementIterationCount += 1 | ||
if (measurementIterationCount == iterationParams.getCount()) { | ||
Collections.singletonList(stopAndDump()); | ||
} | ||
} | ||
|
||
Collections.emptyList(); | ||
} | ||
|
||
private def createTrialOutDir(benchmarkParams: BenchmarkParams): Unit = { | ||
val fileName = benchmarkParams.id().replace("%", "_") | ||
trialOutDir = Some(new File(outDir, fileName)) | ||
trialOutDir.foreach(_.mkdirs()) | ||
} | ||
|
||
private def start(): Unit = Unsafe.unsafe { implicit unsafe => | ||
BenchmarkUtils.runtimeRef.set(SamplingProfiler().supervisedRuntime) | ||
} | ||
|
||
private def stopAndDump(): TextResult = { | ||
val sw = new StringWriter() | ||
val pw = new PrintWriter(sw) | ||
val rt = BenchmarkUtils.runtimeRef.getAndSet(null) | ||
|
||
if (rt ne null) { | ||
val supervisor = rt.environment.get | ||
val resultPath = writeFile("profile.folded", supervisor.unsafeValue().stackCollapse.mkString("\n")) | ||
Unsafe.unsafe(implicit u => rt.unsafe.shutdown()) | ||
|
||
pw.println("zio-profiler results:") | ||
pw.println(s" ${resultPath}") | ||
} | ||
|
||
pw.flush(); | ||
pw.close(); | ||
new TextResult(sw.toString(), "zio-profiling") | ||
} | ||
|
||
private def reset(): Unit = Unsafe.unsafe { implicit unsafe => | ||
val runtime = BenchmarkUtils.runtimeRef.get() | ||
if (runtime ne null) { | ||
runtime.environment.get.reset() | ||
} | ||
} | ||
|
||
private def writeFile(name: String, content: String) = { | ||
val out = new File(trialOutDir.get, name) | ||
Files.write(out.toPath(), content.getBytes()) | ||
} | ||
|
||
} |
This file contains 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
Oops, something went wrong.