Skip to content

Commit 606415e

Browse files
committed
Add a site assembly benchmark test for a deeper site structure with more sections and pages
1 parent dc2f6ae commit 606415e

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

‎hugolib/hugo_sites_build_test.go‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package hugolib
22

33
import (
4+
"fmt"
45
"path/filepath"
6+
"strings"
57
"testing"
68

79
qt "github.com/frankban/quicktest"
10+
"github.com/gohugoio/hugo/common/paths"
811
"github.com/gohugoio/hugo/resources/kinds"
912

1013
"github.com/spf13/afero"
@@ -90,3 +93,119 @@ func writeToFs(t testing.TB, fs afero.Fs, filename, content string) {
9093
t.Fatalf("Failed to write file: %s", err)
9194
}
9295
}
96+
97+
func TestBenchmarkAssembleDeepSiteWithManySections(t *testing.T) {
98+
t.Parallel()
99+
100+
b := createBenchmarkAssembleDeepSiteWithManySectionsBuilder(t, false, 2, 3, 4).Build()
101+
b.AssertFileContent("public/index.html", "Num regular pages recursive: 48|")
102+
}
103+
104+
func createBenchmarkAssembleDeepSiteWithManySectionsBuilder(t testing.TB, skipRender bool, sectionDepth, sectionsPerLevel, pagesPerSection int) *IntegrationTestBuilder {
105+
t.Helper()
106+
107+
const contentTemplate = `---
108+
title: P%d
109+
---
110+
111+
## A title
112+
113+
Some content with a shortcode: {{< foo >}}.
114+
115+
Some more content and then another shortcode: {{< foo >}}.
116+
117+
Some final content.
118+
`
119+
120+
const filesTemplate = `
121+
-- hugo.toml --
122+
baseURL = "http://example.org/"
123+
disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
124+
-- layouts/all.html --
125+
All.{{ .Title }}|{{ .Content }}|Num pages: {{ len .Pages }}|Num sections: {{ len .Sections }}|Num regular pages recursive: {{ len .RegularPagesRecursive }}|
126+
Sections: {{ range .Sections }}{{ .Title }}|{{ end }}|
127+
RegularPagesRecursive: {{ range .RegularPagesRecursive }}{{ .RelPermalink }}|{{ end }}|
128+
-- layouts/_shortcodes/foo.html --
129+
`
130+
page := func(section string, i int) string {
131+
return fmt.Sprintf("\n-- content/%s/p%d.md --\n"+contentTemplate, section, i, i)
132+
}
133+
134+
section := func(section string, i int) string {
135+
if section != "" {
136+
section = paths.AddTrailingSlash(section)
137+
}
138+
return fmt.Sprintf("\n-- content/%ss%d/_index.md --\n"+contentTemplate, section, i, i)
139+
}
140+
141+
var sb strings.Builder
142+
143+
// s0
144+
// s0/s0
145+
// s0/s1
146+
// etc.
147+
var (
148+
pageCount int
149+
sectionCount int
150+
)
151+
var createSections func(currentSection string, currentDepth int)
152+
createSections = func(currentSection string, currentDepth int) {
153+
if currentDepth > sectionDepth {
154+
return
155+
}
156+
157+
for i := 0; i < sectionsPerLevel; i++ {
158+
sectionCount++
159+
sectionName := fmt.Sprintf("s%d", i)
160+
sectionPath := sectionName
161+
if currentSection != "" {
162+
sectionPath = currentSection + "/" + sectionName
163+
}
164+
sb.WriteString(section(currentSection, i))
165+
166+
// Pages in this section
167+
for j := 0; j < pagesPerSection; j++ {
168+
pageCount++
169+
sb.WriteString(page(sectionPath, j))
170+
}
171+
172+
// Recurse
173+
createSections(sectionPath, currentDepth+1)
174+
}
175+
}
176+
177+
createSections("", 1)
178+
179+
sb.WriteString(filesTemplate)
180+
181+
files := sb.String()
182+
183+
return NewIntegrationTestBuilder(
184+
IntegrationTestConfig{
185+
T: t,
186+
TxtarString: files,
187+
BuildCfg: BuildCfg{
188+
SkipRender: skipRender,
189+
},
190+
},
191+
)
192+
}
193+
194+
func BenchmarkAssembleDeepSiteWithManySections(b *testing.B) {
195+
runOne := func(sectionDepth, sectionsPerLevel, pagesPerSection int) {
196+
name := fmt.Sprintf("depth=%d/sectionsPerLevel=%d/pagesPerSection=%d", sectionDepth, sectionsPerLevel, pagesPerSection)
197+
b.Run(name, func(b *testing.B) {
198+
for b.Loop() {
199+
b.StopTimer()
200+
bt := createBenchmarkAssembleDeepSiteWithManySectionsBuilder(b, true, sectionDepth, sectionsPerLevel, pagesPerSection)
201+
b.StartTimer()
202+
bt.Build()
203+
}
204+
})
205+
}
206+
207+
runOne(1, 6, 100)
208+
runOne(2, 2, 100)
209+
runOne(2, 6, 100)
210+
runOne(3, 2, 100)
211+
}

0 commit comments

Comments
 (0)