Skip to content

Commit

Permalink
Fix #385: Add a benchmark for generating random bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
romanstrobl committed Nov 30, 2023
1 parent 8db4fda commit abe22cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public enum PowerAuthStep {
*/
ENCRYPT("encrypt", "Encrypt Request", "encrypt"),

/**
* Run benchmark for generating random bytes
*/
GENERATE_BENCHMARK("generate-benchmark", "Benchmark for generating random bytes", "generate-benchmark"),

/**
* Confirming an activation recovery
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.getlime.security.powerauth.app.cmd.exception.ExecutionException;
import io.getlime.security.powerauth.crypto.lib.enums.PowerAuthSignatureTypes;
import io.getlime.security.powerauth.crypto.lib.generator.KeyGenerator;
import io.getlime.security.powerauth.lib.cmd.CmdLibApplication;
import io.getlime.security.powerauth.lib.cmd.consts.PowerAuthStep;
import io.getlime.security.powerauth.lib.cmd.consts.PowerAuthVersion;
Expand Down Expand Up @@ -57,6 +58,9 @@
*/
public class Application {

private static final long GENERATE_RANDOM_BENCHMARK_ROUNDS = 10_000L;
private static final KeyGenerator KEY_GENERATOR = new KeyGenerator();

/**
* Application main
* @param args Arguments, use --help to print expected arguments
Expand Down Expand Up @@ -107,6 +111,7 @@ public static void main(String[] args) {
options.addOption("P", "platform", true, "User device platform.");
options.addOption("D", "device-info", true, "Information about user device.");
options.addOption("q", "qr-code-data", true, "Data for offline signature encoded in QR code.");
options.addOption("gr", "generate-rounds", true, "Number of rounds for generate random bytes benchmark.");
options.addOption("v", "version", true, "PowerAuth protocol version.");

Option httpHeaderOption = Option.builder("H")
Expand Down Expand Up @@ -547,6 +552,24 @@ public static void main(String[] args) {

stepExecutionService.execute(powerAuthStep, version, model);
}

case GENERATE_BENCHMARK -> {
final String roundsParam = cmd.getOptionValue("gr");
final long rounds;
if (roundsParam != null && roundsParam.matches("[0-9]+")) {
rounds = Long.parseLong(roundsParam);
} else {
rounds = GENERATE_RANDOM_BENCHMARK_ROUNDS;
}
System.out.println("Generating " + rounds + " rounds of 32-byte random bytes started.");
long timeStart = System.currentTimeMillis();
for (int i = 0; i < rounds; i++) {
KEY_GENERATOR.generateRandomBytes(32);
}
long totalTime = System.currentTimeMillis() - timeStart;
System.out.println("Generating " + rounds + " rounds of 32-byte random bytes finished, total time: " + totalTime + "ms.");
}

default -> {
System.err.println("Not recognized PowerAuth step: " + powerAuthStep);
printPowerAuthStepsHelp(stepProvider);
Expand Down

0 comments on commit abe22cd

Please sign in to comment.