Skip to content

Commit 3d9c4f5

Browse files
committed
parser: Add BenchmarkFrontmatterTags
The list handling is surprisingly expensive: ``` ▶ go test -run="NONE" -bench="BenchmarkFrontmatterTags" -test.benchmem=true ./parser | prettybench PASS benchmark iter time/iter bytes alloc allocs --------- ---- --------- ----------- ------ BenchmarkFrontmatterTags/JSON:1-4 1000000 2039.00 ns/op 912 B/op 20 allocs/op BenchmarkFrontmatterTags/JSON:11-4 300000 5202.00 ns/op 1640 B/op 44 allocs/op BenchmarkFrontmatterTags/JSON:21-4 200000 7993.00 ns/op 2392 B/op 65 allocs/op BenchmarkFrontmatterTags/YAML:1-4 200000 9359.00 ns/op 5928 B/op 66 allocs/op BenchmarkFrontmatterTags/YAML:11-4 100000 21218.00 ns/op 8408 B/op 140 allocs/op BenchmarkFrontmatterTags/YAML:21-4 50000 32852.00 ns/op 10920 B/op 211 allocs/op BenchmarkFrontmatterTags/TOML:1-4 100000 21505.00 ns/op 9231 B/op 173 allocs/op BenchmarkFrontmatterTags/TOML:11-4 20000 82919.00 ns/op 19808 B/op 625 allocs/op BenchmarkFrontmatterTags/TOML:21-4 10000 141847.00 ns/op 31200 B/op 1106 allocs/op ok github.com/spf13/hugo/parser 17.890s ``` See #3464
1 parent 250ebc1 commit 3d9c4f5

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

‎parser/frontmatter_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ package parser
1515

1616
import (
1717
"bytes"
18+
"fmt"
1819
"reflect"
20+
"strings"
1921
"testing"
2022
)
2123

@@ -317,3 +319,71 @@ func TestRemoveTOMLIdentifier(t *testing.T) {
317319
}
318320
}
319321
}
322+
323+
func BenchmarkFrontmatterTags(b *testing.B) {
324+
325+
for _, frontmatter := range []string{"JSON", "YAML", "TOML"} {
326+
for i := 1; i < 30; i += 10 {
327+
doBenchmarkFrontmatter(b, frontmatter, i)
328+
}
329+
}
330+
}
331+
332+
func doBenchmarkFrontmatter(b *testing.B, fileformat string, numTags int) {
333+
yamlTemplate := `---
334+
name: "Tags"
335+
tags:
336+
%s
337+
---
338+
`
339+
tomlTemplate := `+++
340+
name = "Tags"
341+
tags = %s
342+
+++
343+
`
344+
345+
jsonTemplate := `{
346+
"name": "Tags",
347+
"tags": [
348+
%s
349+
]
350+
}`
351+
name := fmt.Sprintf("%s:%d", fileformat, numTags)
352+
b.Run(name, func(b *testing.B) {
353+
tags := make([]string, numTags)
354+
var (
355+
tagsStr string
356+
frontmatterTemplate string
357+
)
358+
for i := 0; i < numTags; i++ {
359+
tags[i] = fmt.Sprintf("Hugo %d", i+1)
360+
}
361+
if fileformat == "TOML" {
362+
frontmatterTemplate = tomlTemplate
363+
tagsStr = strings.Replace(fmt.Sprintf("%q", tags), " ", ", ", -1)
364+
} else if fileformat == "JSON" {
365+
frontmatterTemplate = jsonTemplate
366+
tagsStr = strings.Replace(fmt.Sprintf("%q", tags), " ", ", ", -1)
367+
} else {
368+
frontmatterTemplate = yamlTemplate
369+
for _, tag := range tags {
370+
tagsStr += "\n- " + tag
371+
}
372+
}
373+
374+
frontmatter := fmt.Sprintf(frontmatterTemplate, tagsStr)
375+
376+
p := page{frontmatter: []byte(frontmatter)}
377+
378+
b.ResetTimer()
379+
for i := 0; i < b.N; i++ {
380+
meta, err := p.Metadata()
381+
if err != nil {
382+
b.Fatal(err)
383+
}
384+
if meta == nil {
385+
b.Fatal("Meta is nil")
386+
}
387+
}
388+
})
389+
}

0 commit comments

Comments
 (0)