|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.lucene.benchmark.jmh; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.UncheckedIOException; |
| 21 | +import java.lang.invoke.MethodHandle; |
| 22 | +import java.lang.invoke.MethodHandles; |
| 23 | +import java.lang.invoke.MethodType; |
| 24 | +import java.text.ParseException; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.concurrent.ThreadLocalRandom; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import org.apache.lucene.expressions.Expression; |
| 31 | +import org.apache.lucene.expressions.js.JavascriptCompiler; |
| 32 | +import org.apache.lucene.search.DoubleValues; |
| 33 | +import org.openjdk.jmh.annotations.Benchmark; |
| 34 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 35 | +import org.openjdk.jmh.annotations.Fork; |
| 36 | +import org.openjdk.jmh.annotations.Level; |
| 37 | +import org.openjdk.jmh.annotations.Measurement; |
| 38 | +import org.openjdk.jmh.annotations.Mode; |
| 39 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 40 | +import org.openjdk.jmh.annotations.Param; |
| 41 | +import org.openjdk.jmh.annotations.Scope; |
| 42 | +import org.openjdk.jmh.annotations.Setup; |
| 43 | +import org.openjdk.jmh.annotations.State; |
| 44 | +import org.openjdk.jmh.annotations.Warmup; |
| 45 | + |
| 46 | +@BenchmarkMode(Mode.Throughput) |
| 47 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 48 | +@State(Scope.Benchmark) |
| 49 | +@Warmup(iterations = 5, time = 5) |
| 50 | +@Measurement(iterations = 12, time = 8) |
| 51 | +@Fork(value = 1) |
| 52 | +public class ExpressionsBenchmark { |
| 53 | + |
| 54 | + /** |
| 55 | + * Some extra functions to bench "identity" in various variants, another one is named |
| 56 | + * "native_identity" (see below). |
| 57 | + */ |
| 58 | + private static final Map<String, MethodHandle> FUNCTIONS = getFunctions(); |
| 59 | + |
| 60 | + private static final String NATIVE_IDENTITY_NAME = "native_identity"; |
| 61 | + |
| 62 | + private static Map<String, MethodHandle> getFunctions() { |
| 63 | + try { |
| 64 | + var lookup = MethodHandles.lookup(); |
| 65 | + Map<String, MethodHandle> m = new HashMap<>(JavascriptCompiler.DEFAULT_FUNCTIONS); |
| 66 | + m.put( |
| 67 | + "func_identity", |
| 68 | + lookup.findStatic( |
| 69 | + lookup.lookupClass(), "ident", MethodType.methodType(double.class, double.class))); |
| 70 | + m.put("mh_identity", MethodHandles.identity(double.class)); |
| 71 | + return m; |
| 72 | + } catch (ReflectiveOperationException e) { |
| 73 | + throw new AssertionError(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @SuppressWarnings("unused") |
| 78 | + private static double ident(double v) { |
| 79 | + return v; |
| 80 | + } |
| 81 | + |
| 82 | + /** A native implementation of an expression to compare performance */ |
| 83 | + private static final Expression NATIVE_IDENTITY_EXPRESSION = |
| 84 | + new Expression(NATIVE_IDENTITY_NAME, new String[] {"x"}) { |
| 85 | + @Override |
| 86 | + public double evaluate(DoubleValues[] functionValues) { |
| 87 | + try { |
| 88 | + return functionValues[0].doubleValue(); |
| 89 | + } catch (IOException e) { |
| 90 | + throw new UncheckedIOException(e); |
| 91 | + } |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + private double[] randomData; |
| 96 | + private Expression expression; |
| 97 | + |
| 98 | + @Param({"x", "func_identity(x)", "mh_identity", "native_identity", "cos(x)", "cos(x) + sin(x)"}) |
| 99 | + String js; |
| 100 | + |
| 101 | + @Setup(Level.Iteration) |
| 102 | + public void init() throws ParseException { |
| 103 | + ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 104 | + randomData = random.doubles().limit(1024).toArray(); |
| 105 | + expression = |
| 106 | + Objects.equals(js, NATIVE_IDENTITY_NAME) |
| 107 | + ? NATIVE_IDENTITY_EXPRESSION |
| 108 | + : JavascriptCompiler.compile(js, FUNCTIONS); |
| 109 | + } |
| 110 | + |
| 111 | + @Benchmark |
| 112 | + public double expression() throws IOException { |
| 113 | + var it = new ValuesIterator(randomData); |
| 114 | + var values = it.getDoubleValues(); |
| 115 | + double result = 0d; |
| 116 | + while (it.next()) { |
| 117 | + result += expression.evaluate(values); |
| 118 | + } |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + static final class ValuesIterator { |
| 123 | + final double[] data; |
| 124 | + final DoubleValues[] dv; |
| 125 | + int pos = -1; |
| 126 | + |
| 127 | + ValuesIterator(double[] data) { |
| 128 | + this.data = data; |
| 129 | + var dv = |
| 130 | + new DoubleValues() { |
| 131 | + @Override |
| 132 | + public double doubleValue() throws IOException { |
| 133 | + return data[pos]; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public boolean advanceExact(int doc) throws IOException { |
| 138 | + throw new UnsupportedOperationException(); |
| 139 | + } |
| 140 | + }; |
| 141 | + this.dv = new DoubleValues[] {dv}; |
| 142 | + } |
| 143 | + |
| 144 | + boolean next() { |
| 145 | + pos++; |
| 146 | + return (pos < data.length); |
| 147 | + } |
| 148 | + |
| 149 | + DoubleValues[] getDoubleValues() { |
| 150 | + return dv; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments