mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
defaultcache更改为懒加载,在用户使用redis缓存时,避免了程序启动时不必要的初始化开销。 <img width="2638" height="806" alt="image" src="https://github.com/user-attachments/assets/96bb0097-8463-4303-971c-ee1a9ef671a6" /> --------- Co-authored-by: hailaz <739476267@qq.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
146 lines
2.8 KiB
Go
146 lines
2.8 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// 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,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
// go test *.go -bench=".*" -benchmem
|
|
|
|
package gcache_test
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/os/gcache"
|
|
)
|
|
|
|
var (
|
|
localCache = gcache.New()
|
|
localCacheLru = gcache.NewWithAdapter(gcache.NewAdapterMemoryLru(10000))
|
|
)
|
|
|
|
func Benchmark_CacheSet(b *testing.B) {
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
i := 0
|
|
for pb.Next() {
|
|
localCache.Set(ctx, i, i, 0)
|
|
i++
|
|
}
|
|
})
|
|
}
|
|
|
|
func Benchmark_CacheGet(b *testing.B) {
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
i := 0
|
|
for pb.Next() {
|
|
localCache.Get(ctx, i)
|
|
i++
|
|
}
|
|
})
|
|
}
|
|
|
|
func Benchmark_CacheRemove(b *testing.B) {
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
i := 0
|
|
for pb.Next() {
|
|
localCache.Remove(ctx, i)
|
|
i++
|
|
}
|
|
})
|
|
}
|
|
|
|
func Benchmark_CacheLruSet(b *testing.B) {
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
i := 0
|
|
for pb.Next() {
|
|
localCacheLru.Set(ctx, i, i, 0)
|
|
i++
|
|
}
|
|
})
|
|
}
|
|
|
|
func Benchmark_CacheLruGet(b *testing.B) {
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
i := 0
|
|
for pb.Next() {
|
|
localCacheLru.Get(ctx, i)
|
|
i++
|
|
}
|
|
})
|
|
}
|
|
|
|
func Benchmark_CacheLruRemove(b *testing.B) {
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
i := 0
|
|
for pb.Next() {
|
|
localCacheLru.Remove(context.TODO(), i)
|
|
i++
|
|
}
|
|
})
|
|
}
|
|
|
|
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)
|
|
}
|
|
})
|
|
}
|