From 6ef6b01ed3ee396a6c69419a6afcfb8e2c28fad5 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sat, 22 Jun 2019 15:28:00 +0200 Subject: [PATCH 1/4] Improve benchmarks License: MIT Signed-off-by: Jakub Sztandera --- p2p/host/eventbus/basic_test.go | 126 ++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 53 deletions(-) diff --git a/p2p/host/eventbus/basic_test.go b/p2p/host/eventbus/basic_test.go index fc23e61290..c186724692 100644 --- a/p2p/host/eventbus/basic_test.go +++ b/p2p/host/eventbus/basic_test.go @@ -363,69 +363,89 @@ func TestBothMany(t *testing.T) { testMany(t, 10000, 100, 10, false) } -func BenchmarkSubs(b *testing.B) { - b.ReportAllocs() - testMany(b, b.N, 100, 100, false) +type benchCase struct { + subs int + emits int + stateful bool } -func BenchmarkEmits(b *testing.B) { - b.ReportAllocs() - testMany(b, 100, b.N, 100, false) +func (bc benchCase) name() string { + return fmt.Sprintf("subs-%d/emits-%d/stateful-%t", bc.subs, bc.emits, bc.stateful) } -func BenchmarkMsgs(b *testing.B) { - b.ReportAllocs() - testMany(b, 100, 100, b.N, false) -} - -func BenchmarkOneToMany(b *testing.B) { - b.ReportAllocs() - testMany(b, b.N, 1, 100, false) -} - -func BenchmarkManyToOne(b *testing.B) { - b.ReportAllocs() - testMany(b, 1, b.N, 100, false) -} - -func BenchmarkMs1e2m4(b *testing.B) { - b.N = 1000000 - b.ReportAllocs() - testMany(b, 10, 100, 10000, false) +func genTestCases() []benchCase { + ret := make([]benchCase, 0, 200) + for stateful := 0; stateful < 2; stateful++ { + for subs := uint(0); subs <= 8; subs = subs + 2 { + for emits := uint(0); emits <= 8; emits = emits + 2 { + ret = append(ret, benchCase{1 << subs, 1 << emits, stateful == 1}) + } + } + } + return ret } -func BenchmarkMs1e0m6(b *testing.B) { - b.N = 10000000 - b.ReportAllocs() - testMany(b, 10, 1, 1000000, false) +func BenchmarkEvents(b *testing.B) { + for _, bc := range genTestCases() { + b.Run(bc.name(), benchMany(bc)) + } } -func BenchmarkMs0e0m6(b *testing.B) { - b.N = 1000000 - b.ReportAllocs() - testMany(b, 1, 1, 1000000, false) -} +func benchMany(bc benchCase) func(*testing.B) { + return func(b *testing.B) { + b.ReportAllocs() + subs := bc.subs + emits := bc.emits + stateful := bc.stateful + bus := NewBus() + var wait sync.WaitGroup + var ready sync.WaitGroup + wait.Add(subs + emits) + ready.Add(subs + emits) + + for i := 0; i < subs; i++ { + go func() { + sub, err := bus.Subscribe(new(EventB)) + if err != nil { + panic(err) + } + defer sub.Close() + + ready.Done() + ready.Wait() + for i := 0; i < (b.N/emits)*emits; i++ { + _, ok := <-sub.Out() + if !ok { + panic("wat") + } + } + wait.Done() + }() + } -func BenchmarkStatefulMs1e0m6(b *testing.B) { - b.N = 10000000 - b.ReportAllocs() - testMany(b, 10, 1, 1000000, true) -} + for i := 0; i < emits; i++ { + go func() { + em, err := bus.Emitter(new(EventB), func(settings interface{}) error { + settings.(*emitterSettings).makeStateful = stateful + return nil + }) + if err != nil { + panic(err) + } + defer em.Close() -func BenchmarkStatefulMs0e0m6(b *testing.B) { - b.N = 1000000 - b.ReportAllocs() - testMany(b, 1, 1, 1000000, true) -} + ready.Done() + ready.Wait() -func BenchmarkMs0e6m0(b *testing.B) { - b.N = 1000000 - b.ReportAllocs() - testMany(b, 1, 1000000, 1, false) -} + for i := 0; i < b.N/emits; i++ { + em.Emit(EventB(97)) + } -func BenchmarkMs6e0m0(b *testing.B) { - b.N = 1000000 - b.ReportAllocs() - testMany(b, 1000000, 1, 1, false) + wait.Done() + }() + } + ready.Wait() + b.ResetTimer() + wait.Wait() + } } From 12258ae84244d7a015769b937d3da28e2561d72f Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sat, 22 Jun 2019 15:48:06 +0200 Subject: [PATCH 2/4] Reduce number of samples License: MIT Signed-off-by: Jakub Sztandera --- p2p/host/eventbus/basic_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/p2p/host/eventbus/basic_test.go b/p2p/host/eventbus/basic_test.go index c186724692..ccd6bfd61e 100644 --- a/p2p/host/eventbus/basic_test.go +++ b/p2p/host/eventbus/basic_test.go @@ -376,8 +376,8 @@ func (bc benchCase) name() string { func genTestCases() []benchCase { ret := make([]benchCase, 0, 200) for stateful := 0; stateful < 2; stateful++ { - for subs := uint(0); subs <= 8; subs = subs + 2 { - for emits := uint(0); emits <= 8; emits = emits + 2 { + for subs := uint(0); subs <= 8; subs = subs + 4 { + for emits := uint(0); emits <= 8; emits = emits + 4 { ret = append(ret, benchCase{1 << subs, 1 << emits, stateful == 1}) } } From 6759096209555cbf86f3bf80e4a0945a7814ce31 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sat, 22 Jun 2019 15:50:30 +0200 Subject: [PATCH 3/4] Clean up format License: MIT Signed-off-by: Jakub Sztandera --- p2p/host/eventbus/basic_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/host/eventbus/basic_test.go b/p2p/host/eventbus/basic_test.go index ccd6bfd61e..b6b74cff00 100644 --- a/p2p/host/eventbus/basic_test.go +++ b/p2p/host/eventbus/basic_test.go @@ -370,7 +370,7 @@ type benchCase struct { } func (bc benchCase) name() string { - return fmt.Sprintf("subs-%d/emits-%d/stateful-%t", bc.subs, bc.emits, bc.stateful) + return fmt.Sprintf("subs-%03d/emits-%03d/stateful-%t", bc.subs, bc.emits, bc.stateful) } func genTestCases() []benchCase { From 6500e55492f785879cde767831f88663cec297a0 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sat, 22 Jun 2019 17:25:05 +0200 Subject: [PATCH 4/4] Add benchmarks for subscribe and emitter License: MIT Signed-off-by: Jakub Sztandera --- p2p/host/eventbus/basic_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/p2p/host/eventbus/basic_test.go b/p2p/host/eventbus/basic_test.go index b6b74cff00..deddc6b558 100644 --- a/p2p/host/eventbus/basic_test.go +++ b/p2p/host/eventbus/basic_test.go @@ -449,3 +449,36 @@ func benchMany(bc benchCase) func(*testing.B) { wait.Wait() } } + +var div = 100 + +func BenchmarkSubscribe(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N/div; i++ { + bus := NewBus() + for j := 0; j < div; j++ { + bus.Subscribe(new(EventA)) + } + } +} + +func BenchmarkEmitter(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N/div; i++ { + bus := NewBus() + for j := 0; j < div; j++ { + bus.Emitter(new(EventA)) + } + } +} + +func BenchmarkSubscribeAndEmitter(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N/div; i++ { + bus := NewBus() + for j := 0; j < div; j++ { + bus.Subscribe(new(EventA)) + bus.Emitter(new(EventA)) + } + } +}