From 650bf8da4357431f87b4f276c41387eff1ffbb8a Mon Sep 17 00:00:00 2001 From: Nicolas Meylan Date: Sun, 17 Feb 2019 15:53:50 +0100 Subject: [PATCH] Test: Add benchmarks for Curl and PearlDiver (#1331) --- .../iota/iri/benchmarks/BenchmarkRunner.java | 15 +++++ .../iri/benchmarks/crypto/CurlBenchmark.java | 62 +++++++++++++++++++ .../crypto/PearlDiverBenchmark.java | 23 +++++++ 3 files changed, 100 insertions(+) create mode 100644 src/test/java/com/iota/iri/benchmarks/crypto/CurlBenchmark.java create mode 100644 src/test/java/com/iota/iri/benchmarks/crypto/PearlDiverBenchmark.java diff --git a/src/test/java/com/iota/iri/benchmarks/BenchmarkRunner.java b/src/test/java/com/iota/iri/benchmarks/BenchmarkRunner.java index a5954a2ad6..ef4a22af9c 100644 --- a/src/test/java/com/iota/iri/benchmarks/BenchmarkRunner.java +++ b/src/test/java/com/iota/iri/benchmarks/BenchmarkRunner.java @@ -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(); + } } diff --git a/src/test/java/com/iota/iri/benchmarks/crypto/CurlBenchmark.java b/src/test/java/com/iota/iri/benchmarks/crypto/CurlBenchmark.java new file mode 100644 index 0000000000..341ab3d23a --- /dev/null +++ b/src/test/java/com/iota/iri/benchmarks/crypto/CurlBenchmark.java @@ -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 = "RSWWSFXPQJUBJROQBRQZWZXZJWMUBVIVMHPPTYSNW9YQIQQF9RCSJJCVZG9ZWITXNCSBBDHEEKDRBHVTWCZ9SZOOZHVBPCQNPKTWFNZAWGCZ9QDIMKRVINMIRZBPKRKQAIPGOHBTHTGYXTBJLSURDSPEOJ9UKJECUKCCPVIQQHDUYKVKISCEIEGVOQWRBAYXWGSJUTEVG9RPQLPTKYCRAJ9YNCUMDVDYDQCKRJOAPXCSUDAJGETALJINHEVNAARIPONBWXUOQUFGNOCUSSLYWKOZMZUKLNITZIFXFWQAYVJCVMDTRSHORGNSTKX9Z9DLWNHZSMNOYTU9AUCGYBVIITEPEKIXBCOFCMQPBGXYJKSHPXNUKFTXIJVYRFILAVXEWTUICZCYYPCEHNTK9SLGVL9RLAMYTAEPONCBHDXSEQZOXO9XCFUCPPMKEBR9IEJGQOPPILHFXHMIULJYXZJASQEGCQDVYFOM9ETXAGVMSCHHQLFPATWOSMZIDL9AHMSDCE9UENACG9OVFAEIPPQYBCLXDMXXA9UBJFQQBCYKETPNKHNOUKCSSYLWZDLKUARXNVKKKHNRBVSTVKQCZL9RY9BDTDTPUTFUBGRMSTOTXLWUHDMSGYRDSZLIPGQXIDMNCNBOAOI9WFUCXSRLJFIVTIPIAZUK9EDUJJ9B9YCJEZQQELLHVCWDNRH9FUXDGZRGOVXGOKORTCQQA9JXNROLETYCNLRMBGXBL9DQKMOAZCBJGWLNJLGRSTYBKLGFVRUF9QOPZVQFGMDJA9TBVGFJDBAHEVOLW9GNU9NICLCQJBOAJBAHHBZJGOFUCQMBGYQLCWNKSZPPBQMSJTJLM9GXOZHTNDLGIRCSIJAZTENQVQDHFSOQM9WVNWQQJNOPZMEISSCLOADMRNWALBBSLSWNCTOSNHNLWZBVCFIOGFPCPRKQSRGKFXGTWUSCPZSKQNLQJGKDLOXSBJMEHQPDZGSENUKWAH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XRHGBTMI9YKAJKVELRWWKJAPKMSYMJTDYMZ9PJEEYIRXRMMFLRSFSHIXUL9NEJABLRUGHJFL9RASMSKOI9VCFRZ9GWTMODUUESIJBHWWHZYCLDENBFSJQPIOYC9MBGOOXSWEMLVU9L9WJXKZKVDBDMFSVHHISSSNILUMWULMVMESQUIHDGBDXROXGH9MTNFSLWJZRAPOKKRGXAAQBFPYPAAXLSTMNSNDTTJQSDQORNJS9BBGQ9KQJZYPAQ9JYQZJ9B9KQDAXUACZWRUNGMBOQLQZUHFNCKVQGORRZGAHES9PWJUKZWUJSBMNZFILBNBQQKLXITCTQDDBV9UDAOQOUPWMXTXWFWVMCXIXLRMRWMAYYQJPCEAAOFEOGZQMEDAGYGCTKUJBS9AGEXJAFHWWDZRYEN9DN9HVCMLFURISLYSWKXHJKXMHUWZXUQARMYPGKRKQMHVR9JEYXJRPNZINYNCGZHHUNHBAIJHLYZIZGGIDFWVNXZQADLEDJFTIUTQWCQSX9QNGUZXGXJYUUTFSZPQKXBA9DFRQRLTLUJENKESDGTZRGRSLTNYTITXRXRGVLWBTEWPJXZYLGHLQBAVYVOSABIVTQYQM9FIQKCBRRUEMVVTMERLWOK"; + 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 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 curlClass = Curl.class; + Constructor 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); + } + +} diff --git a/src/test/java/com/iota/iri/benchmarks/crypto/PearlDiverBenchmark.java b/src/test/java/com/iota/iri/benchmarks/crypto/PearlDiverBenchmark.java new file mode 100644 index 0000000000..9d36eb1ed3 --- /dev/null +++ b/src/test/java/com/iota/iri/benchmarks/crypto/PearlDiverBenchmark.java @@ -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 = "ZMGYJPAAYHGRDGKCUEAIZDERGWUJH9QIHEESTUAZIQDQGMREKOVZCQRKHUZPXQ9PPIBGARFTZBZCYGAWIEUGPMIEEMKVSLCCJPITK9A9V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JTKXQICGYHKQJ9QAOFACDNHRYYHDMCSHPLMVLWEXUEZDDTQJUBMBVJCO9RNASPTYVHVLHAYYGYUCRBWZZESBHEUEZIHJDNOVOFCEAZTKDXACSUPBFYCIRDVPPJTSWHM9BAVSPPGMBVZKIJBAGWZEIIUNUYOHEVEO9FYMJZXJFLXXIYFBIZYKTHNQWBJYHJPTYJPPUWDBQWJAWEYSGSFVLQYIB9TNYUSNWVVJPFVDQUONBBPQJINWXAKXHFSUQKYJBFGXCQWT9TYHMAFIWQPQI9IBEMDCRIFOZN9KFGBGH99ZSKFTLOWMNYJDBREEYQBVSMZPDVGRYNDRPERXXISDEYTLJBNTJSVTSSTMHG9HCC9PIAHWZAMDGRMZFNQKEJCW9NBFRTNRRXTOTUAJS9DKRAUZWCIUYXTUHYT9SJDSFRWGCPFOBUHHNXMWNLJJRLGMVMBISRI"; + + /** + * Benchmark Pearl Diver search function. + */ + @Benchmark + 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); + } + +}