github.com/gogf/gf/v2@v2.7.4/os/gmlock/gmlock_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 gmlock_test 8 9 import ( 10 "testing" 11 12 "github.com/gogf/gf/v2/os/gmlock" 13 ) 14 15 var ( 16 lockKey = "This is the lock key for gmlock." 17 ) 18 19 func Benchmark_GMLock_Lock_Unlock(b *testing.B) { 20 for i := 0; i < b.N; i++ { 21 gmlock.Lock(lockKey) 22 gmlock.Unlock(lockKey) 23 } 24 } 25 26 func Benchmark_GMLock_RLock_RUnlock(b *testing.B) { 27 for i := 0; i < b.N; i++ { 28 gmlock.RLock(lockKey) 29 gmlock.RUnlock(lockKey) 30 } 31 } 32 33 func Benchmark_GMLock_TryLock_Unlock(b *testing.B) { 34 for i := 0; i < b.N; i++ { 35 if gmlock.TryLock(lockKey) { 36 gmlock.Unlock(lockKey) 37 } 38 } 39 } 40 41 func Benchmark_GMLock_TryRLock_RUnlock(b *testing.B) { 42 for i := 0; i < b.N; i++ { 43 if gmlock.TryRLock(lockKey) { 44 gmlock.RUnlock(lockKey) 45 } 46 } 47 }