|
18 | 18 | import org.hyperledger.besu.evm.operation.ModOperation; |
19 | 19 | import org.hyperledger.besu.evm.operation.Operation; |
20 | 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 | + |
21 | 29 | public class ModOperationBenchmark extends BinaryOperationBenchmark { |
| 30 | + // Benches for a % b |
| 31 | + |
| 32 | + // Define available scenarios |
| 33 | + public enum Case { |
| 34 | + DIV1_MOD1(1, 1), |
| 35 | + DIV2_MOD1(2, 1), |
| 36 | + DIV2_MOD2(2, 2), |
| 37 | + DIV4_MOD1(4, 1), |
| 38 | + DIV4_MOD2(4, 2), |
| 39 | + DIV4_MOD4(4, 4), |
| 40 | + DIV6_MOD1(6, 1), |
| 41 | + DIV6_MOD2(6, 2), |
| 42 | + DIV6_MOD4(6, 4), |
| 43 | + DIV6_MOD6(6, 6), |
| 44 | + DIV8_MOD1(8, 1), |
| 45 | + DIV8_MOD2(8, 2), |
| 46 | + DIV8_MOD4(8, 4), |
| 47 | + DIV8_MOD6(8, 6), |
| 48 | + DIV8_MOD8(8, 8), |
| 49 | + MOD4_LARGER_THAN_DIV2(2, 4), |
| 50 | + MOD8_LARGER_THAN_DIV6(6, 8), |
| 51 | + ZERO_MOD_DIV4(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 | + "DIV1_MOD1", |
| 64 | + "DIV2_MOD1", |
| 65 | + "DIV2_MOD2", |
| 66 | + "DIV4_MOD1", |
| 67 | + "DIV4_MOD2", |
| 68 | + "DIV4_MOD4", |
| 69 | + "DIV6_MOD1", |
| 70 | + "DIV6_MOD2", |
| 71 | + "DIV6_MOD4", |
| 72 | + "DIV6_MOD6", |
| 73 | + "DIV8_MOD1", |
| 74 | + "DIV8_MOD2", |
| 75 | + "DIV8_MOD4", |
| 76 | + "DIV8_MOD6", |
| 77 | + "DIV8_MOD8", |
| 78 | + "MOD4_LARGER_THAN_DIV2", |
| 79 | + "MOD8_LARGER_THAN_DIV6", |
| 80 | + "ZERO_MOD_DIV4" |
| 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 | + BigInteger aInt = new BigInteger(a); |
| 102 | + BigInteger bInt = new BigInteger(b); |
| 103 | + |
| 104 | + if ((scenario.divSize == scenario.modSize) && (aInt.compareTo(bInt) < 0)) { |
| 105 | + aPool[i] = Bytes.wrap(b); |
| 106 | + bPool[i] = Bytes.wrap(a); |
| 107 | + } else { |
| 108 | + aPool[i] = Bytes.wrap(a); |
| 109 | + bPool[i] = Bytes.wrap(b); |
| 110 | + } |
| 111 | + } |
| 112 | + index = 0; |
| 113 | + } |
22 | 114 |
|
23 | 115 | @Override |
24 | 116 | protected Operation.OperationResult invoke(final MessageFrame frame) { |
|
0 commit comments