github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/sync/rw_mutex_test.go (about)

     1  // Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package syncutils_test
     4  
     5  import (
     6  	syncutils "github.com/TeaOSLab/EdgeNode/internal/utils/sync"
     7  	"runtime"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestNewRWMutex(t *testing.T) {
    14  	var locker = syncutils.NewRWMutex(runtime.NumCPU())
    15  	locker.Lock(1)
    16  	t.Log(locker.TryLock(1))
    17  	locker.Unlock(1)
    18  	t.Log(locker.TryLock(1))
    19  }
    20  
    21  func BenchmarkRWMutex_Lock(b *testing.B) {
    22  	var locker = syncutils.NewRWMutex(runtime.NumCPU())
    23  
    24  	b.RunParallel(func(pb *testing.PB) {
    25  		var i = 0
    26  		for pb.Next() {
    27  			i++
    28  			locker.Lock(i)
    29  			time.Sleep(1 * time.Millisecond)
    30  			locker.Unlock(i)
    31  		}
    32  	})
    33  }
    34  
    35  func BenchmarkRWMutex_Lock2(b *testing.B) {
    36  	var locker = &sync.Mutex{}
    37  
    38  	b.RunParallel(func(pb *testing.PB) {
    39  		var i = 0
    40  		for pb.Next() {
    41  			i++
    42  			locker.Lock()
    43  			time.Sleep(1 * time.Millisecond)
    44  			locker.Unlock()
    45  		}
    46  	})
    47  }