|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | +package org.elasticsearch.benchmark.vector; |
| 10 | + |
| 11 | +import org.elasticsearch.common.logging.LogConfigurator; |
| 12 | +import org.elasticsearch.index.codec.vectors.BQVectorUtils; |
| 13 | +import org.openjdk.jmh.annotations.Benchmark; |
| 14 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 15 | +import org.openjdk.jmh.annotations.Fork; |
| 16 | +import org.openjdk.jmh.annotations.Measurement; |
| 17 | +import org.openjdk.jmh.annotations.Mode; |
| 18 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 19 | +import org.openjdk.jmh.annotations.Param; |
| 20 | +import org.openjdk.jmh.annotations.Scope; |
| 21 | +import org.openjdk.jmh.annotations.Setup; |
| 22 | +import org.openjdk.jmh.annotations.State; |
| 23 | +import org.openjdk.jmh.annotations.Warmup; |
| 24 | +import org.openjdk.jmh.infra.Blackhole; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Random; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +@BenchmarkMode(Mode.Throughput) |
| 31 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 32 | +@State(Scope.Benchmark) |
| 33 | +// first iteration is complete garbage, so make sure we really warmup |
| 34 | +@Warmup(iterations = 4, time = 1) |
| 35 | +// real iterations. not useful to spend tons of time here, better to fork more |
| 36 | +@Measurement(iterations = 5, time = 1) |
| 37 | +// engage some noise reduction |
| 38 | +@Fork(value = 1) |
| 39 | +public class PackAsBinaryBenchmark { |
| 40 | + |
| 41 | + static { |
| 42 | + LogConfigurator.configureESLogging(); // native access requires logging to be initialized |
| 43 | + } |
| 44 | + |
| 45 | + @Param({ "384", "782", "1024" }) |
| 46 | + int dims; |
| 47 | + |
| 48 | + int length; |
| 49 | + |
| 50 | + int numVectors = 1000; |
| 51 | + |
| 52 | + int[][] qVectors; |
| 53 | + byte[] packed; |
| 54 | + |
| 55 | + @Setup |
| 56 | + public void setup() throws IOException { |
| 57 | + Random random = new Random(123); |
| 58 | + |
| 59 | + this.length = BQVectorUtils.discretize(dims, 64) / 8; |
| 60 | + this.packed = new byte[length]; |
| 61 | + |
| 62 | + qVectors = new int[numVectors][dims]; |
| 63 | + for (int[] qVector : qVectors) { |
| 64 | + for (int i = 0; i < dims; i++) { |
| 65 | + qVector[i] = random.nextInt(2); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Benchmark |
| 71 | + public void packAsBinary(Blackhole bh) { |
| 72 | + for (int i = 0; i < numVectors; i++) { |
| 73 | + BQVectorUtils.packAsBinary(qVectors[i], packed); |
| 74 | + bh.consume(packed); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Benchmark |
| 79 | + public void packAsBinaryLegacy(Blackhole bh) { |
| 80 | + for (int i = 0; i < numVectors; i++) { |
| 81 | + BQVectorUtils.packAsBinaryLegacy(qVectors[i], packed); |
| 82 | + bh.consume(packed); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments