-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
rskj-core/src/jmh/java/co/rsk/jmh/utils/BenchmarkByteUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package co.rsk.jmh.utils; | ||
|
||
import co.rsk.core.types.bytes.Bytes; | ||
import co.rsk.jmh.utils.plan.DataPlan; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode({Mode.Throughput}) | ||
@Warmup(iterations = 1, time = 5 /* secs */) | ||
@Measurement(iterations = 3, time = 5 /* secs */) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
public class BenchmarkByteUtil { | ||
|
||
@Benchmark | ||
public void toHexString_Bouncycastle(DataPlan plan) { | ||
byte[] data = plan.getData(); | ||
int off = plan.getNextRand(data.length); | ||
int len = plan.getNextRand(data.length - off); | ||
org.bouncycastle.util.encoders.Hex.toHexString(data, off, len); | ||
} | ||
|
||
@Benchmark | ||
public void toHexString_V2(DataPlan plan) { | ||
Bytes bytes = plan.getBytes(); | ||
int bytesLen = bytes.length(); | ||
int off = plan.getNextRand(bytesLen); | ||
int len = plan.getNextRand(bytesLen - off); | ||
bytes.toHexString(off, len); | ||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opt = new OptionsBuilder() | ||
.include(BenchmarkByteUtil.class.getName()) | ||
.forks(2) | ||
.build(); | ||
|
||
new Runner(opt).run(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
rskj-core/src/jmh/java/co/rsk/jmh/utils/plan/DataPlan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* This file is part of RskJ | ||
* Copyright (C) 2024 RSK Labs Ltd. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package co.rsk.jmh.utils.plan; | ||
|
||
import co.rsk.core.types.bytes.Bytes; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
|
||
import java.util.Random; | ||
|
||
@State(Scope.Benchmark) | ||
public class DataPlan { | ||
|
||
private final byte[] data = new byte[1024]; | ||
|
||
private Bytes bytes; | ||
|
||
private Random random; | ||
|
||
@Setup(Level.Trial) | ||
public void doSetup() { | ||
random = new Random(111); | ||
random.nextBytes(data); | ||
bytes = Bytes.of(data); | ||
} | ||
|
||
public byte[] getData() { | ||
return data; | ||
} | ||
|
||
public Bytes getBytes() { | ||
return bytes; | ||
} | ||
|
||
public int getNextRand(int bound) { | ||
return random.nextInt(bound); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters