Files
gf/g/os/gcache/gcache_z_bench_test.go

114 lines
1.9 KiB
Go
Raw Normal View History

// Copyright 2017 gf Author(https://github.com/gogf/gf). 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,
// 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 (
2019-06-19 09:06:52 +08:00
"github.com/gogf/gf/g/os/gcache"
"sync"
"testing"
2018-02-12 15:33:19 +08:00
)
var (
2019-06-19 09:06:52 +08:00
c = gcache.New()
clru = gcache.New(10000)
mInt = make(map[int]int)
mMap = make(map[interface{}]interface{})
2019-06-19 09:06:52 +08:00
muInt = sync.RWMutex{}
muMap = sync.RWMutex{}
)
func Benchmark_CacheSet(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
c.Set(i, i, 0)
}
2018-02-12 15:33:19 +08:00
}
func Benchmark_CacheGet(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
c.Get(i)
}
2018-02-12 15:33:19 +08:00
}
func Benchmark_CacheRemove(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
c.Remove(i)
}
2018-09-18 00:01:10 +08:00
}
func Benchmark_CacheLruSet(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
clru.Set(i, i, 0)
}
2018-09-18 00:01:10 +08:00
}
func Benchmark_CacheLruGet(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
clru.Get(i)
}
2018-09-18 00:01:10 +08:00
}
func Benchmark_CacheLruRemove(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
clru.Remove(i)
}
}
func Benchmark_InterfaceMapWithLockSet(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
muMap.Lock()
mMap[i] = i
muMap.Unlock()
}
}
func Benchmark_InterfaceMapWithLockGet(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
muMap.RLock()
if _, ok := mMap[i]; ok {
2019-06-19 09:06:52 +08:00
}
muMap.RUnlock()
}
}
func Benchmark_InterfaceMapWithLockRemove(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
muMap.Lock()
delete(mMap, i)
muMap.Unlock()
}
}
func Benchmark_IntMapWithLockWithLockSet(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
muInt.Lock()
mInt[i] = i
muInt.Unlock()
}
}
func Benchmark_IntMapWithLockGet(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
muInt.RLock()
if _, ok := mInt[i]; ok {
2019-06-19 09:06:52 +08:00
}
muInt.RUnlock()
}
}
func Benchmark_IntMapWithLockRemove(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
muInt.Lock()
delete(mInt, i)
muInt.Unlock()
}
2018-09-18 00:01:10 +08:00
}