Skip to content

Commit

Permalink
Configure JMH benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
susuro committed Jan 28, 2024
1 parent 1fb074c commit 7412e95
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.marek.tyre.benchmark

import org.openjdk.jmh.annotations.{Benchmark, Scope, State}
import net.marek.tyre.*
import scala.util.Random

@State(Scope.Thread)
class RegexBenchmark:

@Benchmark
def runTyre: Unit = data.foreach(tyreTimeParser.run)

@Benchmark
def runRegex: Unit = data.foreach:
case regexTimeParser(h, m) => (h.toInt, m.toInt)

private val tyreTimeParser =
val ht = tyre"[0-1]\d|2[0-3]"
val mt = tyre"[0-5]\d"
val tt = tyre"$ht:$mt".map: t =>
val ((h10,h1), _, m10, m1) = t
(h10.toInt * 10 + h1.toInt, m10.toInt * 10 + m1.toInt)
tt.compile()

private val regexTimeParser = """([0-1]\d|2[0-3]):([0-5]\d)""".r

private val rand = Random()
private def drawTime =
val h = rand.between(0,24)
val m = rand.between(0,60)
f"$h%02d:$m%02d"
private val data = List.tabulate(100_000)(_ => drawTime)
8 changes: 8 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ lazy val root = (project in file("."))
)
.settings(publishSettings: _*)

lazy val benchmark = (project in file("benchmark"))
.settings(basicSettings: _*)
.settings(
name := "tyre-benchmark"
)
.enablePlugins(JmhPlugin)
.dependsOn(root)

lazy val scalacSettings = Seq(
"-deprecation", // Emit warning and location for usages of deprecated APIs.
"-explain", // Explain errors in more detail.
Expand Down
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7")

0 comments on commit 7412e95

Please sign in to comment.