From dfeafa143dc457b98d4483e470d3f886edabb79d Mon Sep 17 00:00:00 2001 From: Sam Ritchie Date: Thu, 13 Jan 2022 14:43:53 -0700 Subject: [PATCH] add benchmark --- .../algebird/benchmark/MomentsBenchmark.scala | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 algebird-benchmark/src/main/scala/com/twitter/algebird/benchmark/MomentsBenchmark.scala diff --git a/algebird-benchmark/src/main/scala/com/twitter/algebird/benchmark/MomentsBenchmark.scala b/algebird-benchmark/src/main/scala/com/twitter/algebird/benchmark/MomentsBenchmark.scala new file mode 100644 index 000000000..af83e3591 --- /dev/null +++ b/algebird-benchmark/src/main/scala/com/twitter/algebird/benchmark/MomentsBenchmark.scala @@ -0,0 +1,50 @@ +package com.twitter.algebird +package benchmark + +import scala.util.Random + +import org.openjdk.jmh.annotations._ +import org.openjdk.jmh.infra.Blackhole + +object MomentsBenchmark { + @State(Scope.Benchmark) + class MomentsState { + @Param(Array("10000")) + var numElements: Int = 0 + + var inputData: Seq[Double] = _ + var inputMoments: Seq[Moments] = _ + + @Setup(Level.Trial) + def setup(): Unit = { + inputData = Seq.fill(numElements)(Random.nextInt(1000).toLong) + inputMoments = inputData.map(Moments(_)) + } + } +} + +class MomentsBenchmark { + import MomentsBenchmark._ + + @Benchmark + def timePlusDoubles(state: MomentsState, bh: Blackhole): Unit = + bh.consume( + state.inputData.foldLeft(Moments.momentsMonoid.zero)(_ + _) + ) + + @Benchmark + def timePlusMoments(state: MomentsState, bh: Blackhole): Unit = + bh.consume(state.inputMoments.reduce(_ + _)) + + @Benchmark + def timeSumOption(state: MomentsState, bh: Blackhole): Unit = + bh.consume(Moments.momentsMonoid.sumOption(state.inputMoments)) + + @Benchmark + def timeFold(state: MomentsState, bh: Blackhole): Unit = + bh.consume(Moments.fold.overTraversable(state.inputData)) + + @Benchmark + def timeAggregate(state: MomentsState, bh: Blackhole): Unit = + bh.consume(Moments.aggregator(state.inputData)) +}