| 77 | } |
| 78 | } |
| 79 | func BenchmarkIterateOverCache(b *testing.B) { |
| 80 | |
| 81 | m := blob('a', 1) |
| 82 | |
| 83 | for _, shards := range []int{512, 1024, 8192} { |
| 84 | b.Run(fmt.Sprintf("%d-shards", shards), func(b *testing.B) { |
| 85 | cache, _ := New(context.Background(), Config{ |
| 86 | Shards: shards, |
| 87 | LifeWindow: 1000 * time.Second, |
| 88 | MaxEntriesInWindow: max(b.N, 100), |
| 89 | MaxEntrySize: 500, |
| 90 | }) |
| 91 | |
| 92 | for i := 0; i < b.N; i++ { |
| 93 | cache.Set(fmt.Sprintf("key-%d", i), m) |
| 94 | } |
| 95 | |
| 96 | b.ResetTimer() |
| 97 | it := cache.Iterator() |
| 98 | |
| 99 | b.RunParallel(func(pb *testing.PB) { |
| 100 | b.ReportAllocs() |
| 101 | |
| 102 | for pb.Next() { |
| 103 | if it.SetNext() { |
| 104 | it.Value() |
| 105 | } |
| 106 | } |
| 107 | }) |
| 108 | }) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func BenchmarkWriteToCacheWith1024ShardsAndSmallShardInitSize(b *testing.B) { |
| 113 | writeToCache(b, 1024, 100*time.Second, 100) |