-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRandomXBenchmark.java
More file actions
184 lines (163 loc) · 6.28 KB
/
RandomXBenchmark.java
File metadata and controls
184 lines (163 loc) · 6.28 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* The MIT License (MIT)
*
* Copyright (c) 2022-2030 The XdagJ Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.xdag.crypto.randomx;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Comprehensive benchmark for RandomX operations.
* This benchmark tests both mining and light modes with batch and non-batch operations.
* Uses JMH framework for accurate performance measurements.
*/
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Fork(1)
@Warmup(iterations = 1, time = 10)
@Measurement(iterations = 1, time = 10)
@Threads(8)
public class RandomXBenchmark {
private static final Logger logger = LoggerFactory.getLogger(RandomXBenchmark.class);
// Sample block template for hash calculation
private static final byte[] BLOCK_TEMPLATE = {
(byte)0x07, (byte)0x07, (byte)0xf7, (byte)0xa4, (byte)0xf0, (byte)0xd6, (byte)0x05, (byte)0xb3,
(byte)0x03, (byte)0x26, (byte)0x08, (byte)0x16, (byte)0xba, (byte)0x3f, (byte)0x10, (byte)0x90,
(byte)0x2e, (byte)0x1a, (byte)0x14, (byte)0x5a, (byte)0xc5, (byte)0xfa, (byte)0xd3, (byte)0xaa,
(byte)0x3a, (byte)0xf6, (byte)0xea, (byte)0x44, (byte)0xc1, (byte)0x18, (byte)0x69, (byte)0xdc,
(byte)0x4f, (byte)0x85, (byte)0x3f, (byte)0x00, (byte)0x2b, (byte)0x2e, (byte)0xea, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x00
};
// Shared resources across all benchmark threads
private Set<RandomXFlag> flags;
private RandomXCache cache;
private RandomXDataset dataset;
/**
* Thread-local state containing RandomX templates for both mining and light modes
*/
@State(Scope.Thread)
public static class ThreadState {
RandomXTemplate lightTemplate;
RandomXTemplate miningTemplate;
/**
* Initialize thread-local templates using shared benchmark resources
*/
@Setup(Level.Trial)
public void setup(RandomXBenchmark benchmark) {
lightTemplate = RandomXTemplate.builder()
.miningMode(false)
.flags(benchmark.flags)
.cache(benchmark.cache)
.build();
lightTemplate.init();
miningTemplate = RandomXTemplate.builder()
.miningMode(true)
.flags(benchmark.flags)
.cache(benchmark.cache)
.dataset(benchmark.dataset)
.build();
miningTemplate.init();
}
/**
* Clean up thread-local resources
*/
@TearDown(Level.Trial)
public void tearDown() {
if (lightTemplate != null) lightTemplate.close();
if (miningTemplate != null) miningTemplate.close();
}
}
/**
* Initialize shared resources used across all benchmark threads
*/
@Setup(Level.Trial)
public void setup() {
logger.info("Setting up shared resources");
flags = RandomXUtils.getFlagsSet();
cache = new RandomXCache(flags);
cache.init(BLOCK_TEMPLATE);
dataset = new RandomXDataset(flags);
logger.info("Shared resources setup completed");
}
/**
* Clean up shared resources after benchmark completion
*/
@TearDown(Level.Trial)
public void tearDown() {
logger.info("Cleaning up shared resources");
if (dataset != null) dataset.close();
if (cache != null) cache.close();
}
/**
* Benchmark mining mode without batch processing
*/
@Benchmark
@Group("miningNoBatch")
public byte[] miningModeNoBatchHash(ThreadState state) {
return state.miningTemplate.calculateHash(BLOCK_TEMPLATE);
}
/**
* Benchmark mining mode with batch processing
*/
@Benchmark
@Group("miningBatch")
public byte[] miningModeBatchHash(ThreadState state) {
state.miningTemplate.calculateHashFirst(BLOCK_TEMPLATE);
return state.miningTemplate.calculateHashNext(BLOCK_TEMPLATE);
}
/**
* Benchmark light mode without batch processing
*/
@Benchmark
@Group("lightNoBatch")
public byte[] lightModeNoBatchHash(ThreadState state) {
return state.lightTemplate.calculateHash(BLOCK_TEMPLATE);
}
/**
* Benchmark light mode with batch processing
*/
@Benchmark
@Group("lightBatch")
public byte[] lightModeBatchHash(ThreadState state) {
state.lightTemplate.calculateHashFirst(BLOCK_TEMPLATE);
return state.lightTemplate.calculateHashNext(BLOCK_TEMPLATE);
}
/**
* Main method to run the benchmark
* Configures JMH options and executes the benchmark suite
*/
public static void main(String[] args) throws Exception {
Options opt = new OptionsBuilder()
.include(RandomXBenchmark.class.getSimpleName())
.resultFormat(ResultFormatType.TEXT)
.jvmArgs("-Xms2G", "-Xmx2G")
.build();
new Runner(opt).run();
}
}