From 128b42ebe3cd31230ebea5c3fc18fb518b109fbb Mon Sep 17 00:00:00 2001 From: Hypo Date: Tue, 12 Apr 2022 08:13:58 +0800 Subject: [PATCH] update: api & benchmark --- README.md | 56 ++++++++++++++++++++++++++-------------- bench_test.go | 6 ++--- bus/bus.go | 26 +++++++++---------- bus/bus_test.go | 26 +++++++++---------- example/compute/main.go | 2 +- example/wildcard/main.go | 4 +-- 6 files changed, 68 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 133bdda..4958da9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # eventbus -Lightweight eventbus for golang +EventBus is a very useful tool for event-driven style code. You can use events to chain different logic together without coupling components ## Usage @@ -19,7 +19,7 @@ func aAndBComputer(a, b int) { } func main() { - _, _ = bus.Register("op.int.and", aAndBComputer) + _, _ = bus.Subscribe("op.int.and", aAndBComputer) bus.Publish("op.int.and", 1, 2) } ``` @@ -43,8 +43,8 @@ func aliceDoSomething() { } func main() { - _, _ = bus.Register("partner.bob.do", bobDoSomething) - _, _ = bus.Register("partner.alice.do", aliceDoSomething) + _, _ = bus.Subscribe("partner.bob.do", bobDoSomething) + _, _ = bus.Subscribe("partner.alice.do", aliceDoSomething) bus.Publish("partner.*.do") } ``` @@ -60,34 +60,50 @@ pkg: github.com/hyponet/eventbus cpu: Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz BenchmarkBusPublish -BenchmarkBusPublish-12 10000 2058 ns/op 518 B/op 15 allocs/op +BenchmarkBusPublish-2 10000 2954 ns/op 746 B/op 15 allocs/op +BenchmarkBusPublish-4 10000 1624 ns/op 497 B/op 15 allocs/op +BenchmarkBusPublish-8 10000 1333 ns/op 497 B/op 15 allocs/op BenchmarkBusPublish -BenchmarkBusPublish-12 100000 1819 ns/op 500 B/op 15 allocs/op +BenchmarkBusPublish-2 100000 2369 ns/op 543 B/op 15 allocs/op +BenchmarkBusPublish-4 100000 1358 ns/op 497 B/op 15 allocs/op +BenchmarkBusPublish-8 100000 1380 ns/op 497 B/op 15 allocs/op BenchmarkBusPublish -BenchmarkBusPublish-12 1000000 1918 ns/op 498 B/op 15 allocs/op +BenchmarkBusPublish-2 1000000 2202 ns/op 504 B/op 15 allocs/op +BenchmarkBusPublish-4 1000000 1419 ns/op 497 B/op 15 allocs/op +BenchmarkBusPublish-8 1000000 1466 ns/op 497 B/op 15 allocs/op BenchmarkBusPublish -BenchmarkBusPublish-12 10000000 1695 ns/op 497 B/op 15 allocs/op +BenchmarkBusPublish-2 10000000 1992 ns/op 498 B/op 15 allocs/op +BenchmarkBusPublish-4 10000000 1408 ns/op 497 B/op 15 allocs/op +BenchmarkBusPublish-8 10000000 1507 ns/op 497 B/op 15 allocs/op ``` -### Register +### Subscribe ```bash goos: darwin goarch: amd64 pkg: github.com/hyponet/eventbus cpu: Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz -BenchmarkBusRegister -BenchmarkBusRegister-12 10000 2604 ns/op 962 B/op 16 allocs/op - -BenchmarkBusRegister -BenchmarkBusRegister-12 100000 3451 ns/op 951 B/op 16 allocs/op - -BenchmarkBusRegister -BenchmarkBusRegister-12 1000000 3212 ns/op 1074 B/op 16 allocs/op - -BenchmarkBusRegister -BenchmarkBusRegister-12 10000000 3303 ns/op 1016 B/op 16 allocs/op +BenchmarkBusSubscribe +BenchmarkBusSubscribe-2 10000 2682 ns/op 962 B/op 16 allocs/op +BenchmarkBusSubscribe-4 10000 2521 ns/op 983 B/op 16 allocs/op +BenchmarkBusSubscribe-8 10000 2762 ns/op 1196 B/op 16 allocs/op + +BenchmarkBusSubscribe +BenchmarkBusSubscribe-2 100000 2831 ns/op 951 B/op 16 allocs/op +BenchmarkBusSubscribe-4 100000 3036 ns/op 975 B/op 16 allocs/op +BenchmarkBusSubscribe-8 100000 3066 ns/op 1087 B/op 16 allocs/op + +BenchmarkBusSubscribe +BenchmarkBusSubscribe-2 1000000 3315 ns/op 1074 B/op 16 allocs/op +BenchmarkBusSubscribe-4 1000000 3180 ns/op 1076 B/op 16 allocs/op +BenchmarkBusSubscribe-8 1000000 2518 ns/op 770 B/op 16 allocs/op + +BenchmarkBusSubscribe +BenchmarkBusSubscribe-2 10000000 3757 ns/op 1016 B/op 16 allocs/op +BenchmarkBusSubscribe-4 10000000 3489 ns/op 1013 B/op 16 allocs/op +BenchmarkBusSubscribe-8 10000000 4136 ns/op 1256 B/op 16 allocs/op ``` \ No newline at end of file diff --git a/bench_test.go b/bench_test.go index b089f43..cd23aaa 100644 --- a/bench_test.go +++ b/bench_test.go @@ -30,7 +30,7 @@ func doNothing() {} func init() { for _, topic := range topics { - _, _ = bus.Register(topic, doNothing) + _, _ = bus.Subscribe(topic, doNothing) } } @@ -41,9 +41,9 @@ func BenchmarkBusPublish(b *testing.B) { } } -func BenchmarkBusRegister(b *testing.B) { +func BenchmarkBusSubscribe(b *testing.B) { target := append(topics, wildcards...) for n := 0; n < b.N; n++ { - _, _ = bus.Register(target[n%len(target)], doNothing) + _, _ = bus.Subscribe(target[n%len(target)], doNothing) } } diff --git a/bus/bus.go b/bus/bus.go index 9c16814..3e4214c 100644 --- a/bus/bus.go +++ b/bus/bus.go @@ -19,20 +19,20 @@ type eventbus struct { mux sync.RWMutex } -func (b *eventbus) register(l *listener) { +func (b *eventbus) subscribe(l *listener) { b.mux.Lock() b.listeners[l.id] = l b.exchange.add(l.topic, l.id) b.mux.Unlock() } -func (b *eventbus) unregister(lID string) { +func (b *eventbus) unsubscribe(lID string) { b.mux.Lock() - b.unregisterWithLock(lID) + b.unsubscribeWithLock(lID) b.mux.Unlock() } -func (b *eventbus) unregisterWithLock(lID string) { +func (b *eventbus) unsubscribeWithLock(lID string) { delete(b.listeners, lID) b.exchange.remove(lID) } @@ -44,7 +44,7 @@ func (b *eventbus) publish(topic string, args ...interface{}) { for i, lID := range lIDs { needDo = append(needDo, b.listeners[lID]) if needDo[i].once { - b.unregisterWithLock(lID) + b.unsubscribeWithLock(lID) } } b.mux.Unlock() @@ -57,38 +57,38 @@ func (b *eventbus) publish(topic string, args ...interface{}) { } } -func Register(topic string, fn interface{}) (string, error) { +func Subscribe(topic string, fn interface{}) (string, error) { l, err := buildNewListener(topic, fn, false, false) if err != nil { return "", err } - evb.register(l) + evb.subscribe(l) return l.id, nil } -func RegisterOnce(topic string, fn interface{}) (string, error) { +func SubscribeOnce(topic string, fn interface{}) (string, error) { l, err := buildNewListener(topic, fn, false, true) if err != nil { return "", err } - evb.register(l) + evb.subscribe(l) return l.id, nil } -func RegisterWithBlock(topic string, fn interface{}) (string, error) { +func SubscribeWithBlock(topic string, fn interface{}) (string, error) { l, err := buildNewListener(topic, fn, true, false) if err != nil { return "", err } - evb.register(l) + evb.subscribe(l) return l.id, nil } -func Unregister(lID string) { - evb.unregister(lID) +func Unsubscribe(lID string) { + evb.unsubscribe(lID) } func Publish(topic string, args ...interface{}) { diff --git a/bus/bus_test.go b/bus/bus_test.go index e9c5593..f532521 100644 --- a/bus/bus_test.go +++ b/bus/bus_test.go @@ -16,22 +16,22 @@ var _ = Describe("TestEventBusApi", func() { } }) Describe("", func() { - It("test register handler", func() { + It("test subscribe handler", func() { Context("succeed", func() { - _, err = Register("test.topic.a", func() {}) + _, err = Subscribe("test.topic.a", func() {}) Expect(err).Should(BeNil()) - _, err = Register("test.topic.a", func(astr, bstr string) error { return nil }) + _, err = Subscribe("test.topic.a", func(astr, bstr string) error { return nil }) Expect(err).Should(BeNil()) }) Context("not func", func() { - _, err = Register("test.topic.c", "wrong val") + _, err = Subscribe("test.topic.c", "wrong val") Expect(err).ShouldNot(BeNil()) - _, err = Register("test.topic.c", nil) + _, err = Subscribe("test.topic.c", nil) Expect(err).ShouldNot(BeNil()) - _, err = Register("test.topic.c", 0) + _, err = Subscribe("test.topic.c", 0) Expect(err).ShouldNot(BeNil()) }) }) @@ -40,7 +40,7 @@ var _ = Describe("TestEventBusApi", func() { isExec = false topic = "test.topic.a" ) - _, err = Register("test.topic.a", func() { + _, err = Subscribe("test.topic.a", func() { isExec = true }) Expect(err).Should(BeNil()) @@ -51,17 +51,17 @@ var _ = Describe("TestEventBusApi", func() { return isExec == true }, time.Minute, time.Second).Should(BeTrue()) }) - It("test unregister handler", func() { + It("test unsubscribe handler", func() { var ( lID string isExec = false topic = "test.topic.a" ) - lID, err = Register("test.topic.a", func() { + lID, err = Subscribe("test.topic.a", func() { isExec = true }) Expect(err).Should(BeNil()) - Unregister(lID) + Unsubscribe(lID) Publish(topic) time.Sleep(time.Second * 5) Expect(isExec).Should(BeFalse()) @@ -101,7 +101,7 @@ var _ = Describe("TestEventBus", func() { var err error l, err = buildNewListener(topic, runFn, false, false) Expect(err).Should(BeNil()) - testBus.register(l) + testBus.subscribe(l) needRun := 1000 for i := 0; i < needRun; i++ { @@ -117,7 +117,7 @@ var _ = Describe("TestEventBus", func() { var err error l, err = buildNewListener(topic, runFn, true, false) Expect(err).Should(BeNil()) - testBus.register(l) + testBus.subscribe(l) needRun := 1000 for i := 0; i < needRun; i++ { @@ -134,7 +134,7 @@ var _ = Describe("TestEventBus", func() { var err error l, err = buildNewListener(topic, runFn, false, true) Expect(err).Should(BeNil()) - testBus.register(l) + testBus.subscribe(l) needRun := 10 for i := 0; i < needRun; i++ { diff --git a/example/compute/main.go b/example/compute/main.go index 5b73c5c..07d62c7 100644 --- a/example/compute/main.go +++ b/example/compute/main.go @@ -13,7 +13,7 @@ func aAndBComputer(a, b int) { } func main() { - _, _ = bus.Register("op.int.and", aAndBComputer) + _, _ = bus.Subscribe("op.int.and", aAndBComputer) bus.Publish("op.int.and", 1, 2) <-waiting } diff --git a/example/wildcard/main.go b/example/wildcard/main.go index db120e3..603df32 100644 --- a/example/wildcard/main.go +++ b/example/wildcard/main.go @@ -21,8 +21,8 @@ func aliceDoSomething() { } func main() { - _, _ = bus.Register("partner.bob.do", bobDoSomething) - _, _ = bus.Register("partner.alice.do", aliceDoSomething) + _, _ = bus.Subscribe("partner.bob.do", bobDoSomething) + _, _ = bus.Subscribe("partner.alice.do", aliceDoSomething) bus.Publish("partner.*.do") <-waitBob <-waitAlice