// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hugolib
import (
"fmt"
"math/rand"
"path"
"path/filepath"
"testing"
"time"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/resources/kinds"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/deps"
)
const pageCollectionsPageTemplate = `---
title: "%s"
categories:
- Hugo
---
# Doc
`
func BenchmarkGetPage(b *testing.B) {
var (
cfg, fs = newTestCfg()
r = rand.New(rand.NewSource(time.Now().UnixNano()))
)
configs, err := loadTestConfigFromProvider(cfg)
if err != nil {
b.Fatal(err)
}
for i := 0; i < 10; i++ {
for j := 0; j < 100; j++ {
writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), "CONTENT")
}
}
s := buildSingleSite(b, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
pagePaths := make([]string, b.N)
for i := 0; i < b.N; i++ {
pagePaths[i] = fmt.Sprintf("sect%d", r.Intn(10))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
home, _ := s.getPage(nil, "/")
if home == nil {
b.Fatal("Home is nil")
}
p, _ := s.getPage(nil, pagePaths[i])
if p == nil {
b.Fatal("Section is nil")
}
}
}
func createGetPageRegularBenchmarkSite(t testing.TB) *Site {
var (
c = qt.New(t)
cfg, fs = newTestCfg()
)
configs, err := loadTestConfigFromProvider(cfg)
if err != nil {
t.Fatal(err)
}
pc := func(title string) string {
return fmt.Sprintf(pageCollectionsPageTemplate, title)
}
for i := 0; i < 10; i++ {
for j := 0; j < 100; j++ {
content := pc(fmt.Sprintf("Title%d_%d", i, j))
writeSource(c, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), content)
}
}
return buildSingleSite(c, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{SkipRender: true})
}
func TestBenchmarkGetPageRegular(t *testing.T) {
c := qt.New(t)
s := createGetPageRegularBenchmarkSite(t)
for i := 0; i < 10; i++ {
pp := path.Join("/", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", i))
page, _ := s.getPage(nil, pp)
c.Assert(page, qt.Not(qt.IsNil), qt.Commentf(pp))
}
}
func BenchmarkGetPageRegular(b *testing.B) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
b.Run("From root", func(b *testing.B) {
s := createGetPageRegularBenchmarkSite(b)
c := qt.New(b)
pagePaths := make([]string, b.N)
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