github.com/zhongdalu/gf@v1.0.0/g/os/gcache/gcache_z_bench_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gcache_test
    10  
    11  import (
    12  	"github.com/zhongdalu/gf/g/os/gcache"
    13  	"testing"
    14  )
    15  
    16  var (
    17  	cache    = gcache.New()
    18  	cacheLru = gcache.New(10000)
    19  )
    20  
    21  func Benchmark_CacheSet(b *testing.B) {
    22  	b.RunParallel(func(pb *testing.PB) {
    23  		i := 0
    24  		for pb.Next() {
    25  			cache.Set(i, i, 0)
    26  			i++
    27  		}
    28  	})
    29  }
    30  
    31  func Benchmark_CacheGet(b *testing.B) {
    32  	b.RunParallel(func(pb *testing.PB) {
    33  		i := 0
    34  		for pb.Next() {
    35  			cache.Get(i)
    36  			i++
    37  		}
    38  	})
    39  }
    40  
    41  func Benchmark_CacheRemove(b *testing.B) {
    42  	b.RunParallel(func(pb *testing.PB) {
    43  		i := 0
    44  		for pb.Next() {
    45  			cache.Remove(i)
    46  			i++
    47  		}
    48  	})
    49  }
    50  
    51  func Benchmark_CacheLruSet(b *testing.B) {
    52  	b.RunParallel(func(pb *testing.PB) {
    53  		i := 0
    54  		for pb.Next() {
    55  			cacheLru.Set(i, i, 0)
    56  			i++
    57  		}
    58  	})
    59  }
    60  
    61  func Benchmark_CacheLruGet(b *testing.B) {
    62  	b.RunParallel(func(pb *testing.PB) {
    63  		i := 0
    64  		for pb.Next() {
    65  			cacheLru.Get(i)
    66  			i++
    67  		}
    68  	})
    69  }
    70  
    71  func Benchmark_CacheLruRemove(b *testing.B) {
    72  	b.RunParallel(func(pb *testing.PB) {
    73  		i := 0
    74  		for pb.Next() {
    75  			cacheLru.Remove(i)
    76  			i++
    77  		}
    78  	})
    79  }