Golang

Go Mutex Comparision

Mutex vs RWMutex vs sync.Map, Concurrency map benchmark in different concurrent scenarios.

Go Mutex Comparision

Mutex vs RWMutex vs sync.Map, Concurrency map benchmark in different concurrent scenarios.

Methods

Check all the methods and code here benchmark/methods.go

1. sync.Map

Write

syncMap.Store(key, key)

Random Read

v, ok := syncMap.Load(key)
if !ok {
}
v = v

Range Read All

syncMap.Range(func(k, v interface{}) bool {
    k = k
    v = v
    return true
})

2. sync.Mutex

Write

mutex.Lock()
mutexMap[key] = key
mutex.Unlock()

Random Read

mutex.Lock()
v, ok := mutexMap[key]
if !ok {
}
v = v
mutex.Unlock()

Range Read All

mutex.Lock()
for k, v := range mutexMap {
    k = k
    v = v
}
mutex.Unlock()

3. sync.RWMutex

Write

rwMutex.Lock()
rwMutexMap[key] = key
rwMutex.Unlock()

Random Read

rwMutex.RLock()
v, ok := rwMutexMap[key]
if !ok {
}
v = v
rwMutex.RUnlock()

Range Read All

rwMutex.RLock()
for k, v := range rwMutexMap {
    k = k
    v = v
}
rwMutex.RUnlock()

4. orcaman/concurrent-map

Write

cMap.Set(key, key)

Random Read

v, ok := cMap.Get(key)
if !ok {
}
v = v

Range Read All

for i := range cMap.IterBuffered() {
    i = i
}

Test scenarios

Check all the test scenarios here benchmark/scenarios.go

10 concurrents 100k write

MethodTestTime
orcaman/concurrent-mapwrite (10 concurrents) (100000 iters)62.288221ms
RWMutexwrite (10 concurrents) (100000 iters)113.321432ms
Mutexwrite (10 concurrents) (100000 iters)121.2995ms
sync.Mapwrite (10 concurrents) (100000 iters)203.216039ms

10 concurrents 100k random read

MethodTestTime
orcaman/concurrent-maprandom_read (10 concurrents) (100000 iters)15.110771ms
RWMutexrandom_read (10 concurrents) (100000 iters)30.6894ms
sync.Maprandom_read (10 concurrents) (100000 iters)38.139147ms
Mutexrandom_read (10 concurrents) (100000 iters)85.278314ms

10 concurrents 100 range read all

MethodTestTime
RWMutexrange_read (10 concurrents) (100 iters)143.054227ms
Mutexrange_read (10 concurrents) (100 iters)905.993286ms
sync.Maprange_read (10 concurrents) (100 iters)949.263514ms
orcaman/concurrent-maprange_read (10 concurrents) (100 iters)2.366558255s

50 concurrents 20k write

MethodTestTime
sync.Mapwrite (50 concurrents) (20000 iters)22.123396ms
orcaman/concurrent-mapwrite (50 concurrents) (20000 iters)30.682173ms
Mutexwrite (50 concurrents) (20000 iters)122.591997ms
RWMutexwrite (50 concurrents) (20000 iters)134.832297ms

50 concurrents 20k random read

MethodTestTime
sync.Maprandom_read (50 concurrents) (20000 iters)7.055942ms
orcaman/concurrent-maprandom_read (50 concurrents) (20000 iters)12.597161ms
RWMutexrandom_read (50 concurrents) (20000 iters)32.372341ms
Mutexrandom_read (50 concurrents) (20000 iters)118.862146ms

50 concurrents 20 range read all

MethodTestTime
RWMutexrange_read (50 concurrents) (20 iters)104.010416ms
sync.Maprange_read (50 concurrents) (20 iters)577.479455ms
Mutexrange_read (50 concurrents) (20 iters)863.322488ms
orcaman/concurrent-maprange_read (50 concurrents) (20 iters)1.715899163s

100 concurrents 10k write

MethodTestTime
sync.Mapwrite (100 concurrents) (100000 iters)260.946224ms
orcaman/concurrent-mapwrite (100 concurrents) (100000 iters)349.440739ms
Mutexwrite (100 concurrents) (100000 iters)1.630196524s
RWMutexwrite (100 concurrents) (100000 iters)1.680058588s

100 concurrents 10k random read

MethodTestTime
sync.Maprandom_read (100 concurrents) (100000 iters)119.658652ms
orcaman/concurrent-maprandom_read (100 concurrents) (100000 iters)126.84391ms
RWMutexrandom_read (100 concurrents) (100000 iters)280.359023ms
Mutexrandom_read (100 concurrents) (100000 iters)1.644604438s

100 concurrents 10 range read all

MethodTestTime
RWMutexrange_read (100 concurrents) (10 iters)95.519575ms
sync.Maprange_read (100 concurrents) (10 iters)836.398188ms
Mutexrange_read (100 concurrents) (10 iters)972.755566ms
orcaman/concurrent-maprange_read (100 concurrents) (10 iters)2.795446594s