Skip to content

Commit

Permalink
test: add escapedChar benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jan 18, 2022
1 parent c322742 commit 32b1f5d
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions dec_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func TestDecoder_Str(t *testing.T) {
})
}

func TestDecoder_strSlow(t *testing.T) {
r := errReader{}
d := Decode(r, 1)
_, err := d.strSlow(value{})
require.ErrorIs(t, err, r.Err())
}

func Benchmark_appendRune(b *testing.B) {
b.ReportAllocs()
buf := make([]byte, 0, 4)
Expand All @@ -48,6 +55,28 @@ func Benchmark_appendRune(b *testing.B) {
}
}

func BenchmarkDecoder_escapedChar(b *testing.B) {
bench := func(char byte, data []byte) func(b *testing.B) {
return func(b *testing.B) {
d := DecodeBytes(data)
v := value{buf: make([]byte, 0, 16)}

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
d.ResetBytes(data)
v.buf = v.buf[:0]
if _, err := d.escapedChar(v, char); err != nil {
b.Fatal(err)
}
}
}
}
b.Run("Unicode", bench('u', []byte(`000c`)))
b.Run("Newline", bench('n', nil))
}

func benchmarkDecoderStrBytes(data []byte) func(b *testing.B) {
return func(b *testing.B) {
d := GetDecoder()
Expand Down Expand Up @@ -89,10 +118,3 @@ func BenchmarkDecoder_StrBytes(b *testing.B) {
b.Run("EscapedUnicode", runBench("\f", -1))
b.Run("Mixed", runBench("aaaa\naaaa\faaaaa", 64))
}

func TestDecoder_strSlow(t *testing.T) {
r := errReader{}
d := Decode(r, 1)
_, err := d.strSlow(value{})
require.ErrorIs(t, err, r.Err())
}

0 comments on commit 32b1f5d

Please sign in to comment.