Skip to content

Commit 4c7a78f

Browse files
committed
testing: Revise usage of b.N and b.Loop() in benchmarks
1 parent 91eac9e commit 4c7a78f

20 files changed

+128
-736
lines changed

‎common/hreflect/helpers_test.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,18 @@ func TestIndirectInterface(t *testing.T) {
152152
}
153153

154154
func BenchmarkIsContextType(b *testing.B) {
155+
const size = 1000
155156
type k string
156157
b.Run("value", func(b *testing.B) {
157158
ctx := context.Background()
158-
ctxs := make([]reflect.Type, b.N)
159-
for i := 0; b.Loop(); i++ {
159+
ctxs := make([]reflect.Type, size)
160+
for i := range size {
160161
ctxs[i] = reflect.TypeOf(context.WithValue(ctx, k("key"), i))
161162
}
162163

163-
b.ResetTimer()
164-
for i := 0; i < b.N; i++ {
165-
if !IsContextType(ctxs[i]) {
164+
for i := 0; b.Loop(); i++ {
165+
idx := i % size
166+
if !IsContextType(ctxs[idx]) {
166167
b.Fatal("not context")
167168
}
168169
}

‎common/hstrings/strings_test.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func TestUniqueStringsSorted(t *testing.T) {
7171
c.Assert(UniqueStringsSorted(nil), qt.IsNil)
7272
}
7373

74+
// Note that these cannot use b.Loop() because of golang/go#27217.
7475
func BenchmarkUniqueStrings(b *testing.B) {
7576
input := []string{"a", "b", "d", "e", "d", "h", "a", "i"}
7677

@@ -84,15 +85,14 @@ func BenchmarkUniqueStrings(b *testing.B) {
8485
})
8586

8687
b.Run("Reuse slice", func(b *testing.B) {
87-
b.StopTimer()
8888
inputs := make([][]string, b.N)
89-
for i := 0; b.Loop(); i++ {
89+
for i := 0; i < b.N; i++ {
9090
inputc := make([]string, len(input))
9191
copy(inputc, input)
9292
inputs[i] = inputc
9393
}
94-
b.StartTimer()
95-
for i := 0; b.Loop(); i++ {
94+
b.ResetTimer()
95+
for i := 0; i < b.N; i++ {
9696
inputc := inputs[i]
9797

9898
result := UniqueStringsReuse(inputc)
@@ -103,14 +103,13 @@ func BenchmarkUniqueStrings(b *testing.B) {
103103
})
104104

105105
b.Run("Reuse slice sorted", func(b *testing.B) {
106-
b.StopTimer()
107106
inputs := make([][]string, b.N)
108107
for i := 0; i < b.N; i++ {
109108
inputc := make([]string, len(input))
110109
copy(inputc, input)
111110
inputs[i] = inputc
112111
}
113-
b.StartTimer()
112+
b.ResetTimer()
114113
for i := 0; i < b.N; i++ {
115114
inputc := inputs[i]
116115

‎compare/compare_strings_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestLexicographicSort(t *testing.T) {
6363
c.Assert(s, qt.DeepEquals, []string{"A", "b", "Ba", "ba", "ba", "Bz"})
6464
}
6565

66+
// // Note that this cannot use b.Loop() because of golang/go#27217.
6667
func BenchmarkStringSort(b *testing.B) {
6768
prototype := []string{"b", "Bz", "zz", "ba", "αβδ αβδ αβδ", "A", "Ba", "ba", "nnnnasdfnnn", "AAgæåz", "αβδC"}
6869
b.Run("LessStrings", func(b *testing.B) {

‎hugolib/cascade_test.go‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@ func BenchmarkCascade(b *testing.B) {
3434
langs := allLangs[0:i]
3535
b.Run(fmt.Sprintf("langs-%d", len(langs)), func(b *testing.B) {
3636
c := qt.New(b)
37-
b.StopTimer()
38-
builders := make([]*sitesBuilder, b.N)
39-
for i := 0; b.Loop(); i++ {
40-
builders[i] = newCascadeTestBuilder(b, langs)
41-
}
42-
b.StartTimer()
4337

44-
for i := 0; b.Loop(); i++ {
45-
builder := builders[i]
38+
for b.Loop() {
39+
b.StopTimer()
40+
builder := newCascadeTestBuilder(b, langs)
41+
b.StartTimer()
4642
err := builder.BuildE(BuildCfg{})
4743
c.Assert(err, qt.IsNil)
4844
first := builder.H.Sites[0]
@@ -75,16 +71,13 @@ kind = '{section,term}'
7571
T: b,
7672
TxtarString: files,
7773
}
78-
builders := make([]*IntegrationTestBuilder, b.N)
79-
80-
for i := range builders {
81-
builders[i] = NewIntegrationTestBuilder(cfg)
82-
}
83-
8474
b.ResetTimer()
8575

86-
for i := 0; i < b.N; i++ {
87-
builders[i].Build()
76+
for b.Loop() {
77+
b.StopTimer()
78+
builder := NewIntegrationTestBuilder(cfg)
79+
b.StartTimer()
80+
builder.Build()
8881
}
8982
})
9083
}

‎hugolib/hugo_smoke_test.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -743,14 +743,12 @@ func BenchmarkBaseline(b *testing.B) {
743743
T: b,
744744
TxtarString: benchmarkBaselineFiles(false),
745745
}
746-
builders := make([]*IntegrationTestBuilder, b.N)
747746

748-
for i := range builders {
749-
builders[i] = NewIntegrationTestBuilder(cfg)
750-
}
751-
752-
for i := 0; i < b.N; i++ {
753-
builders[i].Build()
747+
for b.Loop() {
748+
b.StopTimer()
749+
builder := NewIntegrationTestBuilder(cfg)
750+
b.StartTimer()
751+
builder.Build()
754752
}
755753
}
756754

‎hugolib/page_permalink_test.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func TestPermalink(t *testing.T) {
6464
}
6565

6666
for i, test := range tests {
67-
test := test
6867
t.Run(fmt.Sprintf("%s-%d", test.file, i), func(t *testing.T) {
6968
t.Parallel()
7069
c := qt.New(t)

‎hugolib/pagecollections_test.go‎

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,35 @@ categories:
3737
`
3838

3939
func BenchmarkGetPage(b *testing.B) {
40-
var (
41-
cfg, fs = newTestCfg()
42-
r = rand.New(rand.NewSource(time.Now().UnixNano()))
43-
)
40+
cfg, fs := newTestCfg()
4441

4542
configs, err := loadTestConfigFromProvider(cfg)
4643
if err != nil {
4744
b.Fatal(err)
4845
}
4946

50-
for i := range 10 {
47+
const size = 10
48+
49+
for i := range size {
5150
for j := range 100 {
5251
writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), "CONTENT")
5352
}
5453
}
5554

5655
s := buildSingleSite(b, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
5756

58-
pagePaths := make([]string, b.N)
59-
60-
for i := 0; b.Loop(); i++ {
61-
pagePaths[i] = fmt.Sprintf("sect%d", r.Intn(10))
57+
pagePaths := make([]string, size)
58+
for i := range size {
59+
pagePaths[i] = fmt.Sprintf("sect%d", i)
6260
}
6361

64-
for i := 0; i < b.N; i++ {
62+
for i := 0; b.Loop(); i++ {
6563
home, _ := s.getPage(nil, "/")
6664
if home == nil {
6765
b.Fatal("Home is nil")
6866
}
6967

70-
p, _ := s.getPage(nil, pagePaths[i])
68+
p, _ := s.getPage(nil, pagePaths[i%size])
7169
if p == nil {
7270
b.Fatal("Section is nil")
7371
}
@@ -113,20 +111,21 @@ func TestBenchmarkGetPageRegular(t *testing.T) {
113111

114112
func BenchmarkGetPageRegular(b *testing.B) {
115113
r := rand.New(rand.NewSource(time.Now().UnixNano()))
114+
const size = 100
116115

117116
b.Run("From root", func(b *testing.B) {
118117
s := createGetPageRegularBenchmarkSite(b)
119118
c := qt.New(b)
120119

121-
pagePaths := make([]string, b.N)
120+
pagePaths := make([]string, size)
122121

123-
for i := 0; i < b.N; i++ {
124-
pagePaths[i] = path.Join(fmt.Sprintf("/sect%d", r.Intn(10)), fmt.Sprintf("page%d.md", r.Intn(100)))
122+
for i := range size {
123+
pagePaths[i] = path.Join(fmt.Sprintf("/sect%d", r.Intn(10)), fmt.Sprintf("page%d.md", i))
125124
}
126125

127126
b.ResetTimer()
128-
for i := 0; i < b.N; i++ {
129-
page, _ := s.getPage(nil, pagePaths[i])
127+
for i := 0; b.Loop(); i++ {
128+
page, _ := s.getPage(nil, pagePaths[i%size])
130129
c.Assert(page, qt.Not(qt.IsNil))
131130
}
132131
})
@@ -136,17 +135,15 @@ func BenchmarkGetPageRegular(b *testing.B) {
136135
c := qt.New(b)
137136
allPages := s.RegularPages()
138137

139-
pagePaths := make([]string, b.N)
140-
pages := make([]page.Page, b.N)
138+
pagePaths := make([]string, size)
139+
pages := allPages[:size]
141140

142-
for i := 0; i < b.N; i++ {
143-
pagePaths[i] = fmt.Sprintf("page%d.md", r.Intn(100))
144-
pages[i] = allPages[r.Intn(len(allPages)/3)]
141+
for i := range size {
142+
pagePaths[i] = fmt.Sprintf("page%d.md", i)
145143
}
146144

147-
b.ResetTimer()
148-
for i := 0; i < b.N; i++ {
149-
page, _ := s.getPage(pages[i], pagePaths[i])
145+
for i := 0; b.Loop(); i++ {
146+
page, _ := s.getPage(pages[i%size], pagePaths[i%size])
150147
c.Assert(page, qt.Not(qt.IsNil))
151148
}
152149
})

‎hugolib/rebuild_test.go‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,22 +1787,15 @@ func BenchmarkRebuildContentFileChange(b *testing.B) {
17871787
T: b,
17881788
TxtarString: files,
17891789
Running: true,
1790-
// Verbose: true,
1791-
// LogLevel: logg.LevelInfo,
17921790
}
1793-
builders := make([]*IntegrationTestBuilder, b.N)
17941791

1795-
for i := range builders {
1796-
builders[i] = NewIntegrationTestBuilder(cfg)
1797-
builders[i].Build()
1798-
}
1799-
1800-
for i := 0; i < b.N; i++ {
1801-
bb := builders[i]
1792+
for b.Loop() {
1793+
b.StopTimer()
1794+
bb := NewIntegrationTestBuilder(cfg).Build()
1795+
b.StartTimer()
18021796
bb.EditFileReplaceFunc("content/mysect/p123/index.md", func(s string) string {
18031797
return s + "... Edited"
18041798
}).Build()
1805-
// fmt.Println(bb.LogString())
18061799
}
18071800
}
18081801

‎hugolib/shortcode_test.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ CSV: {{< myShort >}}
242242
)
243243
}
244244

245+
// Note that this cannot use b.Loop() because of golang/go#27217.
245246
func BenchmarkReplaceShortcodeTokens(b *testing.B) {
246247
type input struct {
247248
in []byte
@@ -263,7 +264,7 @@ func BenchmarkReplaceShortcodeTokens(b *testing.B) {
263264

264265
cnt := 0
265266
in := make([]input, b.N*len(data))
266-
for b.Loop() {
267+
for i := 0; i < b.N; i++ {
267268
for _, this := range data {
268269
replacements := make(map[string]shortcodeRenderer)
269270
for k, v := range this.replacements {
@@ -279,7 +280,8 @@ func BenchmarkReplaceShortcodeTokens(b *testing.B) {
279280

280281
cnt = 0
281282
ctx := context.Background()
282-
for i := 0; b.Loop(); i++ {
283+
b.ResetTimer()
284+
for i := 0; i < b.N; i++ {
283285
for j := range data {
284286
currIn := in[cnt]
285287
cnt++
@@ -335,14 +337,12 @@ title: "Markdown Shortcode"
335337
T: b,
336338
TxtarString: files,
337339
}
338-
builders := make([]*IntegrationTestBuilder, b.N)
339340

340-
for i := range builders {
341-
builders[i] = NewIntegrationTestBuilder(cfg)
342-
}
343-
344-
for i := 0; i < b.N; i++ {
345-
builders[i].Build()
341+
for b.Loop() {
342+
b.StopTimer()
343+
builder := NewIntegrationTestBuilder(cfg)
344+
b.StartTimer()
345+
builder.Build()
346346
}
347347
}
348348

0 commit comments

Comments
 (0)