From 91eac9e573da41d68d061b453738d3e86420f3d7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 6 Nov 2025 10:43:12 +0100 Subject: [PATCH] all: Fix some benchmarks broken by modernize It's not possible to use `b.Loop()` when `b.N` is used to prepare test data. See 264022a75a8e4d91d7461747eef4d06267cc519f See #14107 --- common/hreflect/helpers_test.go | 2 +- common/hstrings/strings_test.go | 4 ++-- compare/compare_strings_test.go | 4 ++-- hugolib/cascade_test.go | 2 +- hugolib/hugo_smoke_test.go | 2 +- hugolib/pagecollections_test.go | 10 +++++----- hugolib/rebuild_test.go | 2 +- hugolib/shortcode_test.go | 4 +--- hugolib/site_benchmark_new_test.go | 2 +- hugolib/sitesmatrix/sitematrix_integration_test.go | 4 ++-- identity/identity_test.go | 2 +- markup/goldmark/goldmark_integration_test.go | 4 ++-- markup/tableofcontents/tableofcontents_test.go | 2 +- related/related_integration_test.go | 2 +- resources/image_test.go | 2 +- tpl/partials/partials_integration_test.go | 2 +- 16 files changed, 24 insertions(+), 26 deletions(-) diff --git a/common/hreflect/helpers_test.go b/common/hreflect/helpers_test.go index 9527fbb7d..bd578650d 100644 --- a/common/hreflect/helpers_test.go +++ b/common/hreflect/helpers_test.go @@ -161,7 +161,7 @@ func BenchmarkIsContextType(b *testing.B) { } b.ResetTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { if !IsContextType(ctxs[i]) { b.Fatal("not context") } diff --git a/common/hstrings/strings_test.go b/common/hstrings/strings_test.go index 205005785..9eb3b04cb 100644 --- a/common/hstrings/strings_test.go +++ b/common/hstrings/strings_test.go @@ -105,13 +105,13 @@ func BenchmarkUniqueStrings(b *testing.B) { b.Run("Reuse slice sorted", func(b *testing.B) { b.StopTimer() inputs := make([][]string, b.N) - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { inputc := make([]string, len(input)) copy(inputc, input) inputs[i] = inputc } b.StartTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { inputc := inputs[i] result := UniqueStringsSorted(inputc) diff --git a/compare/compare_strings_test.go b/compare/compare_strings_test.go index 505eaa7a3..1a5bb0b1a 100644 --- a/compare/compare_strings_test.go +++ b/compare/compare_strings_test.go @@ -67,12 +67,12 @@ func BenchmarkStringSort(b *testing.B) { prototype := []string{"b", "Bz", "zz", "ba", "αβδ αβδ αβδ", "A", "Ba", "ba", "nnnnasdfnnn", "AAgæåz", "αβδC"} b.Run("LessStrings", func(b *testing.B) { ss := make([][]string, b.N) - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { ss[i] = make([]string, len(prototype)) copy(ss[i], prototype) } b.ResetTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { sss := ss[i] sort.Slice(sss, func(i, j int) bool { return LessStrings(sss[i], sss[j]) diff --git a/hugolib/cascade_test.go b/hugolib/cascade_test.go index 659006a2c..b592ee0f8 100644 --- a/hugolib/cascade_test.go +++ b/hugolib/cascade_test.go @@ -83,7 +83,7 @@ kind = '{section,term}' b.ResetTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { builders[i].Build() } }) diff --git a/hugolib/hugo_smoke_test.go b/hugolib/hugo_smoke_test.go index 9b4079dc9..f7c0562ed 100644 --- a/hugolib/hugo_smoke_test.go +++ b/hugolib/hugo_smoke_test.go @@ -749,7 +749,7 @@ func BenchmarkBaseline(b *testing.B) { builders[i] = NewIntegrationTestBuilder(cfg) } - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { builders[i].Build() } } diff --git a/hugolib/pagecollections_test.go b/hugolib/pagecollections_test.go index 0ea757b1c..94ae175b5 100644 --- a/hugolib/pagecollections_test.go +++ b/hugolib/pagecollections_test.go @@ -61,7 +61,7 @@ func BenchmarkGetPage(b *testing.B) { pagePaths[i] = fmt.Sprintf("sect%d", r.Intn(10)) } - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { home, _ := s.getPage(nil, "/") if home == nil { b.Fatal("Home is nil") @@ -120,12 +120,12 @@ func BenchmarkGetPageRegular(b *testing.B) { pagePaths := make([]string, b.N) - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { pagePaths[i] = path.Join(fmt.Sprintf("/sect%d", r.Intn(10)), fmt.Sprintf("page%d.md", r.Intn(100))) } b.ResetTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { page, _ := s.getPage(nil, pagePaths[i]) c.Assert(page, qt.Not(qt.IsNil)) } @@ -139,13 +139,13 @@ func BenchmarkGetPageRegular(b *testing.B) { pagePaths := make([]string, b.N) pages := make([]page.Page, b.N) - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { pagePaths[i] = fmt.Sprintf("page%d.md", r.Intn(100)) pages[i] = allPages[r.Intn(len(allPages)/3)] } b.ResetTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { page, _ := s.getPage(pages[i], pagePaths[i]) c.Assert(page, qt.Not(qt.IsNil)) } diff --git a/hugolib/rebuild_test.go b/hugolib/rebuild_test.go index 53a3bef6c..07317ae1c 100644 --- a/hugolib/rebuild_test.go +++ b/hugolib/rebuild_test.go @@ -1797,7 +1797,7 @@ func BenchmarkRebuildContentFileChange(b *testing.B) { builders[i].Build() } - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { bb := builders[i] bb.EditFileReplaceFunc("content/mysect/p123/index.md", func(s string) string { return s + "... Edited" diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go index cf0a77f18..560e0b4c0 100644 --- a/hugolib/shortcode_test.go +++ b/hugolib/shortcode_test.go @@ -100,7 +100,6 @@ title: "Shortcodes Galore!" {"closed without content", `{{< inner param1 >}}{{< / inner >}}`, regexpCheck("inner.*inner:{}")}, {"inline", `{{< my.inline >}}Hi{{< /my.inline >}}`, regexpCheck("my.inline;inline:true;closing:true;inner:{Hi};")}, } { - t.Run(test.name, func(t *testing.T) { t.Parallel() c := qt.New(t) @@ -342,7 +341,7 @@ title: "Markdown Shortcode" builders[i] = NewIntegrationTestBuilder(cfg) } - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { builders[i].Build() } } @@ -715,7 +714,6 @@ TOC: {{ .TableOfContents }} ) } }) - } } diff --git a/hugolib/site_benchmark_new_test.go b/hugolib/site_benchmark_new_test.go index 75f53cbfd..85fff472f 100644 --- a/hugolib/site_benchmark_new_test.go +++ b/hugolib/site_benchmark_new_test.go @@ -504,7 +504,7 @@ func BenchmarkSiteNew(b *testing.B) { } b.Run(name, func(b *testing.B) { sites := make([]*sitesBuilder, b.N) - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { sites[i] = bm.create(b) if edit { sites[i].Running() diff --git a/hugolib/sitesmatrix/sitematrix_integration_test.go b/hugolib/sitesmatrix/sitematrix_integration_test.go index 3e93e8f96..9408a701d 100644 --- a/hugolib/sitesmatrix/sitematrix_integration_test.go +++ b/hugolib/sitesmatrix/sitematrix_integration_test.go @@ -1125,11 +1125,11 @@ func BenchmarkSitesMatrixContent(b *testing.B) { for _, multipleDimensions := range []bool{false, true} { b.Run(fmt.Sprintf("n%d/md%t", numPages, multipleDimensions), func(b *testing.B) { builders := make([]*hugolib.IntegrationTestBuilder, b.N) - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { builders[i] = newSitesMatrixContentBenchmarkBuilder(b, numPages, true, true) } b.ResetTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { builders[i].Build() } }) diff --git a/identity/identity_test.go b/identity/identity_test.go index a14ab615d..3b15b61b9 100644 --- a/identity/identity_test.go +++ b/identity/identity_test.go @@ -46,7 +46,7 @@ func BenchmarkIdentityManager(b *testing.B) { im := identity.NewManager() b.ResetTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { im.AddIdentity(ids[i]) } diff --git a/markup/goldmark/goldmark_integration_test.go b/markup/goldmark/goldmark_integration_test.go index 8dc24593f..8ea9dbde2 100644 --- a/markup/goldmark/goldmark_integration_test.go +++ b/markup/goldmark/goldmark_integration_test.go @@ -284,7 +284,7 @@ D. builders[i] = hugolib.NewIntegrationTestBuilder(cfg) } - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { builders[i].Build() } } @@ -343,7 +343,7 @@ FENCE b.ResetTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { builders[i].Build() } } diff --git a/markup/tableofcontents/tableofcontents_test.go b/markup/tableofcontents/tableofcontents_test.go index d85156150..4b9b460a6 100644 --- a/markup/tableofcontents/tableofcontents_test.go +++ b/markup/tableofcontents/tableofcontents_test.go @@ -218,7 +218,7 @@ func BenchmarkToc(b *testing.B) { b.Run("ToHTML", func(b *testing.B) { tocs := newTocs(b.N) b.ResetTimer() - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { toc := tocs[i] toc.ToHTML(1, -1, false) } diff --git a/related/related_integration_test.go b/related/related_integration_test.go index 18ec51f4a..5d558368e 100644 --- a/related/related_integration_test.go +++ b/related/related_integration_test.go @@ -181,7 +181,7 @@ keywords: ['k%d'] builders[i] = hugolib.NewIntegrationTestBuilder(cfg) } - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { builders[i].Build() } } diff --git a/resources/image_test.go b/resources/image_test.go index 9b9c69f49..e852d7401 100644 --- a/resources/image_test.go +++ b/resources/image_test.go @@ -469,7 +469,7 @@ func BenchmarkImageExif(b *testing.B) { getImages := func(c *qt.C, b *testing.B, fs afero.Fs) []images.ImageResource { spec := newTestResourceSpec(specDescriptor{fs: fs, c: c}) imgs := make([]images.ImageResource, b.N) - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { imgs[i] = fetchResourceForSpec(spec, c, "sunset.jpg", strconv.Itoa(i)).(images.ImageResource) } return imgs diff --git a/tpl/partials/partials_integration_test.go b/tpl/partials/partials_integration_test.go index 048ccbf90..3337a0ec7 100644 --- a/tpl/partials/partials_integration_test.go +++ b/tpl/partials/partials_integration_test.go @@ -243,7 +243,7 @@ ABCDE builders[i] = hugolib.NewIntegrationTestBuilder(cfg) } - for i := 0; b.Loop(); i++ { + for i := 0; i < b.N; i++ { builders[i].Build() } } -- 2.39.5