Skip to content

Commit

Permalink
add some benchmark test.
Browse files Browse the repository at this point in the history
  • Loading branch information
werbenhu committed Aug 8, 2023
1 parent 23b1733 commit dafa2af
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 47 deletions.
212 changes: 212 additions & 0 deletions cowmap_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package eventbus

import (
"strconv"
"sync"
"testing"
)

func BenchmarkSingleGoroutineStoreAbsentByMap(b *testing.B) {
m := make(map[string]any)
b.ResetTimer()
for i := 0; i < b.N; i++ {
m[strconv.Itoa(i)] = "value"
}
}

func BenchmarkSingleGoroutineStoreAbsentBySyncMap(b *testing.B) {
var m sync.Map
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store(strconv.Itoa(i), "value")
}
}

func BenchmarkSingleGoroutineStoreAbsentByCowMap(b *testing.B) {
m := NewCowMap()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store(strconv.Itoa(i), "value")
}
}

func BenchmarkSingleGoroutineStorePresentByMap(b *testing.B) {
m := make(map[string]any)
m["key"] = "value"
b.ResetTimer()
for i := 0; i < b.N; i++ {
m["key"] = "value"
}
}

func BenchmarkSingleGoroutineStorePresentBySyncMap(b *testing.B) {
var m sync.Map
m.Store("key", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store("key", "value")
}
}

func BenchmarkSingleGoroutineStorePresentByCowMap(b *testing.B) {
m := NewCowMap()
m.Store("key", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store("key", "value")
}
}

func BenchmarkMultiGoroutineLoadDifferentBySyncMap(b *testing.B) {
var m sync.Map
finished := make(chan struct{}, b.N)
for i := 0; i < 1000; i++ {
m.Store(i, i)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
for j := 0; j < 1000; j++ {
m.Load(j)
}
finished <- struct{}{}
}()
}
for i := 0; i < b.N; i++ {
<-finished
}
}

func BenchmarkMultiGoroutineLoadDifferentByCowMap(b *testing.B) {
m := NewCowMap()
finished := make(chan struct{}, b.N)
for i := 0; i < 1000; i++ {
m.Store(i, i)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
for j := 0; j < 1000; j++ {
m.Load(j)
}
finished <- struct{}{}
}()
}
for i := 0; i < b.N; i++ {
<-finished
}
}

func BenchmarkMultiGoroutineLoadSameBySyncMap(b *testing.B) {
var m sync.Map
finished := make(chan struct{}, b.N)
m.Store("key", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
for i := 0; i < 10; i++ {
m.Load("key")
}
finished <- struct{}{}
}()
}
for i := 0; i < b.N; i++ {
<-finished
}
}

func BenchmarkMultiGoroutineLoadSameByCowMap(b *testing.B) {
m := NewCowMap()
finished := make(chan struct{}, b.N)
m.Store("key", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
for i := 0; i < 10; i++ {
m.Load("key")
}
finished <- struct{}{}
}()
}
for i := 0; i < b.N; i++ {
<-finished
}
}

func Benchmark_CowMapStore(b *testing.B) {
m := NewCowMap()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store(i, strconv.Itoa(i))
}
}

func Benchmark_CowMapLoad(b *testing.B) {
m := NewCowMap()
for i := 0; i < 1000; i++ {
m.Store(i, strconv.Itoa(i))
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Load(i % 1000)
}
}

func Benchmark_SyncMapStore(b *testing.B) {
var m sync.Map
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store(i, strconv.Itoa(i))
}
}

func Benchmark_SyncMapLoad(b *testing.B) {
var m sync.Map
for i := 0; i < 1000; i++ {
m.Store(i, strconv.Itoa(i))
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Load(i % 1000)
}
}

// func BenchmarkMultiGoroutineStoreDifferentBySyncMap(b *testing.B) {
// var m sync.Map
// finished := make(chan struct{}, b.N)

// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// go func(k string, v any) {
// for i := 0; i < 10; i++ {
// m.Store(k, v)
// }
// finished <- struct{}{}
// }(strconv.Itoa(i), "value")
// }
// for i := 0; i < b.N; i++ {
// <-finished
// }
// }

// func BenchmarkMultiGoroutineStoreDifferentByCowMap(b *testing.B) {
// m := NewCowMap()
// finished := make(chan struct{}, b.N)

// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// go func(k string, v any) {
// for i := 0; i < 10; i++ {
// m.Store(k, v)
// }
// finished <- struct{}{}
// }(strconv.Itoa(i), "value")
// }
// for i := 0; i < b.N; i++ {
// <-finished
// }
// }
20 changes: 0 additions & 20 deletions cowmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,23 +246,3 @@ func Test_CowMapStoreAndLoadConcurrent(t *testing.T) {
loadWg.Wait()
})
}

func Benchmark_CowMapStore(b *testing.B) {
m := NewCowMap()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store(i, strconv.Itoa(i))
}
}

func Benchmark_CowMapLoad(b *testing.B) {
m := NewCowMap()
for i := 0; i < 100; i++ {
m.Store(i, strconv.Itoa(i))
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Load(i % 100)
}
}
43 changes: 43 additions & 0 deletions pipe_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package eventbus

import (
"testing"
)

func BenchmarkPipePublishSync(b *testing.B) {
pipe := NewPipe[int]()

pipe.Subscribe(pipeHandlerOne)

b.ResetTimer()
for i := 0; i < b.N; i++ {
pipe.PublishSync(i)
}
}

func BenchmarkPipePublish(b *testing.B) {
pipe := NewPipe[int]()

pipe.Subscribe(pipeHandlerOne)

b.ResetTimer()
for i := 0; i < b.N; i++ {
pipe.Publish(i)
}
}

func BenchmarkPipeGoChannel(b *testing.B) {
ch := make(chan int)

go func() {
for {
val := <-ch
pipeHandlerOne(val)
}
}()

b.ResetTimer()
for i := 0; i < b.N; i++ {
ch <- i
}
}
27 changes: 0 additions & 27 deletions pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,3 @@ func Test_PipeClose(t *testing.T) {
assert.Equal(t, ErrChannelClosed, err)
p.Close()
}

func BenchmarkPipePublish(b *testing.B) {
pipe := NewPipe[int]()

pipe.Subscribe(pipeHandlerOne)

b.ResetTimer()
for i := 0; i < b.N; i++ {
pipe.PublishSync(i)
}
}

func BenchmarkPipeGoChannel(b *testing.B) {
ch := make(chan int)

go func() {
for {
val := <-ch
pipeHandlerOne(val)
}
}()

b.ResetTimer()
for i := 0; i < b.N; i++ {
ch <- i
}
}

0 comments on commit dafa2af

Please sign in to comment.