@@ -160,3 +160,144 @@ func testInlinedPubKeyAddedOnRetrieve(kb pstore.KeyBook) func(t *testing.T) {
160
160
}
161
161
}
162
162
}
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