hugolib: Add TOML/YAML switch to benchmark

This commit is contained in:
Bjørn Erik Pedersen 2017-05-30 18:10:48 +03:00
parent 828427ef52
commit 2342df4d2d

View file

@ -24,6 +24,7 @@ import (
) )
type siteBuildingBenchmarkConfig struct { type siteBuildingBenchmarkConfig struct {
Frontmatter string
NumPages int NumPages int
RootSections int RootSections int
Render bool Render bool
@ -32,11 +33,13 @@ type siteBuildingBenchmarkConfig struct {
} }
func (s siteBuildingBenchmarkConfig) String() string { func (s siteBuildingBenchmarkConfig) String() string {
return fmt.Sprintf("num_root_sections=%d|num_pages=%d|tags_per_page=%d|shortcodes=%t|render=%t", s.RootSections, s.NumPages, s.TagsPerPage, s.Shortcodes, s.Render) return fmt.Sprintf("frontmatter=%s|num_root_sections=%d|num_pages=%d|tags_per_page=%d|shortcodes=%t|render=%t", s.Frontmatter, s.RootSections, s.NumPages, s.TagsPerPage, s.Shortcodes, s.Render)
} }
func BenchmarkSiteBuilding(b *testing.B) { func BenchmarkSiteBuilding(b *testing.B) {
var conf siteBuildingBenchmarkConfig var conf siteBuildingBenchmarkConfig
for _, frontmatter := range []string{"YAML", "TOML"} {
conf.Frontmatter = frontmatter
for _, rootSections := range []int{1, 5} { for _, rootSections := range []int{1, 5} {
conf.RootSections = rootSections conf.RootSections = rootSections
for _, tagsPerPage := range []int{0, 1, 5, 20} { for _, tagsPerPage := range []int{0, 1, 5, 20} {
@ -54,6 +57,7 @@ func BenchmarkSiteBuilding(b *testing.B) {
} }
} }
} }
}
func doBenchMarkSiteBuilding(conf siteBuildingBenchmarkConfig, b *testing.B) { func doBenchMarkSiteBuilding(conf siteBuildingBenchmarkConfig, b *testing.B) {
b.Run(conf.String(), func(b *testing.B) { b.Run(conf.String(), func(b *testing.B) {
@ -108,12 +112,21 @@ Unicode is supported. ☺
` `
pageTemplate := `+++ pageTemplateTOML := `+++
title = "%s" title = "%s"
tags = %s tags = %s
+++ +++
%s %s
`
pageTemplateYAML := `---
title: "%s"
tags:
%s
---
%s
` `
siteConfig := ` siteConfig := `
@ -129,6 +142,7 @@ category = "categories"
var ( var (
contentPagesContent [3]string contentPagesContent [3]string
tags = make([]string, cfg.TagsPerPage) tags = make([]string, cfg.TagsPerPage)
pageTemplate string
) )
tagOffset := rand.Intn(10) tagOffset := rand.Intn(10)
@ -137,10 +151,21 @@ category = "categories"
tags[i] = fmt.Sprintf("Hugo %d", i+tagOffset) tags[i] = fmt.Sprintf("Hugo %d", i+tagOffset)
} }
tagsStr := "[]" var tagsStr string
if cfg.Frontmatter == "TOML" {
pageTemplate = pageTemplateTOML
tagsStr = "[]"
if cfg.TagsPerPage > 0 { if cfg.TagsPerPage > 0 {
tagsStr = strings.Replace(fmt.Sprintf("%q", tags[0:cfg.TagsPerPage]), " ", ", ", -1) tagsStr = strings.Replace(fmt.Sprintf("%q", tags[0:cfg.TagsPerPage]), " ", ", ", -1)
} }
} else {
// YAML
pageTemplate = pageTemplateYAML
for _, tag := range tags {
tagsStr += "\n- " + tag
}
}
if cfg.Shortcodes { if cfg.Shortcodes {
contentPagesContent = [3]string{ contentPagesContent = [3]string{