Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
youpy committed Feb 23, 2022
1 parent 780d56e commit bfcf6fa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
38 changes: 0 additions & 38 deletions benchmark/read_samples.go

This file was deleted.

30 changes: 30 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package wav

import (
"fmt"
"io"
"io/ioutil"
"math"
"os"
"testing"

"gotest.tools/assert"
Expand Down Expand Up @@ -149,3 +152,30 @@ func TestReadAlaw(t *testing.T) {

t.Logf("Data size: %d", len(bytes))
}

func BenchmarkReadSamples(b *testing.B) {
n := []uint32{1, 10, 100, 1000, 2000, 3000, 5000, 8000, 10000, 20000, 40000}

var t int

for _, numSamples := range n {
b.Run(fmt.Sprintf("%d", numSamples), func(b *testing.B) {
for i := 0; i < b.N; i++ {

file, _ := os.Open("./files/a.wav")
reader := NewReader(file)

for {
samples, err := reader.ReadSamples(numSamples)
if err == io.EOF {
break
}
for _, sample := range samples {
t += reader.IntValue(sample, 0)
t += reader.IntValue(sample, 1)
}
}
}
})
}
}
29 changes: 29 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wav

import (
"io"
"io/ioutil"
"os"
"testing"
Expand Down Expand Up @@ -283,3 +284,31 @@ func TestWrite32BitStereo(t *testing.T) {
assert.Equal(t, samples[1].Values[0], 123)
assert.Equal(t, samples[1].Values[1], -123)
}

func BenchmarkWriteSamples(b *testing.B) {
n := 4096
samples := []Sample{}

file, _ := os.Open("./files/a.wav")
reader := NewReader(file)

for {
spls, err := reader.ReadSamples(uint32(n))
if err == io.EOF {
break
}
samples = append(samples, spls...)
}

b.Run("write samples", func(b *testing.B) {
for i := 0; i < b.N; i++ {
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
b.Fatal(err)
}
defer os.Remove(tmpfile.Name())
writer := NewWriter(tmpfile, uint32(len(samples)), 2, 44100, 16)
writer.WriteSamples(samples)
}
})
}

0 comments on commit bfcf6fa

Please sign in to comment.