2017-05-30 03:01:54 -04:00
|
|
|
// Copyright 2017-present 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 (
|
2017-10-18 10:46:55 -04:00
|
|
|
"flag"
|
2017-05-30 03:01:54 -04:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
type siteBuildingBenchmarkConfig struct {
|
2017-05-30 11:10:48 -04:00
|
|
|
Frontmatter string
|
2017-05-30 03:01:54 -04:00
|
|
|
NumPages int
|
2017-12-02 05:44:18 -05:00
|
|
|
NumLangs int
|
2017-05-30 03:01:54 -04:00
|
|
|
RootSections int
|
|
|
|
Render bool
|
|
|
|
Shortcodes bool
|
2017-06-01 12:51:51 -04:00
|
|
|
NumTags int
|
2017-05-30 03:01:54 -04:00
|
|
|
TagsPerPage int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s siteBuildingBenchmarkConfig) String() string {
|
2017-05-31 03:35:02 -04:00
|
|
|
// Make it comma separated with no spaces, so it is both Bash and regexp friendly.
|
2017-06-01 06:00:47 -04:00
|
|
|
// To make it a short as possible, we only shows bools when enabled and ints when >= 0 (RootSections > 1)
|
|
|
|
sep := ","
|
|
|
|
id := s.Frontmatter + sep
|
2017-12-02 05:44:18 -05:00
|
|
|
id += fmt.Sprintf("num_langs=%d%s", s.NumLangs, sep)
|
|
|
|
|
2017-06-01 06:00:47 -04:00
|
|
|
if s.RootSections > 1 {
|
|
|
|
id += fmt.Sprintf("num_root_sections=%d%s", s.RootSections, sep)
|
|
|
|
}
|
|
|
|
id += fmt.Sprintf("num_pages=%d%s", s.NumPages, sep)
|
|
|
|
|
2017-06-01 12:51:51 -04:00
|
|
|
if s.NumTags > 0 {
|
|
|
|
id += fmt.Sprintf("num_tags=%d%s", s.NumTags, sep)
|
|
|
|
}
|
|
|
|
|
2017-06-01 06:00:47 -04:00
|
|
|
if s.TagsPerPage > 0 {
|
|
|
|
id += fmt.Sprintf("tags_per_page=%d%s", s.TagsPerPage, sep)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Shortcodes {
|
|
|
|
id += "shortcodes" + sep
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Render {
|
|
|
|
id += "render" + sep
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSuffix(id, sep)
|
2017-05-30 03:01:54 -04:00
|
|
|
}
|
|
|
|
|
2017-12-02 05:44:18 -05:00
|
|
|
var someLangs = []string{"en", "fr", "nn"}
|
|
|
|
|
2017-05-30 03:01:54 -04:00
|
|
|
func BenchmarkSiteBuilding(b *testing.B) {
|
2017-10-18 10:46:55 -04:00
|
|
|
var (
|
|
|
|
// The below represents the full matrix of benchmarks. Big!
|
|
|
|
allFrontmatters = []string{"YAML", "TOML"}
|
|
|
|
allNumRootSections = []int{1, 5}
|
|
|
|
allNumTags = []int{0, 1, 10, 20, 50, 100, 500, 1000, 5000}
|
|
|
|
allTagsPerPage = []int{0, 1, 5, 20, 50, 80}
|
|
|
|
allNumPages = []int{1, 10, 100, 500, 1000, 5000, 10000}
|
|
|
|
allDoRender = []bool{false, true}
|
|
|
|
allDoShortCodes = []bool{false, true}
|
2017-12-02 05:44:18 -05:00
|
|
|
allNumLangs = []int{1, 3}
|
2017-10-18 10:46:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var runDefault bool
|
|
|
|
|
|
|
|
visitor := func(a *flag.Flag) {
|
|
|
|
if a.Name == "test.bench" && len(a.Value.String()) < 40 {
|
|
|
|
// The full suite is too big, so fall back to some smaller default if no
|
|
|
|
// restriction is set.
|
|
|
|
runDefault = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flag.Visit(visitor)
|
|
|
|
|
|
|
|
if runDefault {
|
|
|
|
allFrontmatters = allFrontmatters[1:]
|
|
|
|
allNumRootSections = allNumRootSections[0:2]
|
|
|
|
allNumTags = allNumTags[0:2]
|
|
|
|
allTagsPerPage = allTagsPerPage[2:3]
|
|
|
|
allNumPages = allNumPages[2:5]
|
|
|
|
allDoRender = allDoRender[1:2]
|
|
|
|
allDoShortCodes = allDoShortCodes[1:2]
|
|
|
|
}
|
|
|
|
|
2017-05-30 03:01:54 -04:00
|
|
|
var conf siteBuildingBenchmarkConfig
|
2017-12-02 05:44:18 -05:00
|
|
|
for _, numLangs := range allNumLangs {
|
|
|
|
conf.NumLangs = numLangs
|
|
|
|
for _, frontmatter := range allFrontmatters {
|
|
|
|
conf.Frontmatter = frontmatter
|
|
|
|
for _, rootSections := range allNumRootSections {
|
|
|
|
conf.RootSections = rootSections
|
|
|
|
for _, numTags := range allNumTags {
|
|
|
|
conf.NumTags = numTags
|
|
|
|
for _, tagsPerPage := range allTagsPerPage {
|
|
|
|
conf.TagsPerPage = tagsPerPage
|
|
|
|
for _, numPages := range allNumPages {
|
|
|
|
conf.NumPages = numPages
|
|
|
|
for _, render := range allDoRender {
|
|
|
|
conf.Render = render
|
|
|
|
for _, shortcodes := range allDoShortCodes {
|
|
|
|
conf.Shortcodes = shortcodes
|
|
|
|
doBenchMarkSiteBuilding(conf, b)
|
|
|
|
}
|
2017-06-01 12:51:51 -04:00
|
|
|
}
|
2017-05-30 11:10:48 -04:00
|
|
|
}
|
2017-05-30 03:01:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doBenchMarkSiteBuilding(conf siteBuildingBenchmarkConfig, b *testing.B) {
|
|
|
|
b.Run(conf.String(), func(b *testing.B) {
|
2017-12-05 10:17:11 -05:00
|
|
|
b.StopTimer()
|
2017-05-30 03:01:54 -04:00
|
|
|
sites := createHugoBenchmarkSites(b, b.N, conf)
|
2017-12-05 10:17:11 -05:00
|
|
|
b.StartTimer()
|
2017-05-30 03:01:54 -04:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
h := sites[0]
|
|
|
|
|
|
|
|
err := h.Build(BuildCfg{SkipRender: !conf.Render})
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to help the GC
|
|
|
|
sites[0] = nil
|
2017-08-07 14:03:15 -04:00
|
|
|
sites = sites[1:]
|
2017-05-30 03:01:54 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func createHugoBenchmarkSites(b *testing.B, count int, cfg siteBuildingBenchmarkConfig) []*HugoSites {
|
|
|
|
someMarkdown := `
|
|
|
|
An h1 header
|
|
|
|
============
|
|
|
|
|
|
|
|
Paragraphs are separated by a blank line.
|
|
|
|
|
|
|
|
2nd paragraph. *Italic* and **bold**. Itemized lists
|
|
|
|
look like:
|
|
|
|
|
|
|
|
* this one
|
|
|
|
* that one
|
|
|
|
* the other one
|
|
|
|
|
|
|
|
Note that --- not considering the asterisk --- the actual text
|
|
|
|
content starts at 4-columns in.
|
|
|
|
|
|
|
|
> Block quotes are
|
|
|
|
> written like so.
|
|
|
|
>
|
|
|
|
> They can span multiple paragraphs,
|
|
|
|
> if you like.
|
|
|
|
|
|
|
|
Use 3 dashes for an em-dash. Use 2 dashes for ranges (ex., "it's all
|
|
|
|
in chapters 12--14"). Three dots ... will be converted to an ellipsis.
|
|
|
|
Unicode is supported. ☺
|
|
|
|
`
|
|
|
|
|
|
|
|
someMarkdownWithShortCode := someMarkdown + `
|
|
|
|
|
|
|
|
{{< myShortcode >}}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2017-05-30 11:10:48 -04:00
|
|
|
pageTemplateTOML := `+++
|
2017-05-30 03:01:54 -04:00
|
|
|
title = "%s"
|
|
|
|
tags = %s
|
|
|
|
+++
|
|
|
|
%s
|
|
|
|
|
2017-05-30 11:10:48 -04:00
|
|
|
`
|
|
|
|
|
|
|
|
pageTemplateYAML := `---
|
|
|
|
title: "%s"
|
|
|
|
tags:
|
|
|
|
%s
|
|
|
|
---
|
|
|
|
%s
|
|
|
|
|
2017-05-30 03:01:54 -04:00
|
|
|
`
|
|
|
|
|
|
|
|
siteConfig := `
|
|
|
|
baseURL = "http://example.com/blog"
|
|
|
|
|
|
|
|
paginate = 10
|
|
|
|
defaultContentLanguage = "en"
|
|
|
|
|
2017-12-02 06:05:12 -05:00
|
|
|
[outputs]
|
|
|
|
home = [ "HTML" ]
|
|
|
|
section = [ "HTML" ]
|
|
|
|
taxonomy = [ "HTML" ]
|
|
|
|
taxonomyTerm = [ "HTML" ]
|
|
|
|
page = [ "HTML" ]
|
|
|
|
|
2017-12-02 05:44:18 -05:00
|
|
|
[languages]
|
|
|
|
%s
|
|
|
|
|
2017-05-30 03:01:54 -04:00
|
|
|
[Taxonomies]
|
|
|
|
tag = "tags"
|
|
|
|
category = "categories"
|
|
|
|
`
|
2017-06-02 03:13:45 -04:00
|
|
|
|
2017-12-02 05:44:18 -05:00
|
|
|
langConfigTemplate := `
|
|
|
|
[languages.%s]
|
|
|
|
languageName = "Lang %s"
|
|
|
|
weight = %d
|
|
|
|
`
|
|
|
|
|
|
|
|
langConfig := ""
|
|
|
|
|
|
|
|
for i := 0; i < cfg.NumLangs; i++ {
|
|
|
|
langCode := someLangs[i]
|
|
|
|
langConfig += fmt.Sprintf(langConfigTemplate, langCode, langCode, i+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
siteConfig = fmt.Sprintf(siteConfig, langConfig)
|
|
|
|
|
2017-06-02 03:13:45 -04:00
|
|
|
numTags := cfg.NumTags
|
|
|
|
|
|
|
|
if cfg.TagsPerPage > numTags {
|
|
|
|
numTags = cfg.TagsPerPage
|
|
|
|
}
|
|
|
|
|
2017-05-30 03:01:54 -04:00
|
|
|
var (
|
|
|
|
contentPagesContent [3]string
|
2017-06-02 03:13:45 -04:00
|
|
|
tags = make([]string, numTags)
|
2017-05-30 11:10:48 -04:00
|
|
|
pageTemplate string
|
2017-05-30 03:01:54 -04:00
|
|
|
)
|
|
|
|
|
2017-06-02 03:13:45 -04:00
|
|
|
for i := 0; i < numTags; i++ {
|
2017-06-01 12:51:51 -04:00
|
|
|
tags[i] = fmt.Sprintf("Hugo %d", i+1)
|
2017-05-30 03:01:54 -04:00
|
|
|
}
|
|
|
|
|
2017-05-30 11:10:48 -04:00
|
|
|
var tagsStr string
|
|
|
|
|
2017-05-30 03:01:54 -04:00
|
|
|
if cfg.Shortcodes {
|
|
|
|
contentPagesContent = [3]string{
|
|
|
|
someMarkdownWithShortCode,
|
|
|
|
strings.Repeat(someMarkdownWithShortCode, 2),
|
|
|
|
strings.Repeat(someMarkdownWithShortCode, 3),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contentPagesContent = [3]string{
|
|
|
|
someMarkdown,
|
|
|
|
strings.Repeat(someMarkdown, 2),
|
|
|
|
strings.Repeat(someMarkdown, 3),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sites := make([]*HugoSites, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
// Maybe consider reusing the Source fs
|
|
|
|
mf := afero.NewMemMapFs()
|
|
|
|
th, h := newTestSitesFromConfig(b, mf, siteConfig,
|
2018-01-24 03:08:26 -05:00
|
|
|
"layouts/_default/single.html", `Single HTML|{{ .Title }}|{{ .Content }}|{{ partial "myPartial" . }}`,
|
|
|
|
"layouts/_default/list.html", `List HTML|{{ .Title }}|{{ .Content }}|GetPage: {{ with .Site.GetPage "page" "sect3/page3.md" }}{{ .Title }}{{ end }}`,
|
2017-10-18 10:46:55 -04:00
|
|
|
"layouts/partials/myPartial.html", `Partial: {{ "Hello **world**!" | markdownify }}`,
|
2017-05-30 03:01:54 -04:00
|
|
|
"layouts/shortcodes/myShortcode.html", `<p>MyShortcode</p>`)
|
|
|
|
|
|
|
|
fs := th.Fs
|
|
|
|
|
2017-12-02 05:44:18 -05:00
|
|
|
pagesPerSection := cfg.NumPages / cfg.RootSections / cfg.NumLangs
|
|
|
|
for li := 0; li < cfg.NumLangs; li++ {
|
|
|
|
fileLangCodeID := ""
|
|
|
|
if li > 0 {
|
|
|
|
fileLangCodeID = "." + someLangs[li] + "."
|
|
|
|
}
|
2017-05-30 03:01:54 -04:00
|
|
|
|
2017-12-02 05:44:18 -05:00
|
|
|
for i := 0; i < cfg.RootSections; i++ {
|
|
|
|
for j := 0; j < pagesPerSection; j++ {
|
|
|
|
var tagsSlice []string
|
2017-06-02 03:13:45 -04:00
|
|
|
|
2017-12-02 05:44:18 -05:00
|
|
|
if numTags > 0 {
|
|
|
|
tagsStart := rand.Intn(numTags) - cfg.TagsPerPage
|
|
|
|
if tagsStart < 0 {
|
|
|
|
tagsStart = 0
|
|
|
|
}
|
|
|
|
tagsSlice = tags[tagsStart : tagsStart+cfg.TagsPerPage]
|
2017-06-02 03:13:45 -04:00
|
|
|
}
|
|
|
|
|
2017-12-02 05:44:18 -05:00
|
|
|
if cfg.Frontmatter == "TOML" {
|
|
|
|
pageTemplate = pageTemplateTOML
|
|
|
|
tagsStr = "[]"
|
|
|
|
if cfg.TagsPerPage > 0 {
|
|
|
|
tagsStr = strings.Replace(fmt.Sprintf("%q", tagsSlice), " ", ", ", -1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// YAML
|
|
|
|
pageTemplate = pageTemplateYAML
|
|
|
|
for _, tag := range tagsSlice {
|
|
|
|
tagsStr += "\n- " + tag
|
|
|
|
}
|
2017-06-01 12:51:51 -04:00
|
|
|
}
|
|
|
|
|
2017-12-02 05:44:18 -05:00
|
|
|
content := fmt.Sprintf(pageTemplate, fmt.Sprintf("Title%d_%d", i, j), tagsStr, contentPagesContent[rand.Intn(3)])
|
|
|
|
|
|
|
|
contentFilename := fmt.Sprintf("page%d%s.md", j, fileLangCodeID)
|
|
|
|
|
|
|
|
writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), contentFilename), content)
|
|
|
|
}
|
2017-05-30 03:01:54 -04:00
|
|
|
|
2017-12-02 05:44:18 -05:00
|
|
|
content := fmt.Sprintf(pageTemplate, fmt.Sprintf("Section %d", i), "[]", contentPagesContent[rand.Intn(3)])
|
|
|
|
indexContentFilename := fmt.Sprintf("_index%s.md", fileLangCodeID)
|
|
|
|
writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), indexContentFilename), content)
|
2017-05-30 03:01:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sites[i] = h
|
|
|
|
}
|
|
|
|
|
|
|
|
return sites
|
|
|
|
}
|