Skip to content

Commit

Permalink
Test: Add benchmarks for Curl and PearlDiver (iotaledger#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmeylan authored and GalRogozinski committed Feb 17, 2019
1 parent c3ed671 commit 650bf8d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/test/java/com/iota/iri/benchmarks/BenchmarkRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,19 @@ public void launchDbBenchmarks() throws RunnerException {
//possible to do assertions over run results
new Runner(opts).run();
}

@Test
public void launchCryptoBenchmark() throws RunnerException {
Options opts = new OptionsBuilder()
.include(this.getClass().getPackage().getName() + ".crypto")
.mode(Mode.Throughput)
.timeUnit(TimeUnit.SECONDS)
.warmupIterations(5)
.forks(1)
.measurementIterations(10)
.shouldFailOnError(true)
.shouldDoGC(false)
.build();
new Runner(opts).run();
}
}
62 changes: 62 additions & 0 deletions src/test/java/com/iota/iri/benchmarks/crypto/CurlBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.iota.iri.benchmarks.crypto;

import com.iota.iri.crypto.Curl;
import com.iota.iri.crypto.SpongeFactory;
import com.iota.iri.utils.Converter;
import com.iota.iri.utils.Pair;
import org.junit.Assert;
import org.openjdk.jmh.annotations.Benchmark;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class CurlBenchmark {
private final static String TRYTES = "RSWWSFXPQJUBJROQBRQZWZXZJWMUBVIVMHPPTYSNW9YQIQQF9RCSJJCVZG9ZWITXNCSBBDHEEKDRBHVTWCZ9SZOOZHVBPCQNPKTWFNZAWGCZ9QDIMKRVINMIRZBPKRKQAIPGOHBTHTGYXTBJLSURDSPEOJ9UKJECUKCCPVIQQHDUYKVKISCEIEGVOQWRBAYXWGSJUTEVG9RPQLPTKYCRAJ9YNCUMDVDYDQCKRJOAPXCSUDAJGETALJINHEVNAARIPONBWXUOQUFGNOCUSSLYWKOZ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CXIXLRMRWMAYYQJPCEAAOFEOGZQMEDAGYGCTKUJBS9AGEXJAFHWWDZRYEN9DN9HVCMLFURISLYSWKXHJKXMHUWZXUQARMYPGKRKQMHVR9JEYXJRPNZINYNCGZHHUNHBAIJHLYZIZGGIDFWVNXZQADLEDJFTIUTQWCQSX9QNGUZXGXJYUUTFSZPQKXBA9DFRQRLTLUJENKESDGTZRGRSLTNYTITXRXRGVLWBTEWPJXZYLGHLQBAVYVOSABIVTQYQM9FIQKCBRRUEMVVTMERLWOK";
private final static String HASH = "TIXEPIEYMGURTQ9ABVYVQSWMNGCVQFASMFAEQWUZCLIWLCDIGYVXOEJBBEMZOIHAYSUQMEFOGZBXUMHQW";

/**
* Benchmark absorb and squeeze methods of Curl 81 hash function.
*/
@Benchmark
public void curl() {
int size = 8019;
byte[] in_trits = new byte[size];
byte[] hash_trits = new byte[Curl.HASH_LENGTH];
Converter.trits(TRYTES, in_trits, 0);
Curl curl = (Curl) SpongeFactory.create(SpongeFactory.Mode.CURLP81);
curl.absorb(in_trits, 0, in_trits.length);
curl.squeeze(hash_trits, 0, Curl.HASH_LENGTH);
String out_trytes = Converter.trytes(hash_trits);
Assert.assertEquals(HASH, out_trytes);
}

/**
* Benchmark absorb and squeeze methods of pair Curl 81 hash function.
*/
@Benchmark
public void pairCurl() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
int size = 8019;
byte[] in_trits = new byte[size];
Pair<long[], long[]> hashPair = new Pair<>(new long[Curl.HASH_LENGTH], new long[Curl.HASH_LENGTH]);
Converter.trits(TRYTES, in_trits, 0);
// Using reflection to benchmark private, non-production code.
// Reflection doesn't have impact on benchmark result (this has been tested)
// Please remove this code when method are public
Class<Curl> curlClass = Curl.class;
Constructor<Curl> curlConstructor = curlClass.getDeclaredConstructor(boolean.class, SpongeFactory.Mode.class);
curlConstructor.setAccessible(true);
Curl curl = curlConstructor.newInstance(true, SpongeFactory.Mode.CURLP81);
Method pairAbsorb = curlClass.getDeclaredMethod("absorb", Pair.class, int.class, int.class);
Method pairSqueeze = curlClass.getDeclaredMethod("squeeze", Pair.class, int.class, int.class);
pairAbsorb.setAccessible(true);
pairSqueeze.setAccessible(true);

pairAbsorb.invoke(curl, Converter.longPair(in_trits), 0, in_trits.length);
pairSqueeze.invoke(curl, hashPair, 0, Curl.HASH_LENGTH);
byte[] hash_trits = Converter.trits(hashPair.low, hashPair.hi);
String out_trytes = Converter.trytes(hash_trits);
Assert.assertEquals(HASH, out_trytes);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.iota.iri.benchmarks.crypto;

import com.iota.iri.crypto.PearlDiver;
import com.iota.iri.utils.Converter;
import org.openjdk.jmh.annotations.Benchmark;

public class PearlDiverBenchmark {
private static final int MIN_WEIGHT_MAGNITUDE = 9;
private static final int NUM_CORES = -1; // use n-1 cores
private static final String TRYTES = "ZMGYJPAAYHGRDGKCUEAIZDERGWUJH9QIHEESTUAZIQDQGMREKOVZCQRKHUZPXQ9PPIBGARFTZBZCYGAWIEUGPMIEEMKVSLCCJPITK9A9VLHCWLRZTRYDPGPFGEEFMVMVQGGM9NFPEKLVQHMOPDTJHDIEHBFGBBFOHPLHX9RJVEIQC9WAOFPHP9GGFRYCJEX9UVUGVJLBCBNQCLKSLULGZUHCVUJZMILSROKHYAA9RLF9XQXKXCULHJIUGXDWBKJLGLHXXR9BNKDKG9JMJCTAGMKWGXPPKVBVNROUADXHUJWYDWZDSIUIVUTKKBTVJ9CMKWVFMCYMCVFMDPLQ9YFIHUMRTBZLIT9LIQRFXF9AKDMTXMHKCSFQDJIFQRFBHSIKVKQBUKFDPOLWQXBUZKNGGHJW9IVLG9GTIW9IRWGCM9A9YT99JUDKQQLWBFXGYVDYSRYSAWVRBFXIKMQRGCIHLWOUTKFWWDFRQZMCAUXSSK9LWQRCNNCXBJOIKVMLTMRENT9YLUOHACKHYYUERSBKWSCJTCJBCDCARABPVNNKIXXSVLELPILCTRQCEGYITPVODUMUEYFRJBOKAVVI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chmark
public void search() {
PearlDiver pearlDiver = new PearlDiver();
byte[] myTrits = Converter.allocateTritsForTrytes(TRYTES.length());
Converter.trits(TRYTES, myTrits, 0);
pearlDiver.search(myTrits, MIN_WEIGHT_MAGNITUDE, NUM_CORES);
}

}

0 comments on commit 650bf8d

Please sign in to comment.