github.com/wangyougui/gf/v2@v2.6.5/container/gmap/gmap_z_bench_syncmap_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 gm file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gmap_test
    10  
    11  import (
    12  	"sync"
    13  	"testing"
    14  
    15  	"github.com/wangyougui/gf/v2/container/gmap"
    16  )
    17  
    18  var gm = gmap.NewIntIntMap(true)
    19  
    20  var sm = sync.Map{}
    21  
    22  func Benchmark_GMapSet(b *testing.B) {
    23  	b.RunParallel(func(pb *testing.PB) {
    24  		i := 0
    25  		for pb.Next() {
    26  			gm.Set(i, i)
    27  			i++
    28  		}
    29  	})
    30  }
    31  
    32  func Benchmark_SyncMapSet(b *testing.B) {
    33  	b.RunParallel(func(pb *testing.PB) {
    34  		i := 0
    35  		for pb.Next() {
    36  			sm.Store(i, i)
    37  			i++
    38  		}
    39  	})
    40  }
    41  
    42  func Benchmark_GMapGet(b *testing.B) {
    43  	b.RunParallel(func(pb *testing.PB) {
    44  		i := 0
    45  		for pb.Next() {
    46  			gm.Get(i)
    47  			i++
    48  		}
    49  	})
    50  }
    51  
    52  func Benchmark_SyncMapGet(b *testing.B) {
    53  	b.RunParallel(func(pb *testing.PB) {
    54  		i := 0
    55  		for pb.Next() {
    56  			sm.Load(i)
    57  			i++
    58  		}
    59  	})
    60  }
    61  
    62  func Benchmark_GMapRemove(b *testing.B) {
    63  	b.RunParallel(func(pb *testing.PB) {
    64  		i := 0
    65  		for pb.Next() {
    66  			gm.Remove(i)
    67  			i++
    68  		}
    69  	})
    70  }
    71  
    72  func Benchmark_SyncMapRmove(b *testing.B) {
    73  	b.RunParallel(func(pb *testing.PB) {
    74  		i := 0
    75  		for pb.Next() {
    76  			sm.Delete(i)
    77  			i++
    78  		}
    79  	})
    80  }