diff --git a/README.md b/README.md index cd8c58e..3125bd1 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,24 @@ func main() { } ``` +## Benchmarks + +```bash +go test -bench=. -benchmem +``` + +``` +goos: darwin +goarch: arm64 +pkg: github.com/floatdrop/ringchan +cpu: Apple M1 Pro +BenchmarkSingleSender-10 7097070 167.3 ns/op 0 B/op 0 allocs/op +BenchmarkParallelSenders-10 4145682 295.0 ns/op 0 B/op 0 allocs/op +PASS +coverage: 90.9% of statements +ok github.com/floatdrop/ringchan 3.050s +``` + ## License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/ringchan_test.go b/ringchan_test.go index 1f3f8bc..78fd5c5 100644 --- a/ringchan_test.go +++ b/ringchan_test.go @@ -76,3 +76,33 @@ func TestRingChanRangeAfterClose(t *testing.T) { t.Errorf("unexpected results: %v", results) } } + +func BenchmarkSingleSender(b *testing.B) { + input := make(chan int) + ring := New(input, 1) + b.ResetTimer() + for i := 0; i < b.N; i++ { + input <- i + } + close(input) + for range ring.C { + } + b.StopTimer() +} + +func BenchmarkParallelSenders(b *testing.B) { + input := make(chan int) + ring := New(input, 1) + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + input <- 1 + } + }) + + close(input) + for range ring.C { + } + b.StopTimer() +}