diff --git a/Taskfile.yaml b/Taskfile.yaml index 80eb4b017..a30d8b87c 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -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: diff --git a/build.sbt b/build.sbt index 306a376c6..000ea41ec 100644 --- a/build.sbt +++ b/build.sbt @@ -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` ) @@ -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 diff --git a/elastiknn-jmh-benchmarks/src/main/scala/com/klibisz/elastiknn/jmhbenchmarks/HitCounterBenchmarks.scala b/elastiknn-jmh-benchmarks/src/main/scala/com/klibisz/elastiknn/jmhbenchmarks/HitCounterBenchmarks.scala new file mode 100644 index 000000000..4bd2d3367 --- /dev/null +++ b/elastiknn-jmh-benchmarks/src/main/scala/com/klibisz/elastiknn/jmhbenchmarks/HitCounterBenchmarks.scala @@ -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) + () + } +} diff --git a/elastiknn-models-benchmarks/src/main/scala/com/klibisz/elastiknn/vectors/VectorOpsJmhBenchmark.scala b/elastiknn-jmh-benchmarks/src/main/scala/com/klibisz/elastiknn/jmhbenchmarks/VectorOpsJmhBenchmark.scala similarity index 96% rename from elastiknn-models-benchmarks/src/main/scala/com/klibisz/elastiknn/vectors/VectorOpsJmhBenchmark.scala rename to elastiknn-jmh-benchmarks/src/main/scala/com/klibisz/elastiknn/jmhbenchmarks/VectorOpsJmhBenchmark.scala index 9c153ac1b..acb19b79a 100644 --- a/elastiknn-models-benchmarks/src/main/scala/com/klibisz/elastiknn/vectors/VectorOpsJmhBenchmark.scala +++ b/elastiknn-jmh-benchmarks/src/main/scala/com/klibisz/elastiknn/jmhbenchmarks/VectorOpsJmhBenchmark.scala @@ -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