Add benchmark for content edits

This commit is contained in:
Bjørn Erik Pedersen 2020-02-17 08:45:14 +01:00
parent 56d0b65887
commit 1622510a5c
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F

View file

@ -22,6 +22,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/gohugoio/hugo/resources/page"
qt "github.com/frankban/quicktest" qt "github.com/frankban/quicktest"
) )
@ -370,26 +372,68 @@ func TestBenchmarkSiteNew(b *testing.T) {
} }
} }
// TODO(bep) eventually remove the old (too complicated setup).
func BenchmarkSiteNew(b *testing.B) { func BenchmarkSiteNew(b *testing.B) {
rnd := rand.New(rand.NewSource(32))
benchmarks := getBenchmarkSiteNewTestCases() benchmarks := getBenchmarkSiteNewTestCases()
for _, edit := range []bool{true, false} {
for _, bm := range benchmarks { for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) { name := bm.name
sites := make([]*sitesBuilder, b.N) if edit {
for i := 0; i < b.N; i++ { name += "/Edit"
sites[i] = bm.create(b)
} }
b.Run(name, func(b *testing.B) {
b.ResetTimer() sites := make([]*sitesBuilder, b.N)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
s := sites[i] sites[i] = bm.create(b)
err := s.BuildE(BuildCfg{}) if edit {
if err != nil { sites[i].Running()
b.Fatal(err) }
} }
bm.check(s)
} b.ResetTimer()
}) for i := 0; i < b.N; i++ {
if edit {
b.StopTimer()
}
s := sites[i]
err := s.BuildE(BuildCfg{})
if err != nil {
b.Fatal(err)
}
bm.check(s)
if edit {
if edit {
b.StartTimer()
}
// Edit a random page in a random language.
pages := s.H.Sites[rnd.Intn(len(s.H.Sites))].Pages()
var p page.Page
count := 0
for {
count++
if count > 100 {
panic("infinite loop")
}
p = pages[rnd.Intn(len(pages))]
if !p.File().IsZero() {
break
}
}
s.EditFiles(p.File().Filename(), fmt.Sprintf(`---
title: %s
---
Edited!!`, p.Title()))
err := s.BuildE(BuildCfg{})
if err != nil {
b.Fatal(err)
}
}
}
})
}
} }
} }