Skip to content

Commit 91eac9e

Browse files
committed
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 264022a See #14107
1 parent 04650ce commit 91eac9e

File tree

16 files changed

+24
-26
lines changed

16 files changed

+24
-26
lines changed

common/hreflect/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func BenchmarkIsContextType(b *testing.B) {
161161
}
162162

163163
b.ResetTimer()
164-
for i := 0; b.Loop(); i++ {
164+
for i := 0; i < b.N; i++ {
165165
if !IsContextType(ctxs[i]) {
166166
b.Fatal("not context")
167167
}

common/hstrings/strings_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ func BenchmarkUniqueStrings(b *testing.B) {
105105
b.Run("Reuse slice sorted", func(b *testing.B) {
106106
b.StopTimer()
107107
inputs := make([][]string, b.N)
108-
for i := 0; b.Loop(); i++ {
108+
for i := 0; i < b.N; i++ {
109109
inputc := make([]string, len(input))
110110
copy(inputc, input)
111111
inputs[i] = inputc
112112
}
113113
b.StartTimer()
114-
for i := 0; b.Loop(); i++ {
114+
for i := 0; i < b.N; i++ {
115115
inputc := inputs[i]
116116

117117
result := UniqueStringsSorted(inputc)

compare/compare_strings_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ func BenchmarkStringSort(b *testing.B) {
6767
prototype := []string{"b", "Bz", "zz", "ba", "αβδ αβδ αβδ", "A", "Ba", "ba", "nnnnasdfnnn", "AAgæåz", "αβδC"}
6868
b.Run("LessStrings", func(b *testing.B) {
6969
ss := make([][]string, b.N)
70-
for i := 0; b.Loop(); i++ {
70+
for i := 0; i < b.N; i++ {
7171
ss[i] = make([]string, len(prototype))
7272
copy(ss[i], prototype)
7373
}
7474
b.ResetTimer()
75-
for i := 0; b.Loop(); i++ {
75+
for i := 0; i < b.N; i++ {
7676
sss := ss[i]
7777
sort.Slice(sss, func(i, j int) bool {
7878
return LessStrings(sss[i], sss[j])

hugolib/cascade_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ kind = '{section,term}'
8383

8484
b.ResetTimer()
8585

86-
for i := 0; b.Loop(); i++ {
86+
for i := 0; i < b.N; i++ {
8787
builders[i].Build()
8888
}
8989
})

hugolib/hugo_smoke_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ func BenchmarkBaseline(b *testing.B) {
749749
builders[i] = NewIntegrationTestBuilder(cfg)
750750
}
751751

752-
for i := 0; b.Loop(); i++ {
752+
for i := 0; i < b.N; i++ {
753753
builders[i].Build()
754754
}
755755
}

hugolib/pagecollections_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func BenchmarkGetPage(b *testing.B) {
6161
pagePaths[i] = fmt.Sprintf("sect%d", r.Intn(10))
6262
}
6363

64-
for i := 0; b.Loop(); i++ {
64+
for i := 0; i < b.N; i++ {
6565
home, _ := s.getPage(nil, "/")
6666
if home == nil {
6767
b.Fatal("Home is nil")
@@ -120,12 +120,12 @@ func BenchmarkGetPageRegular(b *testing.B) {
120120

121121
pagePaths := make([]string, b.N)
122122

123-
for i := 0; b.Loop(); i++ {
123+
for i := 0; i < b.N; i++ {
124124
pagePaths[i] = path.Join(fmt.Sprintf("/sect%d", r.Intn(10)), fmt.Sprintf("page%d.md", r.Intn(100)))
125125
}
126126

127127
b.ResetTimer()
128-
for i := 0; b.Loop(); i++ {
128+
for i := 0; i < b.N; i++ {
129129
page, _ := s.getPage(nil, pagePaths[i])
130130
c.Assert(page, qt.Not(qt.IsNil))
131131
}
@@ -139,13 +139,13 @@ func BenchmarkGetPageRegular(b *testing.B) {
139139
pagePaths := make([]string, b.N)
140140
pages := make([]page.Page, b.N)
141141

142-
for i := 0; b.Loop(); i++ {
142+
for i := 0; i < b.N; i++ {
143143
pagePaths[i] = fmt.Sprintf("page%d.md", r.Intn(100))
144144
pages[i] = allPages[r.Intn(len(allPages)/3)]
145145
}
146146

147147
b.ResetTimer()
148-
for i := 0; b.Loop(); i++ {
148+
for i := 0; i < b.N; i++ {
149149
page, _ := s.getPage(pages[i], pagePaths[i])
150150
c.Assert(page, qt.Not(qt.IsNil))
151151
}

hugolib/rebuild_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ func BenchmarkRebuildContentFileChange(b *testing.B) {
17971797
builders[i].Build()
17981798
}
17991799

1800-
for i := 0; b.Loop(); i++ {
1800+
for i := 0; i < b.N; i++ {
18011801
bb := builders[i]
18021802
bb.EditFileReplaceFunc("content/mysect/p123/index.md", func(s string) string {
18031803
return s + "... Edited"

hugolib/shortcode_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ title: "Shortcodes Galore!"
100100
{"closed without content", `{{< inner param1 >}}{{< / inner >}}`, regexpCheck("inner.*inner:{}")},
101101
{"inline", `{{< my.inline >}}Hi{{< /my.inline >}}`, regexpCheck("my.inline;inline:true;closing:true;inner:{Hi};")},
102102
} {
103-
104103
t.Run(test.name, func(t *testing.T) {
105104
t.Parallel()
106105
c := qt.New(t)
@@ -342,7 +341,7 @@ title: "Markdown Shortcode"
342341
builders[i] = NewIntegrationTestBuilder(cfg)
343342
}
344343

345-
for i := 0; b.Loop(); i++ {
344+
for i := 0; i < b.N; i++ {
346345
builders[i].Build()
347346
}
348347
}
@@ -715,7 +714,6 @@ TOC: {{ .TableOfContents }}
715714
)
716715
}
717716
})
718-
719717
}
720718
}
721719

hugolib/site_benchmark_new_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ func BenchmarkSiteNew(b *testing.B) {
504504
}
505505
b.Run(name, func(b *testing.B) {
506506
sites := make([]*sitesBuilder, b.N)
507-
for i := 0; b.Loop(); i++ {
507+
for i := 0; i < b.N; i++ {
508508
sites[i] = bm.create(b)
509509
if edit {
510510
sites[i].Running()

hugolib/sitesmatrix/sitematrix_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,11 +1125,11 @@ func BenchmarkSitesMatrixContent(b *testing.B) {
11251125
for _, multipleDimensions := range []bool{false, true} {
11261126
b.Run(fmt.Sprintf("n%d/md%t", numPages, multipleDimensions), func(b *testing.B) {
11271127
builders := make([]*hugolib.IntegrationTestBuilder, b.N)
1128-
for i := 0; b.Loop(); i++ {
1128+
for i := 0; i < b.N; i++ {
11291129
builders[i] = newSitesMatrixContentBenchmarkBuilder(b, numPages, true, true)
11301130
}
11311131
b.ResetTimer()
1132-
for i := 0; b.Loop(); i++ {
1132+
for i := 0; i < b.N; i++ {
11331133
builders[i].Build()
11341134
}
11351135
})

0 commit comments

Comments
 (0)