@Anurag @muskan_sethia The value of b.N remains the same in one iteration. So the benchmark written is fine.
The value is missing after sets because ristretto can choose to drop keys in case of a high load. The setbuf here ristretto/cache.go at 8f368f2f2ab3a54cbe62fb9772cd75ce55e07802 · dgraph-io/ristretto · GitHub will drop keys if there is high load (like a benchmark)
@muskan_sethia The following benchmark runs fine. Set slowInsertions if you don’t want cache to drop anything.
func BenchmarkCacheSetWithRistretto(b *testing.B) {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 10000000 * 10,
MaxCost: 10000000,
BufferItems: 64,
})
if err != nil {
panic(err)
}
failedSets := 0
// Set slow insertions if you want all the entries to be inserted.
slowInsertions := true
for n := 0; n < b.N; n++ {
if slowInsertions {
time.Sleep(time.Microsecond)
}
key := strconv.Itoa(n%1000000) + strconv.Itoa(n%1000000)
// Cache.Set returns a boolean denoting if the set was successful or
// not. The cache can choose to drop keys in times of high load.
if !cache.SetWithTTL(key, "value", 1, time.Duration(5*time.Second)) {
failedSets++
}
}
// All sets should be succesful if we're inserting slowing.
if slowInsertions {
require.Zero(b, failedSets)
}
time.Sleep(50 * time.Millisecond)
notFoundCount := 0
for n := 0; n < b.N; n++ {
_, found := cache.Get(strconv.Itoa(n%1000000) + strconv.Itoa(n%1000000))
if !found {
notFoundCount++
}
}
require.Equal(b, failedSets, notFoundCount)
}