Skip to content

Commit 61d26f8

Browse files
committed
Add microbenchmarks of BinaryUtil.{from,to}Hex
1 parent 53e1950 commit 61d26f8

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

yubico-util/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55
signing
66
id("info.solidsoft.pitest")
77
id("io.github.cosmicsilence.scalafix")
8+
id("me.champeau.jmh") version "0.6.8"
89
}
910

1011
description = "Yubico internal utilities"
@@ -36,6 +37,13 @@ dependencies {
3637
testImplementation("org.scalatest:scalatest_2.13")
3738
testImplementation("org.scalatestplus:junit-4-13_2.13")
3839
testImplementation("org.scalatestplus:scalacheck-1-16_2.13")
40+
41+
jmhImplementation(platform(project(":test-platform")))
42+
jmhRuntimeOnly("org.slf4j:slf4j-nop")
43+
}
44+
45+
configurations.jmhRuntimeClasspath {
46+
exclude(module = "slf4j-test")
3947
}
4048

4149

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.yubico.internal.util.benchmark;
2+
3+
import com.yubico.internal.util.BinaryUtil;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.concurrent.TimeUnit;
7+
import java.util.stream.Collectors;
8+
import org.openjdk.jmh.annotations.Benchmark;
9+
import org.openjdk.jmh.annotations.Measurement;
10+
import org.openjdk.jmh.annotations.Warmup;
11+
import org.openjdk.jmh.infra.Blackhole;
12+
13+
@Warmup(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS)
14+
@Measurement(iterations = 10, time = 100, timeUnit = TimeUnit.MILLISECONDS)
15+
public class BinaryUtilBenchmark {
16+
17+
private static final class FromHexTests {
18+
public static final List<String> shortTests =
19+
Arrays.asList(
20+
"", "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d",
21+
"0e", "0f", "10", "20", "30", "40", "50", "60", "70", "80", "90", "a0", "b0", "c0",
22+
"d0", "e0", "f0", "ff");
23+
24+
public static final String long128 =
25+
"05a32393cf6c4a162796e83360fe4cbc4e24bf0e89a0288d8594edda689e188379fb7d3d1e0dc7b6f5afaffcce40640c3a6bf197225f82039a568c3f232321cc3307edd7c10ff80f21fd5d1dd588054614db3a30d660e537143ee4604d5006a89226d9a0abd57e3108348d22f4dfd1c0ea3e2fb3d20f673f51b295809414c8bd";
26+
public static final String long256 =
27+
"9a21145c579178e21973096ba131bb0af24b122350ed15b585eee634231fe8c0b16c1920bf76b6e100572c462856a1dcdaaa6d2023e895cd74f8db0e1189d5a264840ec7a7f59011b13725d2ede8fe4f813cdfa9e4cb67953f6f609cd694e82567585b88e72276170b69e7531cf2f7378e3ce0fc5ec7c28c00c179d5c7cc621007b93bb658dd9d07ee0d5a8307bc26c9fddc7daa50ec0d4e751dec29649b50051967aa3c360e78c6d1abb32d1143cd706a50b5e353a1fc1f690b2c5bcfd188813c7338ed231784ef1f67e08f1b08925718bb0bd3ef3f37365c4c344672915008d20d37e6a3d7a779f95d6be0fffd7cf0ba5fb7ea4da0ae0c997102621e3f4841";
28+
public static final String long512 =
29+
"d6f686693781aad51a2cf40d1bfe4985f7dc094fbfa480023529b72602e376e9f51242176a16096ace8742807e6b2d5aae534762131fbd00b7adc9f6f8c70a29356f792cbf8b869a8265752c67658cd7566afa3c701889ba9e9b089200002ee69c7cf533d03d7077f31dc270de83e3a64a4102efdd5defb1e244ad2515b7f6043a7b34240dfd359ecc564aba8df30a9e41f36b9a7439a4b11304219dbfecd77cb30f69352276b816ac6ec5663b6fd72a7dbff03e9501b0dae97424abd6de101a150cdc446f7ad12ee578b62f1a468c8adcf717969b8c0580b453a920141a58ba2eaea32bf5a24fda5b4f7dabea985c0dec754d75683252f6a23623ce9248d7974075bd4eb6dc585ab516fa189cdcc987bde0548e4a06aac2682c3e41f23c53046933bcf62def3504d0f472066aa0c7ea9d2652aaa9e62d126a787292480617f09e8b75eef77bfe4fb0620366f406c83503f81f5778ca241137296ab9378a0201ddec60250330640fb9958cbb9c4e3cf0f0a78e0157882f393fce0bf9536cdae09390db232fc5d6902eb824c6ae5a8fd944898b9a0ead90e22e6b37d2c3e054ee50225f154fea740a38db7e857aeed6ca06fc7060d6a1e352969d26c6ee0966b855cb44ad22f5974c57992fca17d3d67c8132886c199bdff6eed11b3078e4805723742767af4d9f4ab04dcb176a2657adc7d28cc8b881fda344592d0a0f1ef34c";
30+
public static final String long1024 =
31+
"8a454aaa25b4e1f93e28145579aecef9bcb7abcb593957db4993b018d871903ab9c33eeb3004addbe33abb411ff3d659d0806274001f2a5d15f371280fece5c531c232d751c9eb82b46d140aced518dcba0c10226130887036bba2b9cb8bd2523e9a26f92bd438ade5367cbb2e7982243dc81f3af5705507c664f1bad84bbfe719bea97e1c5c8973b05176fe0a9bc497a0497ce4c6e8decfda7be928c6114a3302bc64e90ca903f6c8b4d42042508d2af828a893492c8ef5d04a1f166426227fcce07412c9a66259c349a4139728b3c288f1b2d08660d7482e3151a028ecbf41a6a4eba848e67076fbbf3a9109ce2e7386a83c76dbcac34948bcfd6094f53cb8181cbb32981afd8018e88c40524002a5b8415ee81582daa50c4e85b4ea3501878ae5bed8ce4fe8af9f1ed0e45aefee1cd743521025db3d2f907a6552c9e79800e67af100b22f3e4ea051060bc2e65d9ceeb113d6af8c41847c54421f533a6ee1908ecd013a169a566349e8ea05a2e5d93d6903da28088e09416d119b47363a8c7b33657257ff99f1ea225a087ee774d0efa7d002dfeff0707972b8c4dd4e5628719f3ae75c145b038f44b0032542bb0c00564c0d6d75ffb3c1a6976a6f6c766be92396ba53d8ba46d296820cde9096f4b089aa9090062d9b9af9823b9fc2078c6d15fba1b024aa90bcc174c6269718dc56002c32bf4ad62a13583db2aac4a28bd131f94ab78478f8f8d7a5819b936f1eeb1a519f16368a2741bb04005e61e592b2e2f0ee35a81eae58c5417801dda5d44a7d3b2887907e4c708b9f11e5d449484e6532de6068c01af9d5f2e4ebd1873bd457e4afcedbec1d9459c8772c9db7cfb83beecdf68977fd5319eeb00666b49dacd5b947549bc757a07a8ee8404a41bf44c87dd553ff88f536471175abd92f7448f2ee9c7860ec6059a8d53e1a8490c734557a141167d3f37e871de27706417ac786f0edee32651631931c9d3156a9667d09dbf5a77a3f894500599ba5eeb7be531aa63b29d87e7439fda3e85d05ca1b4412036b7fbb44b684920c219d3edcf56ce40d30f877167a014de32c71a82e767d8478394edf172288772e8bdf8968c22c9dc27c89ba69e5c68f165be133f25567cf91e74cdf472b31d7f68b4b189522f47498c4089ce356b123e1a5d3a87e7faba6ca7ff8699bc137de6161c12c21916f6017903e762fb34a383f9e1a3705e1bcd6fb0307cf1434d1a86da69ba237488e8c64bcadf419ad6722d695e835b33a450ee71d4db1237a7b26d414ccd963ba4cd0b31c63e68fc953bb51b824c2776de68dd95d41be3bac154d1c3776f88f371cb8b1b8f489fb84e6bf0d0a6cb74e1a9280f1d04a3c845033cae8f75c612659a3733c0d094487b039a483bf6ab66f27e39b950b8bda0cd4d0aac83d149c59c804b1f1abff5ae4aa54e88df6e8106d3f";
32+
public static final String long2048 =
33+
"77d3a29ca97179f409b407de4442f429a6f336ca638c5e702f192ddbdd92ebc127b5a9fcb0e71f211adcb5a55c06cb5b7be38e69bc68e23e6b9ab968e7bedd77516fb5fb4c59cbe8ad59ca163c2e7a829ec025b23e87724911f093a61dd23b86c8acb8c164c3567a89f9e32280c8f689997d790a6a22c1a2ea85a488587a889452a6c0764b205c8063db6c350521a8921034d058c33412a532b35f6cc42240150d5f1634a2918211a0de42097acde1eb3d562fc7c1294f2eb2ddd741832db6e7e77488389147164ff3c46f76ddfb181d080e5e8e0ffd9592bd45f6865470204aa4550c559f456e6c974bfe5dc435d8357971c63267f2a7bfe1649988b8273c2e9c8eb22a277197ce1477d21af24e6663de01aa11f439234968734312d355af88de4abffea68737af127cdc4b7f7a2d8b9789fb18469aff29f746fb981fca589b1992c03f477df26191cdc09c1251b94159ef19f0468a128899e1d6b8caefa0d6314627a8b594b062cfad0c5f557ded49e3b0a1b34f9f0eba37e513a6c7cf8fd45e22dcde01ef029f622bd1773c6cf882b82cab32fc8f37d485488a7acdda49781c0a4b53ee4ee10b2e8ccc6c452a5337586f168730b251e0edd2f9999f38dd0cbf5425ece00bb89b649068219ce4ebcca83b8d6630d2dcdd02f9d3aa6900f64e4ac999d9638977855d55ca3da12fdda22d988a0be6e9b450b927e92a4398fda8a1d9216616dc49c52f41044a01b4ce9113df65a50a3c4c0175b8a17e98b4a88e0b064e9b242fa6fa2e05e5894ee22ed82a66682348ca079e8fac16487eb2822ac67c77a1bb8d644dbf6b542c376dc19ad9f304ffc3f091eae942225340a89382b3d0f3d8edc9a8e8d4af813262c4fffb22ea84262e530645f91a0f2bb394f620dc9367ddd5649b524125456a84c1c8c64e6dbfa34f06bade952a92fe40bfa13e35a05e2aa7b2895576a5052af99a6d46b96dfe1e35a5c1723a1ed459828364af1ef5c6be4fb5f97e9b047030ae3908b27854564230878ee57e00c8288ee64249a1e1fc4a8536e32432163d355284db58ce0ef0a3e4e499c703081b86061790e2267c27fedf09a8446a1c8d8d68d5099502838547e9ec984e4713f0449a79d80cd15cd079478616e164915e780899666bf82dd508799ba5de00c3497c536b550888f355c4978f8f2ea2202c4ca7397d26a56e24d338e90af1b0458656462c4efebd47b3287c15e3ce99b10eac7680b0745659080dd7dac83f8ed1dace494c0018b1c671e8594d585574ed3bda263f7e616cbb970abd9ca54279d849ea9afe444ae0a658394d23f08dc8ad95956e40cef7741751a1ab775dd08e1d4c3834ab91a210392addd4647ecdfede03029d814967299b286add415a5c7078518f51a691a6302764f93c98471f9903a694e7b41e8c87a7985b0f23d9d4aa57ac6971777ff1f957058c41b51f4deb7225e3ef4cef334e06f90d43f2339ffc319fcb891cb64dd43d1cf1ccb10f25a9c717b4090168c4efb8e4539831fe48bd85df13849c48d9d26e448c369c51cd55de2e490c3aa2378725738b472251edf3e3e13e021783455d91ce2a66074b0f17e66a8c7fee8fa1df80f79b5ebcc0dc970fa7ed6f782d65182b349ff3c04a5e81472905d28efb6e1a003b9671a08416dcba67c18e02d9e8e9ea018d31dd981ffaa4e23c6656fa8ca05ec5c428e58ea9a530d863173e906bfec30be25dd61ad4f227d157bca31782ad26cd956c79016332a059e36fdb319591718778c047f129bf35136dab0012c2e5564a6cbd3d848d14de00316270160cae34d2255a7597573dc829ab2d11dff26c4c2442c6b37fa9b19a813af79e508ec32129355e47b062b7e392805faa47969b4ef520cc5c6b70a5eb57b11a0fec8b582b901bf1f70534037486e895205c05c3b9ce2eb6b0109bf74ae34d70820baf12eea1ef7da2dd2b0bd28c3beddfc08f56e4c16caf9aba86fa92d58fb85c17ddba36b867e63cdce69201ec9730a4058cbdac1a0a6e363f2e1601d5fb59474513902585af12334466d21ee5873aa8b352e1cf315f5c8fa3bd94210ac4866be795fbf789eefb38a799a796e5b466a32802dc51a6e80356af68c465e16bd86f26e12aaa41406707a6915f9f4449386f54b294d99e5b795fc3a651bc3b1ba365e835798ee7b99cfe98e2265a92abb2fd3067b3d90938d3a27f651eb82c6324181a0fecd3d7b847da04647130376b76603919dc4bc5bff3c51a7e8fff6ee9a4584f47f6352886bc7740ef536312848f0d40f444a3b4b4a611b058f174827007fede7df9da2f8759d8ef9d20d3444ef8d47b7477ee6316339d0b14f8545a1abd14902048173bae1cde1a455180a986a4428fd0e29c980266eba4b7c6fc80910dee6d43bd4163c3091121af38e173008add1d93ec00d9ea31fcb756000f6f59c78f6fb2d2c1a3e0644e81563e4ae678b5cbb9cc585f6f88c53ab866f423570764ab497ace4b89c7f5d9fefd49fbdcc2d3c9e35a369ae814821095b19ded47739107f219000dced7eff7a84a14e91e50d011b39ee1f2bd9c2a6b70686595e3e1d18bae9544d6157589352b0903ad22377f7155f8b0f0c67f12c3338732660fc113c81e65d93133523936727b3905c46eaec4dd9e3b9515871a7a551dfa0aee8d7391269e10153bf2dab86483aaf7aca5e86dbe501efd2aa8b5208fd55b4f0cdd53addb5db1f55593d3f698e63cab1c87868fe99f1fe91d149a3e7de7f2726bf7a1893f49291091a024d8c72d93da4fa051187d83e2ba755bb909157633ce5e4ceeeaaabd10c70be8a3401a15643e060dec472c605774ee6b4bdeb7ea264bedf33db21b18b5d876a818148da98a89a27447fbbf6aaa3cbd484f9c45b759d6d7c2eaf74d01";
34+
}
35+
36+
private static final class ToHexTests {
37+
public static final List<byte[]> shortTests =
38+
FromHexTests.shortTests.stream().map(BinaryUtil::fromHex).collect(Collectors.toList());
39+
40+
public static final byte[] long128 = BinaryUtil.fromHex(FromHexTests.long128);
41+
public static final byte[] long256 = BinaryUtil.fromHex(FromHexTests.long256);
42+
public static final byte[] long512 = BinaryUtil.fromHex(FromHexTests.long512);
43+
public static final byte[] long1024 = BinaryUtil.fromHex(FromHexTests.long1024);
44+
public static final byte[] long2048 = BinaryUtil.fromHex(FromHexTests.long2048);
45+
}
46+
47+
@Benchmark
48+
public void fromHexShort(Blackhole bh) {
49+
for (String s : FromHexTests.shortTests) {
50+
bh.consume(BinaryUtil.fromHex(s));
51+
}
52+
}
53+
54+
@Benchmark
55+
public void fromHexLong128(Blackhole bh) {
56+
bh.consume(BinaryUtil.fromHex(FromHexTests.long128));
57+
}
58+
59+
@Benchmark
60+
public void fromHexLong256(Blackhole bh) {
61+
bh.consume(BinaryUtil.fromHex(FromHexTests.long256));
62+
}
63+
64+
@Benchmark
65+
public void fromHexLong512(Blackhole bh) {
66+
bh.consume(BinaryUtil.fromHex(FromHexTests.long512));
67+
}
68+
69+
@Benchmark
70+
public void fromHexLong1024(Blackhole bh) {
71+
bh.consume(BinaryUtil.fromHex(FromHexTests.long1024));
72+
}
73+
74+
@Benchmark
75+
public void fromHexLong2048(Blackhole bh) {
76+
bh.consume(BinaryUtil.fromHex(FromHexTests.long2048));
77+
}
78+
79+
@Benchmark
80+
public void toHexShort(Blackhole bh) {
81+
for (byte[] b : ToHexTests.shortTests) {
82+
bh.consume(BinaryUtil.toHex(b));
83+
}
84+
}
85+
86+
@Benchmark
87+
public void toHexLong128(Blackhole bh) {
88+
bh.consume(BinaryUtil.toHex(ToHexTests.long128));
89+
}
90+
91+
@Benchmark
92+
public void toHexLong256(Blackhole bh) {
93+
bh.consume(BinaryUtil.toHex(ToHexTests.long256));
94+
}
95+
96+
@Benchmark
97+
public void toHexLong512(Blackhole bh) {
98+
bh.consume(BinaryUtil.toHex(ToHexTests.long512));
99+
}
100+
101+
@Benchmark
102+
public void toHexLong1024(Blackhole bh) {
103+
bh.consume(BinaryUtil.toHex(ToHexTests.long1024));
104+
}
105+
106+
@Benchmark
107+
public void toHexLong2048(Blackhole bh) {
108+
bh.consume(BinaryUtil.toHex(ToHexTests.long2048));
109+
}
110+
}

0 commit comments

Comments
 (0)