@@ -15,7 +15,9 @@ package parser
1515
1616import (
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