4ea03de40d
benchstat old new name old time/op new time/op delta AppendString/MultiBytesFirst-8 77.9ns ± 5% 70.2ns ± 1% -9.88% (p=0.008 n=5+5) AppendString/MultiBytesMiddle-8 64.2ns ± 1% 56.3ns ± 5% -12.19% (p=0.008 n=5+5) AppendString/MultiBytesLast-8 51.2ns ± 2% 45.2ns ± 4% -11.65% (p=0.008 n=5+5) AppendString/NoEncoding-8 36.2ns ± 4% 34.0ns ± 6% ~ (p=0.087 n=5+5) AppendString/EncodingFirst-8 67.7ns ± 2% 59.4ns ± 2% -12.26% (p=0.008 n=5+5) AppendString/EncodingMiddle-8 56.5ns ± 2% 50.6ns ± 5% -10.54% (p=0.008 n=5+5) AppendString/EncodingLast-8 41.3ns ± 1% 39.6ns ± 5% -4.11% (p=0.024 n=5+5) AppendBytes/MultiBytesLast-8 53.5ns ± 6% 45.6ns ± 4% -14.79% (p=0.008 n=5+5) AppendBytes/NoEncoding-8 36.3ns ± 3% 28.6ns ± 3% -21.10% (p=0.008 n=5+5) AppendBytes/EncodingFirst-8 67.3ns ± 4% 62.1ns ± 4% -7.75% (p=0.008 n=5+5) AppendBytes/EncodingMiddle-8 59.2ns ± 7% 51.0ns ± 6% -13.85% (p=0.008 n=5+5) AppendBytes/EncodingLast-8 43.7ns ± 6% 34.4ns ± 2% -21.32% (p=0.008 n=5+5) AppendBytes/MultiBytesFirst-8 77.7ns ± 2% 71.2ns ± 3% -8.37% (p=0.008 n=5+5) AppendBytes/MultiBytesMiddle-8 63.6ns ± 3% 57.8ns ± 5% -9.12% (p=0.008 n=5+5)
83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package json
|
||
|
||
import (
|
||
"testing"
|
||
"unicode"
|
||
)
|
||
|
||
func TestAppendBytes(t *testing.T) {
|
||
for _, tt := range encodeStringTests {
|
||
b := AppendBytes([]byte{}, []byte(tt.in))
|
||
if got, want := string(b), tt.out; got != want {
|
||
t.Errorf("appendBytes(%q) = %#q, want %#q", tt.in, got, want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestAppendHex(t *testing.T) {
|
||
for _, tt := range encodeHexTests {
|
||
b := AppendHex([]byte{}, []byte{tt.in})
|
||
if got, want := string(b), tt.out; got != want {
|
||
t.Errorf("appendHex(%x) = %s, want %s", tt.in, got, want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestStringBytes(t *testing.T) {
|
||
t.Parallel()
|
||
// Test that encodeState.stringBytes and encodeState.string use the same encoding.
|
||
var r []rune
|
||
for i := '\u0000'; i <= unicode.MaxRune; i++ {
|
||
r = append(r, i)
|
||
}
|
||
s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
|
||
|
||
enc := string(AppendString([]byte{}, s))
|
||
encBytes := string(AppendBytes([]byte{}, []byte(s)))
|
||
|
||
if enc != encBytes {
|
||
i := 0
|
||
for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {
|
||
i++
|
||
}
|
||
enc = enc[i:]
|
||
encBytes = encBytes[i:]
|
||
i = 0
|
||
for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {
|
||
i++
|
||
}
|
||
enc = enc[:len(enc)-i]
|
||
encBytes = encBytes[:len(encBytes)-i]
|
||
|
||
if len(enc) > 20 {
|
||
enc = enc[:20] + "..."
|
||
}
|
||
if len(encBytes) > 20 {
|
||
encBytes = encBytes[:20] + "..."
|
||
}
|
||
|
||
t.Errorf("encodings differ at %#q vs %#q", enc, encBytes)
|
||
}
|
||
}
|
||
|
||
func BenchmarkAppendBytes(b *testing.B) {
|
||
tests := map[string]string{
|
||
"NoEncoding": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
|
||
"EncodingFirst": `"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
|
||
"EncodingMiddle": `aaaaaaaaaaaaaaaaaaaaaaaaa"aaaaaaaaaaaaaaaaaaaaaaaa`,
|
||
"EncodingLast": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"`,
|
||
"MultiBytesFirst": `❤️aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
|
||
"MultiBytesMiddle": `aaaaaaaaaaaaaaaaaaaaaaaaa❤️aaaaaaaaaaaaaaaaaaaaaaaa`,
|
||
"MultiBytesLast": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa❤️`,
|
||
}
|
||
for name, str := range tests {
|
||
byt := []byte(str)
|
||
b.Run(name, func(b *testing.B) {
|
||
buf := make([]byte, 0, 100)
|
||
for i := 0; i < b.N; i++ {
|
||
_ = AppendBytes(buf, byt)
|
||
}
|
||
})
|
||
}
|
||
}
|