|
| 1 | +/* |
| 2 | + * Copyright contributors to Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.ethereum.vm.operations; |
| 16 | + |
| 17 | +import org.hyperledger.besu.evm.frame.MessageFrame; |
| 18 | +import org.hyperledger.besu.evm.operation.ModOperation; |
| 19 | +import org.hyperledger.besu.evm.operation.Operation; |
| 20 | + |
| 21 | +import java.math.BigInteger; |
| 22 | +import java.util.concurrent.ThreadLocalRandom; |
| 23 | + |
| 24 | +import org.apache.tuweni.bytes.Bytes; |
| 25 | +import org.openjdk.jmh.annotations.Level; |
| 26 | +import org.openjdk.jmh.annotations.Param; |
| 27 | +import org.openjdk.jmh.annotations.Setup; |
| 28 | + |
| 29 | +public class ModOperationBySizesBenchmark extends BinaryOperationBenchmark { |
| 30 | + // Benches for a % b |
| 31 | + |
| 32 | + // Define available scenarios |
| 33 | + public enum Case { |
| 34 | + MOD_32_32(1, 1), |
| 35 | + MOD_64_32(2, 1), |
| 36 | + MOD_64_64(2, 2), |
| 37 | + MOD_128_32(4, 1), |
| 38 | + MOD_128_64(4, 2), |
| 39 | + MOD_128_128(4, 4), |
| 40 | + MOD_192_32(6, 1), |
| 41 | + MOD_192_64(6, 2), |
| 42 | + MOD_192_128(6, 4), |
| 43 | + MOD_192_192(6, 6), |
| 44 | + MOD_256_32(8, 1), |
| 45 | + MOD_256_64(8, 2), |
| 46 | + MOD_256_128(8, 4), |
| 47 | + MOD_256_192(8, 6), |
| 48 | + MOD_256_256(8, 8), |
| 49 | + LARGER_MOD_64_128(2, 4), |
| 50 | + LARGER_MOD_192_256(6, 8), |
| 51 | + ZERO_MOD_128_0(4, 0); |
| 52 | + |
| 53 | + final int divSize; |
| 54 | + final int modSize; |
| 55 | + |
| 56 | + Case(final int divSize, final int modSize) { |
| 57 | + this.divSize = divSize; |
| 58 | + this.modSize = modSize; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Param({ |
| 63 | + "MOD_32_32", |
| 64 | + "MOD_64_32", |
| 65 | + "MOD_64_64", |
| 66 | + "MOD_128_32", |
| 67 | + "MOD_128_64", |
| 68 | + "MOD_128_128", |
| 69 | + "MOD_192_32", |
| 70 | + "MOD_192_64", |
| 71 | + "MOD_192_128", |
| 72 | + "MOD_192_192", |
| 73 | + "MOD_256_32", |
| 74 | + "MOD_256_64", |
| 75 | + "MOD_256_128", |
| 76 | + "MOD_256_192", |
| 77 | + "MOD_256_256", |
| 78 | + "LARGER_MOD_64_128", |
| 79 | + "LARGER_MOD_192_256", |
| 80 | + "ZERO_MOD_128_0" |
| 81 | + }) |
| 82 | + private String caseName; |
| 83 | + |
| 84 | + @Setup(Level.Iteration) |
| 85 | + @Override |
| 86 | + public void setUp() { |
| 87 | + frame = BenchmarkHelper.createMessageCallFrame(); |
| 88 | + |
| 89 | + Case scenario = Case.valueOf(caseName); |
| 90 | + aPool = new Bytes[SAMPLE_SIZE]; |
| 91 | + bPool = new Bytes[SAMPLE_SIZE]; |
| 92 | + |
| 93 | + final ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 94 | + |
| 95 | + for (int i = 0; i < SAMPLE_SIZE; i++) { |
| 96 | + final byte[] a = new byte[scenario.divSize * 4]; |
| 97 | + final byte[] b = new byte[scenario.modSize * 4]; |
| 98 | + random.nextBytes(a); |
| 99 | + random.nextBytes(b); |
| 100 | + |
| 101 | + if ((scenario.divSize != scenario.modSize)) { |
| 102 | + aPool[i] = Bytes.wrap(a); |
| 103 | + bPool[i] = Bytes.wrap(b); |
| 104 | + } else { |
| 105 | + BigInteger aInt = new BigInteger(a); |
| 106 | + BigInteger bInt = new BigInteger(b); |
| 107 | + if ((aInt.compareTo(bInt) < 0)) { |
| 108 | + aPool[i] = Bytes.wrap(b); |
| 109 | + bPool[i] = Bytes.wrap(a); |
| 110 | + } else { |
| 111 | + aPool[i] = Bytes.wrap(a); |
| 112 | + bPool[i] = Bytes.wrap(b); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + index = 0; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + protected Operation.OperationResult invoke(final MessageFrame frame) { |
| 121 | + return ModOperation.staticOperation(frame); |
| 122 | + } |
| 123 | +} |
0 commit comments