Skip to content
This repository was archived by the owner on Aug 19, 2022. It is now read-only.

Commit f74a212

Browse files
committed
add keybook benchmarks
License: MIT Signed-off-by: Adrian Lanzafame <[email protected]>
1 parent 4e66aa6 commit f74a212

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

pstoreds/ds_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ func TestDsKeyBook(t *testing.T) {
6363
}
6464
}
6565

66+
func BenchmarkDsKeyBook(b *testing.B) {
67+
for name, dsFactory := range dstores {
68+
b.Run(name, func(b *testing.B) {
69+
pt.BenchmarkKeyBook(b, keyBookFactory(b, dsFactory, DefaultOpts()))
70+
})
71+
}
72+
}
73+
6674
func BenchmarkDsPeerstore(b *testing.B) {
6775
caching := DefaultOpts()
6876
caching.CacheSize = 1024

pstoremem/inmem_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ func BenchmarkInMemoryPeerstore(b *testing.B) {
3030
return NewPeerstore(), nil
3131
}, "InMem")
3232
}
33+
34+
func BenchmarkInMemoryKeyBook(b *testing.B) {
35+
pt.BenchmarkKeyBook(b, func() (pstore.KeyBook, func()) {
36+
return NewKeyBook(), nil
37+
})
38+
}

test/keybook_suite.go

+141
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,144 @@ func testInlinedPubKeyAddedOnRetrieve(kb pstore.KeyBook) func(t *testing.T) {
160160
}
161161
}
162162
}
163+
164+
var keybookBenchmarkSuite = map[string]func(kb pstore.KeyBook) func(*testing.B){
165+
"PubKey": benchmarkPubKey,
166+
"AddPubKey": benchmarkAddPubKey,
167+
"PrivKey": benchmarkPrivKey,
168+
"AddPrivKey": benchmarkAddPrivKey,
169+
"PeersWithKeys": benchmarkPeersWithKeys,
170+
}
171+
172+
func BenchmarkKeyBook(b *testing.B, factory KeyBookFactory) {
173+
ordernames := make([]string, 0, len(keybookBenchmarkSuite))
174+
for name := range keybookBenchmarkSuite {
175+
ordernames = append(ordernames, name)
176+
}
177+
sort.Strings(ordernames)
178+
for _, name := range ordernames {
179+
bench := keybookBenchmarkSuite[name]
180+
kb, closeFunc := factory()
181+
182+
b.Run(name, bench(kb))
183+
184+
if closeFunc != nil {
185+
closeFunc()
186+
}
187+
}
188+
}
189+
190+
func benchmarkPubKey(kb pstore.KeyBook) func(*testing.B) {
191+
return func(b *testing.B) {
192+
_, pub, err := pt.RandTestKeyPair(512)
193+
if err != nil {
194+
b.Error(err)
195+
}
196+
197+
id, err := peer.IDFromPublicKey(pub)
198+
if err != nil {
199+
b.Error(err)
200+
}
201+
202+
err = kb.AddPubKey(id, pub)
203+
if err != nil {
204+
b.Fatal(err)
205+
}
206+
207+
b.ResetTimer()
208+
for i := 0; i < b.N; i++ {
209+
kb.PubKey(id)
210+
}
211+
}
212+
}
213+
214+
func benchmarkAddPubKey(kb pstore.KeyBook) func(*testing.B) {
215+
return func(b *testing.B) {
216+
_, pub, err := pt.RandTestKeyPair(512)
217+
if err != nil {
218+
b.Error(err)
219+
}
220+
221+
id, err := peer.IDFromPublicKey(pub)
222+
if err != nil {
223+
b.Error(err)
224+
}
225+
226+
b.ResetTimer()
227+
for i := 0; i < b.N; i++ {
228+
kb.AddPubKey(id, pub)
229+
}
230+
}
231+
}
232+
233+
func benchmarkPrivKey(kb pstore.KeyBook) func(*testing.B) {
234+
return func(b *testing.B) {
235+
priv, _, err := pt.RandTestKeyPair(512)
236+
if err != nil {
237+
b.Error(err)
238+
}
239+
240+
id, err := peer.IDFromPrivateKey(priv)
241+
if err != nil {
242+
b.Error(err)
243+
}
244+
245+
err = kb.AddPrivKey(id, priv)
246+
if err != nil {
247+
b.Fatal(err)
248+
}
249+
250+
b.ResetTimer()
251+
for i := 0; i < b.N; i++ {
252+
kb.PrivKey(id)
253+
}
254+
}
255+
}
256+
257+
func benchmarkAddPrivKey(kb pstore.KeyBook) func(*testing.B) {
258+
return func(b *testing.B) {
259+
priv, _, err := pt.RandTestKeyPair(512)
260+
if err != nil {
261+
b.Error(err)
262+
}
263+
264+
id, err := peer.IDFromPrivateKey(priv)
265+
if err != nil {
266+
b.Error(err)
267+
}
268+
269+
b.ResetTimer()
270+
for i := 0; i < b.N; i++ {
271+
kb.AddPrivKey(id, priv)
272+
}
273+
}
274+
}
275+
276+
func benchmarkPeersWithKeys(kb pstore.KeyBook) func(*testing.B) {
277+
return func(b *testing.B) {
278+
for i := 0; i < 10; i++ {
279+
priv, pub, err := pt.RandTestKeyPair(512)
280+
if err != nil {
281+
b.Error(err)
282+
}
283+
284+
id, err := peer.IDFromPublicKey(pub)
285+
if err != nil {
286+
b.Error(err)
287+
}
288+
289+
err = kb.AddPubKey(id, pub)
290+
if err != nil {
291+
b.Fatal(err)
292+
}
293+
err = kb.AddPrivKey(id, priv)
294+
if err != nil {
295+
b.Fatal(err)
296+
}
297+
}
298+
b.ResetTimer()
299+
for i := 0; i < b.N; i++ {
300+
kb.PeersWithKeys()
301+
}
302+
}
303+
}

0 commit comments

Comments
 (0)