|
| 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 | + |
| 10 | +package org.elasticsearch.benchmark.vector; |
| 11 | + |
| 12 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 13 | + |
| 14 | +import org.apache.lucene.util.Constants; |
| 15 | +import org.elasticsearch.test.ESTestCase; |
| 16 | +import org.junit.BeforeClass; |
| 17 | +import org.openjdk.jmh.annotations.Param; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | + |
| 21 | +public class Int7uScorerBenchmarkTests extends ESTestCase { |
| 22 | + |
| 23 | + final double delta = 1e-3; |
| 24 | + final int dims; |
| 25 | + |
| 26 | + public Int7uScorerBenchmarkTests(int dims) { |
| 27 | + this.dims = dims; |
| 28 | + } |
| 29 | + |
| 30 | + @BeforeClass |
| 31 | + public static void skipWindows() { |
| 32 | + assumeFalse("doesn't work on windows yet", Constants.WINDOWS); |
| 33 | + } |
| 34 | + |
| 35 | + public void testDotProduct() throws Exception { |
| 36 | + for (int i = 0; i < 100; i++) { |
| 37 | + var bench = new Int7uScorerBenchmark(); |
| 38 | + bench.dims = dims; |
| 39 | + bench.setup(); |
| 40 | + try { |
| 41 | + float expected = bench.dotProductScalar(); |
| 42 | + assertEquals(expected, bench.dotProductLucene(), delta); |
| 43 | + assertEquals(expected, bench.dotProductNative(), delta); |
| 44 | + |
| 45 | + expected = bench.dotProductLuceneQuery(); |
| 46 | + assertEquals(expected, bench.dotProductNativeQuery(), delta); |
| 47 | + } finally { |
| 48 | + bench.teardown(); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public void testSquareDistance() throws Exception { |
| 54 | + for (int i = 0; i < 100; i++) { |
| 55 | + var bench = new Int7uScorerBenchmark(); |
| 56 | + bench.dims = dims; |
| 57 | + bench.setup(); |
| 58 | + try { |
| 59 | + float expected = bench.squareDistanceScalar(); |
| 60 | + assertEquals(expected, bench.squareDistanceLucene(), delta); |
| 61 | + assertEquals(expected, bench.squareDistanceNative(), delta); |
| 62 | + |
| 63 | + expected = bench.squareDistanceLuceneQuery(); |
| 64 | + assertEquals(expected, bench.squareDistanceNativeQuery(), delta); |
| 65 | + } finally { |
| 66 | + bench.teardown(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @ParametersFactory |
| 72 | + public static Iterable<Object[]> parametersFactory() { |
| 73 | + try { |
| 74 | + var params = Int7uScorerBenchmark.class.getField("dims").getAnnotationsByType(Param.class)[0].value(); |
| 75 | + return () -> Arrays.stream(params).map(Integer::parseInt).map(i -> new Object[] { i }).iterator(); |
| 76 | + } catch (NoSuchFieldException e) { |
| 77 | + throw new AssertionError(e); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments