Skip to content

Commit b7143c2

Browse files
authored
Merge pull request #7 from SebUndefined/lastfeatures
WIP: benchmarks & README tweaks
2 parents 0ee95e3 + a7ed5a2 commit b7143c2

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ test-race: ## run tests with race detector
1919
go test -race -count=1 ./...
2020

2121
test-race-cover: ## run tests with race detector and coverage
22-
go test -race -count=1 -coverprofile=coverage.out ./...
22+
go test -race -count=1 -coverprofile=coverage.out ./...
23+
24+
test-bench: ## run benchmark tests
25+
go test -bench -v ./...
26+
27+
test-bench-mem: ## with mem and allocation
28+
go test -bench=. -benchmem -benchtime=2s ./...

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ kind of in-process system (adding config, per subscriber strategy...)
9292
- Clean abstractions with good defaults
9393
- Plays well with tests, mocks, and small services
9494

95+
## 📈 Benchmark
96+
97+
```shell
98+
go test -bench=. -benchmem -benchtime=2s ./...
99+
goos: darwin
100+
goarch: arm64
101+
pkg: github.com/sebundefined/thebus
102+
cpu: Apple M4 Pro
103+
BenchmarkBus_Publish_NoSubscriber-14 62964634 38.04 ns/op 6729.34 MB/s 0 B/op 0 allocs/op
104+
BenchmarkBus_Publish_CopyOnPublish_NoSubscriber-14 62382220 38.36 ns/op 6673.82 MB/s 0 B/op 0 allocs/op
105+
BenchmarkBus_Publish_OneSubscriber-14 60785077 40.02 ns/op 6397.49 MB/s 0 B/op 0 allocs/op
106+
BenchmarkBus_Publish_CopyOnPublish_OneSubscriber-14 60166773 39.96 ns/op 6406.85 MB/s 0 B/op 0 allocs/op
107+
```
108+
95109
## 🧪 Testing
96110

97111
Run the full suite:

bench_test.go

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,110 @@
1-
package thebus
1+
package thebus_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/sebundefined/thebus"
8+
)
9+
10+
func BenchmarkBus_Publish_NoSubscriber(b *testing.B) {
11+
bus, _ := thebus.New()
12+
b.Cleanup(func() {
13+
_ = bus.Close()
14+
})
15+
16+
payload := make([]byte, 256)
17+
b.ReportAllocs()
18+
b.SetBytes(int64(len(payload)))
19+
20+
b.ResetTimer()
21+
for i := 0; i < b.N; i++ {
22+
_, err := bus.Publish("benchmark/no-subscriber", payload)
23+
if err != nil {
24+
b.Fatalf("publish fail: %s", err)
25+
}
26+
}
27+
}
28+
29+
func BenchmarkBus_Publish_CopyOnPublish_NoSubscriber(b *testing.B) {
30+
bus, _ := thebus.New(thebus.WithCopyOnPublish(true))
31+
b.Cleanup(func() {
32+
_ = bus.Close()
33+
})
34+
35+
payload := make([]byte, 256)
36+
b.ReportAllocs()
37+
b.SetBytes(int64(len(payload)))
38+
39+
b.ResetTimer()
40+
for i := 0; i < b.N; i++ {
41+
_, err := bus.Publish("benchmark/no-subscriber", payload)
42+
if err != nil {
43+
b.Fatalf("publish fail: %s", err)
44+
}
45+
}
46+
}
47+
48+
func BenchmarkBus_Publish_OneSubscriber(b *testing.B) {
49+
bus, _ := thebus.New()
50+
b.Cleanup(func() {
51+
_ = bus.Close()
52+
})
53+
ctx, cancelFunc := context.WithCancel(context.Background())
54+
b.Cleanup(cancelFunc)
55+
56+
subscriber, err := bus.Subscribe(ctx, "benchmark/one-subscriber",
57+
thebus.WithBufferSize(1<<16),
58+
thebus.WithDropIfFull(true))
59+
if err != nil {
60+
b.Fatalf("subscribe fail: %s", err)
61+
}
62+
go func() {
63+
for range subscriber.Read() {
64+
65+
}
66+
}()
67+
payload := make([]byte, 256)
68+
b.ReportAllocs()
69+
b.SetBytes(int64(len(payload)))
70+
71+
b.ResetTimer()
72+
for i := 0; i < b.N; i++ {
73+
_, err := bus.Publish("benchmark/no-subscriber", payload)
74+
if err != nil {
75+
b.Fatalf("publish fail: %s", err)
76+
}
77+
}
78+
}
79+
80+
func BenchmarkBus_Publish_CopyOnPublish_OneSubscriber(b *testing.B) {
81+
bus, _ := thebus.New(thebus.WithCopyOnPublish(true))
82+
b.Cleanup(func() {
83+
_ = bus.Close()
84+
})
85+
ctx, cancelFunc := context.WithCancel(context.Background())
86+
b.Cleanup(cancelFunc)
87+
88+
subscriber, err := bus.Subscribe(ctx, "benchmark/one-subscriber",
89+
thebus.WithBufferSize(1<<16),
90+
thebus.WithDropIfFull(true))
91+
if err != nil {
92+
b.Fatalf("subscribe fail: %s", err)
93+
}
94+
go func() {
95+
for range subscriber.Read() {
96+
97+
}
98+
}()
99+
payload := make([]byte, 256)
100+
b.ReportAllocs()
101+
b.SetBytes(int64(len(payload)))
102+
103+
b.ResetTimer()
104+
for i := 0; i < b.N; i++ {
105+
_, err := bus.Publish("benchmark/no-subscriber", payload)
106+
if err != nil {
107+
b.Fatalf("publish fail: %s", err)
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)