|
| 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.math.BigInteger; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.concurrent.ThreadLocalRandom; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import org.apache.lucene.util.NumericUtils; |
| 24 | +import org.openjdk.jmh.annotations.Benchmark; |
| 25 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 26 | +import org.openjdk.jmh.annotations.Fork; |
| 27 | +import org.openjdk.jmh.annotations.Level; |
| 28 | +import org.openjdk.jmh.annotations.Measurement; |
| 29 | +import org.openjdk.jmh.annotations.Mode; |
| 30 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 31 | +import org.openjdk.jmh.annotations.Param; |
| 32 | +import org.openjdk.jmh.annotations.Scope; |
| 33 | +import org.openjdk.jmh.annotations.Setup; |
| 34 | +import org.openjdk.jmh.annotations.State; |
| 35 | +import org.openjdk.jmh.annotations.Warmup; |
| 36 | + |
| 37 | +@BenchmarkMode(Mode.Throughput) |
| 38 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 39 | +@State(Scope.Benchmark) |
| 40 | +// first iteration is complete garbage, so make sure we really warmup |
| 41 | +@Warmup(iterations = 4, time = 1) |
| 42 | +// real iterations. not useful to spend tons of time here, better to fork more |
| 43 | +@Measurement(iterations = 5, time = 1) |
| 44 | +// engage some noise reduction |
| 45 | +@Fork( |
| 46 | + value = 3, |
| 47 | + jvmArgsAppend = {"-Xmx2g", "-Xms2g", "-XX:+AlwaysPreTouch"}) |
| 48 | +public class NumericUtilsBenchmark { |
| 49 | + @Param({"1", "128", "207", "256", "300", "512", "702", "1024"}) |
| 50 | + int size; |
| 51 | + |
| 52 | + private byte[] subA; |
| 53 | + private byte[] subB; |
| 54 | + private byte[] subResult; |
| 55 | + private byte[] subExpected; |
| 56 | + |
| 57 | + private byte[] addA; |
| 58 | + private byte[] addB; |
| 59 | + private byte[] addResult; |
| 60 | + private byte[] addExpected; |
| 61 | + |
| 62 | + @Setup(Level.Iteration) |
| 63 | + public void subInit() { |
| 64 | + ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 65 | + |
| 66 | + subA = new byte[size]; |
| 67 | + subB = new byte[size]; |
| 68 | + subResult = new byte[size]; |
| 69 | + subExpected = new byte[size]; |
| 70 | + |
| 71 | + random.nextBytes(subA); |
| 72 | + random.nextBytes(subB); |
| 73 | + |
| 74 | + // Treat as unsigned integers |
| 75 | + BigInteger aBig = new BigInteger(1, subA); |
| 76 | + BigInteger bBig = new BigInteger(1, subB); |
| 77 | + |
| 78 | + // Swap a <-> b if a < b |
| 79 | + if (aBig.compareTo(bBig) < 0) { |
| 80 | + byte[] temp = subA; |
| 81 | + subA = subB; |
| 82 | + subB = temp; |
| 83 | + |
| 84 | + BigInteger tempBig = aBig; |
| 85 | + aBig = bBig; |
| 86 | + bBig = tempBig; |
| 87 | + } |
| 88 | + |
| 89 | + byte[] temp = aBig.subtract(bBig).toByteArray(); |
| 90 | + if (temp.length == size + 1) { // BigInteger pads with extra 0 if MSB is 1 |
| 91 | + assert temp[0] == 0; |
| 92 | + System.arraycopy(temp, 1, subExpected, 0, size); |
| 93 | + } else { |
| 94 | + System.arraycopy(temp, 0, subExpected, size - temp.length, temp.length); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Setup(Level.Iteration) |
| 99 | + public void addInit() { |
| 100 | + ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 101 | + |
| 102 | + addA = new byte[size]; |
| 103 | + addB = new byte[size]; |
| 104 | + addResult = new byte[size]; |
| 105 | + addExpected = new byte[size]; |
| 106 | + |
| 107 | + random.nextBytes(addA); |
| 108 | + random.nextBytes(addB); |
| 109 | + |
| 110 | + // Treat as unsigned integers |
| 111 | + BigInteger aBig = new BigInteger(1, addA); |
| 112 | + BigInteger bBig = new BigInteger(1, addB); |
| 113 | + |
| 114 | + byte[] temp = aBig.add(bBig).toByteArray(); |
| 115 | + if (temp.length == size + 1) { // BigInteger pads with extra 0 if MSB is 1 |
| 116 | + if (temp[0] != 0) { // overflow |
| 117 | + addInit(); // re-init |
| 118 | + return; |
| 119 | + } |
| 120 | + System.arraycopy(temp, 1, addExpected, 0, size); |
| 121 | + } else { |
| 122 | + System.arraycopy(temp, 0, addExpected, size - temp.length, temp.length); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Benchmark |
| 127 | + public void subtract() { |
| 128 | + NumericUtils.subtract(size, 0, subA, subB, subResult); |
| 129 | + assert Arrays.equals(subExpected, subResult); |
| 130 | + } |
| 131 | + |
| 132 | + @Benchmark |
| 133 | + public void add() { |
| 134 | + NumericUtils.add(size, 0, addA, addB, addResult); |
| 135 | + assert Arrays.equals(addExpected, addResult); |
| 136 | + } |
| 137 | +} |
0 commit comments