From 6500e55492f785879cde767831f88663cec297a0 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sat, 22 Jun 2019 17:25:05 +0200 Subject: [PATCH] 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 b6b74cff0..deddc6b55 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)) + } + } +}