@@ -6,6 +6,7 @@ package expvar
6
6
7
7
import (
8
8
"bytes"
9
+ "crypto/sha1"
9
10
"encoding/json"
10
11
"fmt"
11
12
"net"
@@ -299,6 +300,26 @@ func BenchmarkMapSetDifferent(b *testing.B) {
299
300
})
300
301
}
301
302
303
+ // BenchmarkMapSetDifferentRandom simulates such a case where the concerned
304
+ // keys of Map.Set are generated dynamically and as a result insertion is
305
+ // out of order and the number of the keys may be large.
306
+ func BenchmarkMapSetDifferentRandom (b * testing.B ) {
307
+ keys := make ([]string , 100 )
308
+ for i := range keys {
309
+ keys [i ] = fmt .Sprintf ("%x" , sha1 .Sum ([]byte (fmt .Sprint (i ))))
310
+ }
311
+
312
+ v := new (Int )
313
+ b .ResetTimer ()
314
+
315
+ for i := 0 ; i < b .N ; i ++ {
316
+ m := new (Map ).Init ()
317
+ for _ , k := range keys {
318
+ m .Set (k , v )
319
+ }
320
+ }
321
+ }
322
+
302
323
func BenchmarkMapSetString (b * testing.B ) {
303
324
m := new (Map ).Init ()
304
325
@@ -350,6 +371,25 @@ func BenchmarkMapAddDifferent(b *testing.B) {
350
371
})
351
372
}
352
373
374
+ // BenchmarkMapAddDifferentRandom simulates such a case where that the concerned
375
+ // keys of Map.Add are generated dynamically and as a result insertion is out of
376
+ // order and the number of the keys may be large.
377
+ func BenchmarkMapAddDifferentRandom (b * testing.B ) {
378
+ keys := make ([]string , 100 )
379
+ for i := range keys {
380
+ keys [i ] = fmt .Sprintf ("%x" , sha1 .Sum ([]byte (fmt .Sprint (i ))))
381
+ }
382
+
383
+ b .ResetTimer ()
384
+
385
+ for i := 0 ; i < b .N ; i ++ {
386
+ m := new (Map ).Init ()
387
+ for _ , k := range keys {
388
+ m .Add (k , 1 )
389
+ }
390
+ }
391
+ }
392
+
353
393
func BenchmarkMapAddSameSteadyState (b * testing.B ) {
354
394
m := new (Map ).Init ()
355
395
b .RunParallel (func (pb * testing.PB ) {
0 commit comments