github.com/gogf/gf/v2@v2.7.4/os/gmutex/gmutex_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 package gmutex_test 8 9 import ( 10 "sync" 11 "testing" 12 13 "github.com/gogf/gf/v2/os/gmutex" 14 ) 15 16 var ( 17 mu = sync.Mutex{} 18 rwmu = sync.RWMutex{} 19 gmu = gmutex.New() 20 ) 21 22 func Benchmark_Mutex_LockUnlock(b *testing.B) { 23 b.RunParallel(func(pb *testing.PB) { 24 for pb.Next() { 25 mu.Lock() 26 mu.Unlock() 27 } 28 }) 29 } 30 31 func Benchmark_RWMutex_LockUnlock(b *testing.B) { 32 b.RunParallel(func(pb *testing.PB) { 33 for pb.Next() { 34 rwmu.Lock() 35 rwmu.Unlock() 36 } 37 }) 38 } 39 40 func Benchmark_RWMutex_RLockRUnlock(b *testing.B) { 41 b.RunParallel(func(pb *testing.PB) { 42 for pb.Next() { 43 rwmu.RLock() 44 rwmu.RUnlock() 45 } 46 }) 47 } 48 49 func Benchmark_GMutex_LockUnlock(b *testing.B) { 50 b.RunParallel(func(pb *testing.PB) { 51 for pb.Next() { 52 gmu.Lock() 53 gmu.Unlock() 54 } 55 }) 56 } 57 58 func Benchmark_GMutex_TryLock(b *testing.B) { 59 b.RunParallel(func(pb *testing.PB) { 60 for pb.Next() { 61 if gmu.TryLock() { 62 gmu.Unlock() 63 } 64 } 65 }) 66 } 67 68 func Benchmark_GMutex_RLockRUnlock(b *testing.B) { 69 b.RunParallel(func(pb *testing.PB) { 70 for pb.Next() { 71 gmu.RLock() 72 gmu.RUnlock() 73 } 74 }) 75 } 76 77 func Benchmark_GMutex_TryRLock(b *testing.B) { 78 b.RunParallel(func(pb *testing.PB) { 79 for pb.Next() { 80 if gmu.TryRLock() { 81 gmu.RUnlock() 82 } 83 } 84 }) 85 }