forked from tmio/tuweni
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBytesMegamorphicBenchmarkV1.java
More file actions
62 lines (54 loc) · 1.91 KB
/
BytesMegamorphicBenchmarkV1.java
File metadata and controls
62 lines (54 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright The Tuweni Authors
// SPDX-License-Identifier: Apache-2.0
package org.benchmark;
import org.apache.tuweni.bytes.Bytes;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@BenchmarkMode(value = Mode.AverageTime)
@State(Scope.Thread)
@OutputTimeUnit(value = TimeUnit.NANOSECONDS)
public class BytesMegamorphicBenchmarkV1 {
private static final int N = 4;
private static final int FACTOR = 1_000;
private static final Random RANDOM = new Random(23L);
Bytes[] bytesV1;
@Param({"mono", "mega"})
private String mode;
@Setup
public void setup() {
bytesV1 = new Bytes[N * FACTOR];
for (int i = 0; i < N * FACTOR; i += N) {
bytesV1[i] = Bytes.wrap(getBytes(32));
bytesV1[i + 1] = "mega".equals(mode) ? Bytes.wrap(getBytes(48)) : Bytes.wrap(getBytes(32));
bytesV1[i + 2] =
"mega".equals(mode) ? Bytes.repeat((byte) 0x09, 16) : Bytes.wrap(getBytes(32));
bytesV1[i + 3] =
"mega".equals(mode) ? Bytes.wrap(bytesV1[i], bytesV1[i + 1]) : Bytes.wrap(getBytes(32));
}
}
private static byte[] getBytes(final int size) {
byte[] b = new byte[size];
RANDOM.nextBytes(b);
return b;
}
@Benchmark
@OperationsPerInvocation(N * FACTOR)
public void test() {
for (Bytes b : bytesV1) {
b.slice(1);
}
}
}