github.com/gogf/gf/v2@v2.7.4/os/gcache/gcache_z_bench_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gcache_test
    10  
    11  import (
    12  	"context"
    13  	"testing"
    14  
    15  	"github.com/gogf/gf/v2/os/gcache"
    16  )
    17  
    18  var (
    19  	localCache    = gcache.New()
    20  	localCacheLru = gcache.New(10000)
    21  )
    22  
    23  func Benchmark_CacheSet(b *testing.B) {
    24  	b.RunParallel(func(pb *testing.PB) {
    25  		i := 0
    26  		for pb.Next() {
    27  			localCache.Set(ctx, i, i, 0)
    28  			i++
    29  		}
    30  	})
    31  }
    32  
    33  func Benchmark_CacheGet(b *testing.B) {
    34  	b.RunParallel(func(pb *testing.PB) {
    35  		i := 0
    36  		for pb.Next() {
    37  			localCache.Get(ctx, i)
    38  			i++
    39  		}
    40  	})
    41  }
    42  
    43  func Benchmark_CacheRemove(b *testing.B) {
    44  	b.RunParallel(func(pb *testing.PB) {
    45  		i := 0
    46  		for pb.Next() {
    47  			localCache.Remove(ctx, i)
    48  			i++
    49  		}
    50  	})
    51  }
    52  
    53  func Benchmark_CacheLruSet(b *testing.B) {
    54  	b.RunParallel(func(pb *testing.PB) {
    55  		i := 0
    56  		for pb.Next() {
    57  			localCacheLru.Set(ctx, i, i, 0)
    58  			i++
    59  		}
    60  	})
    61  }
    62  
    63  func Benchmark_CacheLruGet(b *testing.B) {
    64  	b.RunParallel(func(pb *testing.PB) {
    65  		i := 0
    66  		for pb.Next() {
    67  			localCacheLru.Get(ctx, i)
    68  			i++
    69  		}
    70  	})
    71  }
    72  
    73  func Benchmark_CacheLruRemove(b *testing.B) {
    74  	b.RunParallel(func(pb *testing.PB) {
    75  		i := 0
    76  		for pb.Next() {
    77  			localCacheLru.Remove(context.TODO(), i)
    78  			i++
    79  		}
    80  	})
    81  }