Permalink
Browse files

Add benchmark for Rerunnable

  • Loading branch information...
travisbrown committed Mar 13, 2016
1 parent c0ce894 commit d7735b1c4f106d9bfb8412e74fd3201e5d6d4231
@@ -0,0 +1,60 @@
package io.catbird.benchmark
import com.twitter.util.{ Await, Future, FuturePool }
import io.catbird.util.Rerunnable
import java.util.concurrent.{ ExecutorService, Executors, TimeUnit }
import org.openjdk.jmh.annotations._
/**
* Compare the performance of `Rerunnable` against ordinary `Future`s.
*
* The following command will run the benchmarks with reasonable settings:
*
* > sbt "benchmark/jmh:run -i 20 -wi 10 -f 2 -t 1 io.catbird.benchmark.RerunnableBenchmark"
*/
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class RerunnableBenchmark {
val count: Int = 100000
val numbers: IndexedSeq[Int] = 0 to count
var es: ExecutorService = _
var pool: FuturePool = _
@Setup
def initPool(): Unit = {
es = Executors.newFixedThreadPool(4)
pool = FuturePool(es)
}
@TearDown
def shutdownPool(): Unit = es.shutdown()
@Benchmark
def sumIntsF: Int = Await.result(
numbers.foldLeft(Future(0)) {
case (acc, i) => acc.flatMap(prev => Future(prev + i))
}
)
@Benchmark
def sumIntsR: Int = Await.result(
numbers.foldLeft(Rerunnable(0)) {
case (acc, i) => acc.flatMap(prev => Rerunnable(prev + i))
}.run
)
@Benchmark
def sumIntsPF: Int = Await.result(
numbers.foldLeft(pool(0)) {
case (acc, i) => acc.flatMap(prev => pool(prev + i))
}
)
@Benchmark
def sumIntsPR: Int = Await.result(
numbers.foldLeft(Rerunnable.withFuturePool(pool)(0)) {
case (acc, i) => acc.flatMap(prev => Rerunnable.withFuturePool(pool)(prev + i))
}.run
)
}
@@ -0,0 +1,27 @@
package io.catbird.benchmark
import org.scalatest.{ BeforeAndAfter, FlatSpec }
class RerunnableBenchmarkSpec extends FlatSpec with BeforeAndAfter {
val benchmark: RerunnableBenchmark = new RerunnableBenchmark
val sum = benchmark.numbers.sum
before { benchmark.initPool() }
after { benchmark.shutdownPool() }
"The benchmark" should "correctly calculate the sum using futures" in {
assert(benchmark.sumIntsF === sum)
}
it should "correctly calculate the sum using futures and future pools" in {
assert(benchmark.sumIntsPF === sum)
}
it should "correctly calculate the sum using rerunnables" in {
assert(benchmark.sumIntsR === sum)
}
it should "correctly calculate the sum using rerunnables and future pools" in {
assert(benchmark.sumIntsPR === sum)
}
}
@@ -67,7 +67,7 @@ lazy val root = project.in(file("."))
""".stripMargin
)
.settings(scalacOptions in (Compile, console) := compilerOptions)
.aggregate(util, finagle, bijections, tests)
.aggregate(util, finagle, bijections, tests, benchmark)
.dependsOn(util, finagle, bijections)
lazy val tests = project
@@ -113,6 +113,16 @@ lazy val bijections = project
)
.dependsOn(util)
lazy val benchmark = project
.settings(moduleName := "catbird-benchmark")
.settings(allSettings)
.settings(noPublishSettings)
.settings(
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0-M9"
)
.enablePlugins(JmhPlugin)
.dependsOn(util)
lazy val publishSettings = Seq(
releaseCrossBuild := true,
releasePublishArtifactsAction := PgpKeys.publishSigned.value,
@@ -12,3 +12,4 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5")
addSbtPlugin("org.brianmckenna" % "sbt-wartremover" % "0.14")
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.8.0")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.3.5")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.6")

0 comments on commit d7735b1

Please sign in to comment.