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"
|
2019-06-19 09:06:52 +08:00
|
|
|
"testing"
|
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()
|
|
|
|
localCacheLru = gcache.New(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
|
|
|
}
|