Skip to content

Instantly share code, notes, and snippets.

@Fullstop000
Created April 4, 2020 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fullstop000/5d4222273afd963d209272d3fc8d8ac6 to your computer and use it in GitHub Desktop.
Save Fullstop000/5d4222273afd963d209272d3fc8d8ac6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
"testing"
cmap "github.com/orcaman/concurrent-map"
)
func BenchmarkConcurrentWrite(b *testing.B) {
for _, c := range []int{10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("sync.Map%d", c), func(b *testing.B) {
for i := 0; i < b.N; i++ {
sm := sync.Map{}
wg := sync.WaitGroup{}
for j := 0; j < c; j++ {
key := fmt.Sprintf("key%d", j)
wg.Add(1)
go func(k string) {
sm.Store(k, "value")
wg.Done()
}(key)
}
wg.Wait()
}
})
b.Run(fmt.Sprintf("cmap%d", c), func(b *testing.B) {
for i := 0; i < b.N; i++ {
cm := cmap.New()
wg := sync.WaitGroup{}
for j := 0; j < c; j++ {
key := fmt.Sprintf("key%d", j)
wg.Add(1)
go func(k string) {
cm.Set(k, "value")
wg.Done()
}(key)
}
}
})
}
}
func BenchmarkConcurrentRead(b *testing.B) {
for _, c := range []int{10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("sync.Map%d", c), func(b *testing.B) {
sm := sync.Map{}
wg := sync.WaitGroup{}
for i := 0; i < c; i++ {
key := fmt.Sprintf("key%d", i)
value := fmt.Sprintf("value%d", i)
wg.Add(1)
go func() {
sm.Store(key, value)
wg.Done()
}()
}
wg.Wait()
for i := 0; i < b.N; i++ {
for j := 0; j < 2*c; j++ {
key := fmt.Sprintf("key%d", j%c)
wg.Add(1)
go func() {
_, ok := sm.Load(key)
if !ok {
panic(key)
}
wg.Done()
}()
}
wg.Wait()
}
})
b.Run(fmt.Sprintf("cmap%d", c), func(b *testing.B) {
cm := cmap.New()
wg := sync.WaitGroup{}
for i := 0; i < c; i++ {
key := fmt.Sprintf("key%d", i)
value := fmt.Sprintf("value%d", i)
wg.Add(1)
go func() {
cm.Set(key, value)
wg.Done()
}()
}
wg.Wait()
for i := 0; i < b.N; i++ {
for j := 0; j < 2*c; j++ {
key := fmt.Sprintf("key%d", j%c)
wg.Add(1)
go func() {
_, ok := cm.Get(key)
if !ok {
panic(key)
}
wg.Done()
}()
}
wg.Wait()
}
})
}
}
@Fullstop000
Copy link
Author

goos: linux
goarch: amd64
pkg: cmap
BenchmarkConcurrentWrite/sync.Map10-32         	  157424	      7851 ns/op	    1463 B/op	      65 allocs/op
BenchmarkConcurrentWrite/cmap10-32             	  103605	     12445 ns/op	    5891 B/op	      95 allocs/op
BenchmarkConcurrentWrite/sync.Map100-32        	   14396	     83223 ns/op	   15402 B/op	     612 allocs/op
BenchmarkConcurrentWrite/cmap100-32            	   15769	     77398 ns/op	   13688 B/op	     297 allocs/op
BenchmarkConcurrentWrite/sync.Map1000-32       	     691	   1760499 ns/op	  197051 B/op	    6066 allocs/op
BenchmarkConcurrentWrite/cmap1000-32           	    1648	    785324 ns/op	  169034 B/op	    2232 allocs/op
BenchmarkConcurrentWrite/sync.Map10000-32      	      81	  15682511 ns/op	 1786362 B/op	   61225 allocs/op
BenchmarkConcurrentWrite/cmap10000-32          	     152	   7724982 ns/op	 1459553 B/op	   20628 allocs/op
BenchmarkConcurrentWrite/sync.Map100000-32     	       6	 171937900 ns/op	19459064 B/op	  635083 allocs/op
BenchmarkConcurrentWrite/cmap100000-32         	      15	  73736500 ns/op	12224135 B/op	  202173 allocs/op

@Fullstop000
Copy link
Author

Fullstop000 commented Apr 4, 2020

goos: linux
goarch: amd64
pkg: cmap
BenchmarkConcurrentRead/sync.Map10-32         	  158725	      8387 ns/op	     289 B/op	      38 allocs/op
BenchmarkConcurrentRead/cmap10-32             	  152773	      7773 ns/op	     288 B/op	      38 allocs/op
BenchmarkConcurrentRead/sync.Map100-32        	   12218	     99383 ns/op	    3193 B/op	     398 allocs/op
BenchmarkConcurrentRead/cmap100-32            	   13064	     92989 ns/op	    3192 B/op	     398 allocs/op
BenchmarkConcurrentRead/sync.Map1000-32       	     922	   1310089 ns/op	   32558 B/op	    4009 allocs/op
BenchmarkConcurrentRead/cmap1000-32           	     914	   1337196 ns/op	   32634 B/op	    4004 allocs/op
BenchmarkConcurrentRead/sync.Map10000-32      	      81	  13844117 ns/op	  351851 B/op	   41158 allocs/op
BenchmarkConcurrentRead/cmap10000-32          	      85	  13823916 ns/op	  342371 B/op	   40595 allocs/op
BenchmarkConcurrentRead/sync.Map100000-32     	       6	 180707933 ns/op	 7119746 B/op	  558035 allocs/op
BenchmarkConcurrentRead/cmap100000-32         	       7	 154268071 ns/op	 5519739 B/op	  471748 allocs/op
PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment