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)
94 lines
2.3 KiB
Go
94 lines
2.3 KiB
Go
package json
|
||
|
||
import (
|
||
"testing"
|
||
)
|
||
|
||
var encodeStringTests = []struct {
|
||
in string
|
||
out string
|
||
}{
|
||
{"", `""`},
|
||
{"\\", `"\\"`},
|
||
{"\x00", `"\u0000"`},
|
||
{"\x01", `"\u0001"`},
|
||
{"\x02", `"\u0002"`},
|
||
{"\x03", `"\u0003"`},
|
||
{"\x04", `"\u0004"`},
|
||
{"\x05", `"\u0005"`},
|
||
{"\x06", `"\u0006"`},
|
||
{"\x07", `"\u0007"`},
|
||
{"\x08", `"\b"`},
|
||
{"\x09", `"\t"`},
|
||
{"\x0a", `"\n"`},
|
||
{"\x0b", `"\u000b"`},
|
||
{"\x0c", `"\f"`},
|
||
{"\x0d", `"\r"`},
|
||
{"\x0e", `"\u000e"`},
|
||
{"\x0f", `"\u000f"`},
|
||
{"\x10", `"\u0010"`},
|
||
{"\x11", `"\u0011"`},
|
||
{"\x12", `"\u0012"`},
|
||
{"\x13", `"\u0013"`},
|
||
{"\x14", `"\u0014"`},
|
||
{"\x15", `"\u0015"`},
|
||
{"\x16", `"\u0016"`},
|
||
{"\x17", `"\u0017"`},
|
||
{"\x18", `"\u0018"`},
|
||
{"\x19", `"\u0019"`},
|
||
{"\x1a", `"\u001a"`},
|
||
{"\x1b", `"\u001b"`},
|
||
{"\x1c", `"\u001c"`},
|
||
{"\x1d", `"\u001d"`},
|
||
{"\x1e", `"\u001e"`},
|
||
{"\x1f", `"\u001f"`},
|
||
{"✭", `"✭"`},
|
||
{"foo\xc2\x7fbar", `"foo\ufffd\u007fbar"`}, // invalid sequence
|
||
{"ascii", `"ascii"`},
|
||
{"\"a", `"\"a"`},
|
||
{"\x1fa", `"\u001fa"`},
|
||
{"foo\"bar\"baz", `"foo\"bar\"baz"`},
|
||
{"\x1ffoo\x1fbar\x1fbaz", `"\u001ffoo\u001fbar\u001fbaz"`},
|
||
{"emoji \u2764\ufe0f!", `"emoji ❤️!"`},
|
||
}
|
||
|
||
var encodeHexTests = []struct {
|
||
in byte
|
||
out string
|
||
}{
|
||
{0x00, `"00"`},
|
||
{0x0f, `"0f"`},
|
||
{0x10, `"10"`},
|
||
{0xf0, `"f0"`},
|
||
{0xff, `"ff"`},
|
||
}
|
||
|
||
func TestAppendString(t *testing.T) {
|
||
for _, tt := range encodeStringTests {
|
||
b := AppendString([]byte{}, tt.in)
|
||
if got, want := string(b), tt.out; got != want {
|
||
t.Errorf("appendString(%q) = %#q, want %#q", tt.in, got, want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func BenchmarkAppendString(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 {
|
||
b.Run(name, func(b *testing.B) {
|
||
buf := make([]byte, 0, 100)
|
||
for i := 0; i < b.N; i++ {
|
||
_ = AppendString(buf, str)
|
||
}
|
||
})
|
||
}
|
||
}
|