Skip to content

Instantly share code, notes, and snippets.

@l15k4
Created December 21, 2016 17:49
Show Gist options
  • Save l15k4/2af77341a4e20148626c890145bef2d7 to your computer and use it in GitHub Desktop.
Save l15k4/2af77341a4e20148626c890145bef2d7 to your computer and use it in GitHub Desktop.
package example
import cats.implicits._
import com.google.common.hash.Hashing
import com.rklaehn.abc._
import com.rklaehn.sonicreducer.Reducer
import org.scalameter.Bench.{HTMLReport, OfflineReport}
import org.scalameter.{Bench, KeyValue}
import org.scalameter.api._
object BenchGroup extends Bench.Group {
performance of "memory footprint" config(reports.resultDir -> "target/benchmarks/memory") in {
include(new CollMemoryBench {})
}
performance of "running time" config(reports.resultDir -> "target/benchmarks/time") in {
include(new CollPerfBench {})
}
}
trait CollMemoryBench extends OfflineReport with CollBench {
override def measurer = new Executor.Measurer.MemoryFootprint
}
trait CollPerfBench extends OfflineReport with CollBench {
override def measurer = new Executor.Measurer.Default
performance of "get" in {
performance of "OpenHashMap" in {
using(gen.map(buildOpenMap))
.config(execs: _*)
.in { hashMap =>
val size = hashMap.size
hashKeyValueStream.take(size).foreach { case (k,v) =>
assert(hashMap(k) == v)
}
}
}
performance of "ArrayMap" in {
using(gen.map(buildArrayMap))
.config(execs: _*)
.in { hashMap =>
val size = hashMap.size
hashKeyValueStream.take(size).foreach { case (k,v) =>
assert(hashMap.get(k).contains(v))
}
}
}
}
}
trait CollBench extends HTMLReport {
def hashKeyValueStream = Iterator.from(0).map { index =>
(Hashing.murmur3_32().hashInt(index).asInt(), index)
}
def buildOpenMap(size: Int): PrimitiveKeyOpenHashMap[Int, Int] =
hashKeyValueStream.take(size).foldLeft(new PrimitiveKeyOpenHashMap[Int, Int](size)) { case (acc, (k,v)) =>
acc.update(k,v)
acc
}
def buildArrayMap(size: Int) = {
val reducer = Reducer[ArrayMap[Int, Int]](_ merge _)
for((k,v) <- hashKeyValueStream.take(size))
reducer(ArrayMap.singleton(k, v))
reducer.result
}
val gen = Gen.range("size")(50000, 150000, 50000)
val execs = Seq[KeyValue](
exec.benchRuns -> 2,
exec.maxWarmupRuns -> 1,
exec.independentSamples -> 1,
exec.requireGC -> true,
exec.jvmflags -> List("-server", "-Xms2048m", "-Xmx8048m", "-XX:+UseG1GC")
)
performance of "build" in {
performance of "OpenHashMap" in {
using(gen)
.config(execs: _*)
.in { size =>
buildOpenMap(size)
}
}
performance of "ArrayMap" in {
using(gen)
.config(execs: _*)
.in { size =>
buildArrayMap(size)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment