-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSignAndVerifyPerfMain.java
More file actions
97 lines (84 loc) · 3.78 KB
/
Copy pathSignAndVerifyPerfMain.java
File metadata and controls
97 lines (84 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright 2016-2022 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.salt;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.core.Jvm;
import javax.xml.bind.DatatypeConverter;
import java.util.stream.IntStream;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
/*
Windows 10 laptop, i7-7700HQ CPU @ 2.80GHz, 2801 Mhz, 4 Core(s), 8 Logical Processor(s)
Throughput: Sign: 64013/s, Verify: 30022/s
Throughput: Sign: 68490/s, Verify: 26917/s
Centos 7, Intel(R) Core(TM) i7-7820X CPU @ 3.60GHz
Throughput: Sign: 259851/s, Verify: 103771/s
Throughput: Sign: 263146/s, Verify: 108373/s
Centos 7, Dual Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.90GHz
Throughput: Sign: 503524/s, Verify: 207797/s
Throughput: Sign: 510287/s, Verify: 198604/s
Unbuntu, Ryzen 9 59050X
Throughput: Sign: 568424/s, Verify: 302609/s
*/
public class SignAndVerifyPerfMain {
public static void main(String[] args) {
final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd";
Bytes<?> publicKey = Bytes.allocateDirect(32);
Bytes<?> secretKey = Bytes.allocateDirect(64);
BytesStore<?, ?> privateKey = fromHex(SIGN_PRIVATE);
Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
assertEquals(32, publicKey.readRemaining());
assertEquals(64, secretKey.readRemaining());
ThreadLocal<Bytes<?>> sigAndMsg = ThreadLocal.withInitial(() -> Bytes.allocateDirect(64 + 64));
ThreadLocal<Bytes<?>> sigAndMsg2 = ThreadLocal.withInitial(() -> Bytes.allocateDirect(64 + 64));
int procs = Runtime.getRuntime().availableProcessors();
for (int t = 0; t < 10; t++) {
int runs = procs * 20;
long start = System.nanoTime();
IntStream.range(0, runs).parallel().forEach(i -> {
sigAndMsg.get().writePosition(0);
Ed25519.sign(sigAndMsg.get(), secretKey, secretKey);
sigAndMsg2.get().writePosition(0);
Ed25519.sign(sigAndMsg2.get(), privateKey, secretKey);
});
long time = System.nanoTime() - start;
Bytes<?> bytes = sigAndMsg.get();
bytes.writePosition(0);
Bytes<?> bytes2 = sigAndMsg2.get();
bytes2.writePosition(0);
Ed25519.sign(bytes, secretKey, secretKey);
Ed25519.sign(bytes2, privateKey, secretKey);
long start2 = System.nanoTime();
IntStream.range(0, runs).parallel().forEach(i -> {
assertTrue(Ed25519.verify(bytes, publicKey));
assertTrue(Ed25519.verify(bytes2, publicKey));
});
long time2 = System.nanoTime() - start2;
System.out.println(
"Throughput: " + "Sign: " + (long) ((2 * runs * 1e9) / time) + "/s, " + "Verify: " + (long) ((2 * runs * 1e9) / time2) + "/s");
Jvm.pause(100);
}
}
static Bytes<?> fromHex(String s) {
byte[] byteArr = DatatypeConverter.parseHexBinary(s);
Bytes<?> bytes = Bytes.allocateDirect(byteArr.length);
bytes.write(byteArr);
return bytes;
}
}