|
1 | 1 | package hugolib |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "path/filepath" |
| 6 | + "strings" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | qt "github.com/frankban/quicktest" |
| 10 | + "github.com/gohugoio/hugo/common/paths" |
8 | 11 | "github.com/gohugoio/hugo/resources/kinds" |
9 | 12 |
|
10 | 13 | "github.com/spf13/afero" |
@@ -90,3 +93,119 @@ func writeToFs(t testing.TB, fs afero.Fs, filename, content string) { |
90 | 93 | t.Fatalf("Failed to write file: %s", err) |
91 | 94 | } |
92 | 95 | } |
| 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