Skip to content

Commit

Permalink
Merge pull request lxzan#12 from lxzan/dev
Browse files Browse the repository at this point in the history
update benchmark
  • Loading branch information
lxzan committed Nov 29, 2023
2 parents 94bca3a + 853e2f6 commit 288873c
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func BenchmarkMemoryCache_Set(b *testing.B) {
memorycache.WithBucketNum(128),
memorycache.WithBucketSize(1000, 10000),
)
var i = atomic.Int64{}
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
for pb.Next() {
index := i.Add(1) % benchcount
mc.Set(benchkeys[index], 1, time.Hour)
Expand All @@ -43,24 +43,47 @@ func BenchmarkMemoryCache_Get(b *testing.B) {
mc.Set(benchkeys[i%benchcount], 1, time.Hour)
}

var i = atomic.Int64{}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
for pb.Next() {
index := i.Add(1) % benchcount
mc.Get(benchkeys[index])
}
})
}

func BenchmarkMemoryCache_SetAndGet(b *testing.B) {
var mc = memorycache.New(
memorycache.WithBucketNum(128),
memorycache.WithBucketSize(1000, 10000),
)
for i := 0; i < benchcount; i++ {
mc.Set(benchkeys[i%benchcount], 1, time.Hour)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
for pb.Next() {
index := i.Add(1) % benchcount
if index&7 == 0 {
mc.Set(benchkeys[index], 1, time.Hour)
} else {
mc.Get(benchkeys[index])
}
}
})
}

func BenchmarkRistretto_Set(b *testing.B) {
var mc, _ = ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
var i = atomic.Int64{}
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
for pb.Next() {
index := i.Add(1) % benchcount
mc.SetWithTTL(benchkeys[index], 1, 1, time.Hour)
Expand All @@ -78,12 +101,36 @@ func BenchmarkRistretto_Get(b *testing.B) {
mc.SetWithTTL(benchkeys[i%benchcount], 1, 1, time.Hour)
}

var i = atomic.Int64{}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
for pb.Next() {
index := i.Add(1) % benchcount
mc.Get(benchkeys[index])
}
})
}

func BenchmarkRistretto_SetAndGet(b *testing.B) {
var mc, _ = ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
for i := 0; i < benchcount; i++ {
mc.SetWithTTL(benchkeys[i%benchcount], 1, 1, time.Hour)
}

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i = atomic.Int64{}
for pb.Next() {
index := i.Add(1) % benchcount
if index&7 == 0 {
mc.SetWithTTL(benchkeys[index], 1, 1, time.Hour)
} else {
mc.Get(benchkeys[index])
}
}
})
}

0 comments on commit 288873c

Please sign in to comment.