From b96c535061065ab90c9356c5c8fbf0095fa4eac0 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Mon, 1 Feb 2021 19:25:07 -0500 Subject: [PATCH] Check for allocs --- toml_test.go | 77 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 18 deletions(-) diff --git a/toml_test.go b/toml_test.go index 7b2e30b..bb26178 100644 --- a/toml_test.go +++ b/toml_test.go @@ -7,25 +7,24 @@ import ( "github.com/stretchr/testify/assert" ) +var inputs = []string{ + ` #foo`, + `#foo`, + `#`, + "\n\n\n", + "#one\n # two \n", + `a = false`, + `abc = false`, + ` abc = false # foo`, + `'abc' = false`, + `"foo bar" = false`, + `"hello\tworld" = false`, + `"hello \u1234 foo" = false`, + `a.b.c = false`, + `a."b".c = true`, +} + func TestParse(t *testing.T) { - - inputs := []string{ - ` #foo`, - `#foo`, - `#`, - "\n\n\n", - "#one\n # two \n", - `a = false`, - `abc = false`, - ` abc = false # foo`, - `'abc' = false`, - `"foo bar" = false`, - `"hello\tworld" = false`, - `"hello \u1234 foo" = false`, - `a.b.c = false`, - `a."b".c = true`, - } - for i, data := range inputs { t.Run(fmt.Sprintf("example %d", i), func(t *testing.T) { fmt.Printf("input:\n\t`%s`\n", data) @@ -35,3 +34,45 @@ func TestParse(t *testing.T) { }) } } + +type noopBuilder struct { +} + +func (n noopBuilder) Whitespace(b []byte) { +} + +func (n noopBuilder) Comment(b []byte) { +} + +func (n noopBuilder) UnquotedKey(b []byte) { +} + +func (n noopBuilder) LiteralString(b []byte) { +} + +func (n noopBuilder) BasicString(b []byte) { +} + +func (n noopBuilder) Dot(b []byte) { +} + +func (n noopBuilder) Boolean(b []byte) { +} + +func (n noopBuilder) Equal(b []byte) { +} + +func BenchmarkParseAll(b *testing.B) { + b.ReportAllocs() + + for i := 0; i < b.N; i++ { + for _, data := range inputs { + builder := noopBuilder{} + p := parser{builder: &builder, data: []byte(data)} + err := p.parse() + if err != nil { + b.Fatalf("error: %s", err) + } + } + } +}