From 6e88eec2c9c8ebf7857a9b059693f69a873fd77f Mon Sep 17 00:00:00 2001 From: Mark Kremer Date: Thu, 2 Nov 2023 18:53:06 +0100 Subject: [PATCH] Benchmark the speaker conversion from samples to bytes --- speaker/speaker.go | 8 +++++--- speaker/speaker_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 speaker/speaker_test.go diff --git a/speaker/speaker.go b/speaker/speaker.go index fc83208..fc83102 100644 --- a/speaker/speaker.go +++ b/speaker/speaker.go @@ -2,11 +2,13 @@ package speaker import ( - "github.com/ebitengine/oto/v3" - "github.com/gopxl/beep" - "github.com/pkg/errors" "io" "sync" + + "github.com/ebitengine/oto/v3" + "github.com/pkg/errors" + + "github.com/gopxl/beep" ) const channelCount = 2 diff --git a/speaker/speaker_test.go b/speaker/speaker_test.go new file mode 100644 index 0000000..6a41216 --- /dev/null +++ b/speaker/speaker_test.go @@ -0,0 +1,36 @@ +package speaker + +import ( + "fmt" + "io" + "testing" + + "github.com/gopxl/beep/internal/testtools" +) + +func BenchmarkSampleReader_Read(b *testing.B) { + // note: must be multiples of bytesPerSample + bufferSizes := []int{64, 512, 8192, 32768} + + for _, bs := range bufferSizes { + b.Run(fmt.Sprintf("with buffer size %d", bs), func(b *testing.B) { + s, _ := testtools.RandomDataStreamer(b.N) + r := newReaderFromStreamer(s) + buf := make([]byte, bs) + + b.StartTimer() + for { + n, err := r.Read(buf) + if err == io.EOF { + break + } + if err != nil { + panic(err) + } + if n != len(buf) { + break + } + } + }) + } +}