mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
update
This commit is contained in:
@ -2,7 +2,9 @@ package maps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@ -65,3 +67,57 @@ func BenchmarkMap(b *testing.B) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkLoadStore(b *testing.B) {
|
||||
ms := []Map[string, string]{
|
||||
NewRWMap[string, string](),
|
||||
NewConcurrentMap[string, string](),
|
||||
NewHashMap[string, string]().Synchronize(),
|
||||
NewHashMap[string, string](),
|
||||
}
|
||||
|
||||
for _, m := range ms {
|
||||
var i int64
|
||||
b.Run(fmt.Sprintf("%T", m), func(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
gid := int(atomic.AddInt64(&i, 1) - 1)
|
||||
|
||||
if gid == 0 {
|
||||
m.Set("0", strconv.Itoa(n))
|
||||
} else {
|
||||
m.Get("0")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkLoadStoreCollision(b *testing.B) {
|
||||
ms := []Map[string, string]{
|
||||
NewRWMap[string, string](),
|
||||
NewConcurrentMap[string, string](),
|
||||
NewHashMap[string, string]().Synchronize(),
|
||||
// &sync.Map{},
|
||||
}
|
||||
|
||||
// 测试对于同一个 key 的 n-1 并发读和 1 并发写的性能
|
||||
for _, m := range ms {
|
||||
b.Run(fmt.Sprintf("%T", m), func(b *testing.B) {
|
||||
var i int64
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
// 记录并发执行的 goroutine id
|
||||
gid := int(atomic.AddInt64(&i, 1) - 1)
|
||||
|
||||
if gid == 0 {
|
||||
for i := 0; pb.Next(); i++ {
|
||||
m.Set("0", strconv.Itoa(i))
|
||||
}
|
||||
} else {
|
||||
for pb.Next() {
|
||||
m.Get("0")
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user