Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JMH benchmarks for Lucene IntIntHashMap and Eclipse IntShortHashMap #599

Merged
merged 11 commits into from
Nov 26, 2023
2 changes: 1 addition & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ tasks:
cmds:
- sbt -client elastiknn-api4s/test
- sbt -client elastiknn-client-elastic4s/test
- sbt -client elastiknn-jmh-benchmarks/test
- sbt -client elastiknn-lucene/test
- sbt -client elastiknn-models/test
- sbt -client elastiknn-models-benchmarks/test
- sbt -client elastiknn-plugin/test

jvmUnitTestQuick:
Expand Down
14 changes: 9 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ lazy val `elastiknn-root` = project
`elastiknn-client-elastic4s`,
`elastiknn-lucene`,
`elastiknn-models`,
`elastiknn-models-benchmarks`,
`elastiknn-jmh-benchmarks`,
`elastiknn-plugin`,
`elastiknn-plugin-integration-tests`
)
Expand Down Expand Up @@ -101,13 +101,17 @@ lazy val `elastiknn-models` = project
TestSettings
)

lazy val `elastiknn-models-benchmarks` = project
.in(file("elastiknn-models-benchmarks"))
.dependsOn(`elastiknn-models`, `elastiknn-api4s`)
lazy val `elastiknn-jmh-benchmarks` = project
.in(file("elastiknn-jmh-benchmarks"))
.dependsOn(`elastiknn-models`, `elastiknn-api4s`, `elastiknn-lucene`)
.enablePlugins(JmhPlugin)
.settings(
Jmh / javaOptions ++= Seq("--add-modules", "jdk.incubator.vector"),
TpolecatSettings
TpolecatSettings,
libraryDependencies ++= Seq(
"org.eclipse.collections" % "eclipse-collections" % "11.1.0",
"org.eclipse.collections" % "eclipse-collections-api" % "11.1.0"
)
)

lazy val `elastiknn-plugin` = project
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.klibisz.elastiknn.jmhbenchmarks

import org.openjdk.jmh.annotations._
import org.apache.lucene.util.hppc.IntIntHashMap
import org.eclipse.collections.impl.map.mutable.primitive.IntShortHashMap

import scala.util.Random

@State(Scope.Benchmark)
class HitCounterBenchmarksFixtures {
val rng = new Random(0)
val numDocs = 60000
val numHits = 2000
val initialMapSize = 1000
val docs = (1 to numHits).map(_ => rng.nextInt(numDocs)).toArray
}

class HitCounterBenchmarks {

@Benchmark
@BenchmarkMode(Array(Mode.Throughput))
@Fork(value = 1)
@Warmup(time = 5, iterations = 5)
@Measurement(time = 5, iterations = 5)
def arrayCountBaseline(f: HitCounterBenchmarksFixtures): Unit = {
val arr = new Array[Int](f.numDocs)
for (d <- f.docs) arr.update(d, arr(d) + 1)
()
}

@Benchmark
@BenchmarkMode(Array(Mode.Throughput))
@Fork(value = 1)
@Warmup(time = 5, iterations = 5)
@Measurement(time = 5, iterations = 5)
def hashMapGetOrDefault(f: HitCounterBenchmarksFixtures): Unit = {
val h = new java.util.HashMap[Int, Int](f.initialMapSize, 0.99f)
for (d <- f.docs) h.put(d, h.getOrDefault(d, 0) + 1)
()
}

@Benchmark
@BenchmarkMode(Array(Mode.Throughput))
@Fork(value = 1)
@Warmup(time = 5, iterations = 5)
@Measurement(time = 5, iterations = 5)
def luceneIntIntHashMap(f: HitCounterBenchmarksFixtures): Unit = {
val m = new IntIntHashMap(f.initialMapSize, 0.99d)
for (d <- f.docs) m.putOrAdd(d, 1, 1)
()
}

@Benchmark
@BenchmarkMode(Array(Mode.Throughput))
@Fork(value = 1)
@Warmup(time = 5, iterations = 5)
@Measurement(time = 5, iterations = 5)
def eclipseIntShortHashMapAddToValue(f: HitCounterBenchmarksFixtures): Unit = {
val m = new IntShortHashMap(f.initialMapSize)
for (d <- f.docs) m.addToValue(d, 1)
()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.klibisz.elastiknn.vectors
package com.klibisz.elastiknn.jmhbenchmarks

import com.klibisz.elastiknn.api.Vec
import com.klibisz.elastiknn.vectors._
import org.openjdk.jmh.annotations._

import scala.util.Random
Expand Down