3939import java .util .function .DoubleSupplier ;
4040
4141/**
42- * Various benchmarks for the distance functions
43- * used by indexed and non-indexed vectors.
44- * Parameters include element, dims, function, and type.
42+ * Various benchmarks for the distance functions used by indexed and non-indexed vectors.
43+ * Parameters include doc and query type, dims, function, and implementation.
4544 * For individual local tests it may be useful to increase
46- * fork, measurement, and operations per invocation. (Note
47- * to also update the benchmark loop if operations per invocation
48- * is increased.)
45+ * fork, measurement, and operations per invocation.
4946 */
5047@ Fork (1 )
5148@ Warmup (iterations = 1 )
5249@ Measurement (iterations = 2 )
5350@ BenchmarkMode (Mode .AverageTime )
5451@ OutputTimeUnit (TimeUnit .NANOSECONDS )
55- @ OperationsPerInvocation (25000 )
52+ @ OperationsPerInvocation (DistanceFunctionBenchmark . OPERATIONS )
5653@ State (Scope .Benchmark )
5754public class DistanceFunctionBenchmark {
5855
56+ public static final int OPERATIONS = 25000 ;
57+
5958 static {
6059 LogConfigurator .configureESLogging ();
6160 }
@@ -125,7 +124,7 @@ private static BytesRef generateVectorData(float[] vector) {
125124 }
126125
127126 private static BytesRef generateVectorData (float [] vector , float mag ) {
128- ByteBuffer buffer = ByteBuffer .allocate (vector .length * 4 + 4 );
127+ ByteBuffer buffer = ByteBuffer .allocate (vector .length * Float . BYTES + Float . BYTES );
129128 for (float f : vector ) {
130129 buffer .putFloat (f );
131130 }
@@ -136,14 +135,15 @@ private static BytesRef generateVectorData(float[] vector, float mag) {
136135 private static BytesRef generateVectorData (byte [] vector ) {
137136 float mag = calculateMag (vector );
138137
139- ByteBuffer buffer = ByteBuffer .allocate (vector .length + 4 );
138+ ByteBuffer buffer = ByteBuffer .allocate (vector .length + Float . BYTES );
140139 buffer .put (vector );
141140 buffer .putFloat (mag );
142141 return new BytesRef (buffer .array ());
143142 }
144143
145144 @ Setup
146145 public void findBenchmarkImpl () {
146+ if (dims % 8 != 0 ) throw new IllegalArgumentException ("Dims must be a multiple of 8" );
147147 Random r = new Random ();
148148
149149 float [] floatDocVector = new float [dims ];
@@ -225,15 +225,15 @@ public void findBenchmarkImpl() {
225225 @ Fork (1 )
226226 @ Benchmark
227227 public void benchmark (Blackhole blackhole ) {
228- for (int i = 0 ; i < 25000 ; ++i ) {
228+ for (int i = 0 ; i < OPERATIONS ; ++i ) {
229229 blackhole .consume (benchmarkImpl .getAsDouble ());
230230 }
231231 }
232232
233233 @ Fork (value = 1 , jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" })
234234 @ Benchmark
235235 public void vectorBenchmark (Blackhole blackhole ) {
236- for (int i = 0 ; i < 25000 ; ++i ) {
236+ for (int i = 0 ; i < OPERATIONS ; ++i ) {
237237 blackhole .consume (benchmarkImpl .getAsDouble ());
238238 }
239239 }
0 commit comments