2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-02-12 15:33:19 +08:00
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-02-12 15:33:19 +08:00
|
|
|
|
2018-09-17 09:52:24 +08:00
|
|
|
// go test *.go -bench=".*" -benchmem
|
2018-02-12 15:33:19 +08:00
|
|
|
|
2018-12-30 14:53:16 +08:00
|
|
|
package gcache_test
|
2018-02-12 15:33:19 +08:00
|
|
|
|
|
|
|
|
import (
|
2021-09-17 10:48:08 +08:00
|
|
|
"context"
|
2025-10-15 15:01:47 +08:00
|
|
|
"sync"
|
2019-06-19 09:06:52 +08:00
|
|
|
"testing"
|
2025-10-15 15:01:47 +08:00
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gcache"
|
2018-02-12 15:33:19 +08:00
|
|
|
)
|
|
|
|
|
|
2018-09-15 16:40:13 +08:00
|
|
|
var (
|
2020-08-12 20:13:13 +08:00
|
|
|
localCache = gcache.New()
|
2024-09-28 18:07:11 +08:00
|
|
|
localCacheLru = gcache.NewWithAdapter(gcache.NewAdapterMemoryLru(10000))
|
2018-09-15 16:40:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Benchmark_CacheSet(b *testing.B) {
|
2019-07-28 13:10:34 +08:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
2021-09-17 10:48:08 +08:00
|
|
|
localCache.Set(ctx, i, i, 0)
|
2019-07-28 13:10:34 +08:00
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-12 15:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-15 16:40:13 +08:00
|
|
|
func Benchmark_CacheGet(b *testing.B) {
|
2019-07-28 13:10:34 +08:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
2021-09-17 10:48:08 +08:00
|
|
|
localCache.Get(ctx, i)
|
2019-07-28 13:10:34 +08:00
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-12 15:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-15 16:40:13 +08:00
|
|
|
func Benchmark_CacheRemove(b *testing.B) {
|
2019-07-28 13:10:34 +08:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
2021-09-17 10:48:08 +08:00
|
|
|
localCache.Remove(ctx, i)
|
2019-07-28 13:10:34 +08:00
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-09-18 00:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Benchmark_CacheLruSet(b *testing.B) {
|
2019-07-28 13:10:34 +08:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
2021-09-17 10:48:08 +08:00
|
|
|
localCacheLru.Set(ctx, i, i, 0)
|
2019-07-28 13:10:34 +08:00
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-09-18 00:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Benchmark_CacheLruGet(b *testing.B) {
|
2019-07-28 13:10:34 +08:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
2021-09-17 10:48:08 +08:00
|
|
|
localCacheLru.Get(ctx, i)
|
2019-07-28 13:10:34 +08:00
|
|
|
i++
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-07-28 13:10:34 +08:00
|
|
|
})
|
2018-09-15 16:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-28 13:10:34 +08:00
|
|
|
func Benchmark_CacheLruRemove(b *testing.B) {
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
2021-09-17 10:48:08 +08:00
|
|
|
localCacheLru.Remove(context.TODO(), i)
|
2019-07-28 13:10:34 +08:00
|
|
|
i++
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-07-28 13:10:34 +08:00
|
|
|
})
|
2018-09-18 00:01:10 +08:00
|
|
|
}
|
2025-10-15 15:01:47 +08:00
|
|
|
|
|
|
|
|
var oldDefaultCache = gcache.New()
|
|
|
|
|
var newDefaultCache = sync.OnceValue(func() *gcache.Cache {
|
|
|
|
|
return gcache.New()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
func BenchmarkOldImplementation(b *testing.B) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_ = oldDefaultCache.Set(ctx, "key", "value", time.Minute)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkNewImplementation(b *testing.B) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
newDefaultCache()
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_ = newDefaultCache().Set(ctx, "key", "value", time.Minute)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkOldGet(b *testing.B) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
oldDefaultCache.Set(ctx, "test_key", "test_value", time.Minute)
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_, _ = oldDefaultCache.Get(ctx, "test_key")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkNewGet(b *testing.B) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
newDefaultCache().Set(ctx, "test_key", "test_value", time.Minute)
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_, _ = newDefaultCache().Get(ctx, "test_key")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkOldConcurrent(b *testing.B) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
_ = oldDefaultCache.Set(ctx, "key", "value", time.Minute)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkNewConcurrent(b *testing.B) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
_ = newDefaultCache().Set(ctx, "key", "value", time.Minute)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|