diff options
author | Bjørn Erik Pedersen <[email protected]> | 2021-05-04 17:45:24 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2021-05-04 17:59:04 +0200 |
commit | b660ea8d545d6ba5479dd28a670044d57e5d196f (patch) | |
tree | 167383deed940238e8820c40020c624121dbdd0b /hugolib | |
parent | 64f88f3011de5a510d8e6d6bad8ac4a091b11c0c (diff) | |
download | hugo-b660ea8d545d6ba5479dd28a670044d57e5d196f.tar.gz hugo-b660ea8d545d6ba5479dd28a670044d57e5d196f.zip |
Add a benchmark
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/pages_test.go | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/hugolib/pages_test.go b/hugolib/pages_test.go index 0d879d2c7..30e9e59d2 100644 --- a/hugolib/pages_test.go +++ b/hugolib/pages_test.go @@ -11,17 +11,21 @@ import ( ) func newPagesPrevNextTestSite(t testing.TB, numPages int) *sitesBuilder { + categories := []string{"blue", "green", "red", "orange", "indigo", "amber", "lime"} + cat1, cat2 := categories[rand.Intn(len(categories))], categories[rand.Intn(len(categories))] + categoriesSlice := fmt.Sprintf("[%q,%q]", cat1, cat2) pageTemplate := ` --- title: "Page %d" weight: %d +categories: %s --- ` b := newTestSitesBuilder(t) for i := 1; i <= numPages; i++ { - b.WithContent(fmt.Sprintf("page%d.md", i), fmt.Sprintf(pageTemplate, i, rand.Intn(numPages))) + b.WithContent(fmt.Sprintf("page%d.md", i), fmt.Sprintf(pageTemplate, i, rand.Intn(numPages), categoriesSlice)) } return b @@ -81,3 +85,35 @@ func BenchmarkPagesPrevNext(b *testing.B) { } } } + +func BenchmarkPagePageCollections(b *testing.B) { + type Variant struct { + name string + run func(p page.Page) + } + + for _, variant := range []Variant{ + {".Pages", func(p page.Page) { p.Pages() }}, + {".RegularPages", func(p page.Page) { p.RegularPages() }}, + {".RegularPagesRecursive", func(p page.Page) { p.RegularPagesRecursive() }}, + } { + for _, numPages := range []int{300, 5000} { + b.Run(fmt.Sprintf("%s-%d", variant.name, numPages), func(b *testing.B) { + b.StopTimer() + builder := newPagesPrevNextTestSite(b, numPages) + builder.Build(BuildCfg{SkipRender: true}) + var pages page.Pages + for _, p := range builder.H.Sites[0].Pages() { + if !p.IsPage() { + pages = append(pages, p) + } + } + b.StartTimer() + for i := 0; i < b.N; i++ { + p := pages[rand.Intn(len(pages))] + variant.run(p) + } + }) + } + } +} |