|
| 1 | +// Copyright 2017-present The Hugo Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package hugolib |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "math/rand" |
| 19 | + "path/filepath" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/spf13/afero" |
| 24 | +) |
| 25 | + |
| 26 | +type siteBuildingBenchmarkConfig struct { |
| 27 | + NumPages int |
| 28 | + RootSections int |
| 29 | + Render bool |
| 30 | + Shortcodes bool |
| 31 | + TagsPerPage int |
| 32 | +} |
| 33 | + |
| 34 | +func (s siteBuildingBenchmarkConfig) String() string { |
| 35 | + return fmt.Sprintf("num_root_sections=%d|num_pages=%d|tags_per_page=%d|shortcodes=%t|render=%t", s.RootSections, s.NumPages, s.TagsPerPage, s.Shortcodes, s.Render) |
| 36 | +} |
| 37 | + |
| 38 | +func BenchmarkSiteBuilding(b *testing.B) { |
| 39 | + var conf siteBuildingBenchmarkConfig |
| 40 | + for _, rootSections := range []int{1, 5} { |
| 41 | + conf.RootSections = rootSections |
| 42 | + for _, tagsPerPage := range []int{0, 1, 5, 20} { |
| 43 | + conf.TagsPerPage = tagsPerPage |
| 44 | + for _, numPages := range []int{10, 100, 500, 1000, 5000} { |
| 45 | + conf.NumPages = numPages |
| 46 | + for _, render := range []bool{false, true} { |
| 47 | + conf.Render = render |
| 48 | + for _, shortcodes := range []bool{false, true} { |
| 49 | + conf.Shortcodes = shortcodes |
| 50 | + doBenchMarkSiteBuilding(conf, b) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func doBenchMarkSiteBuilding(conf siteBuildingBenchmarkConfig, b *testing.B) { |
| 59 | + b.Run(conf.String(), func(b *testing.B) { |
| 60 | + sites := createHugoBenchmarkSites(b, b.N, conf) |
| 61 | + b.ResetTimer() |
| 62 | + for i := 0; i < b.N; i++ { |
| 63 | + h := sites[0] |
| 64 | + |
| 65 | + err := h.Build(BuildCfg{SkipRender: !conf.Render}) |
| 66 | + if err != nil { |
| 67 | + b.Fatal(err) |
| 68 | + } |
| 69 | + |
| 70 | + // Try to help the GC |
| 71 | + sites[0] = nil |
| 72 | + sites = sites[1:len(sites)] |
| 73 | + } |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +func createHugoBenchmarkSites(b *testing.B, count int, cfg siteBuildingBenchmarkConfig) []*HugoSites { |
| 78 | + someMarkdown := ` |
| 79 | +An h1 header |
| 80 | +============ |
| 81 | +
|
| 82 | +Paragraphs are separated by a blank line. |
| 83 | +
|
| 84 | +2nd paragraph. *Italic* and **bold**. Itemized lists |
| 85 | +look like: |
| 86 | +
|
| 87 | + * this one |
| 88 | + * that one |
| 89 | + * the other one |
| 90 | +
|
| 91 | +Note that --- not considering the asterisk --- the actual text |
| 92 | +content starts at 4-columns in. |
| 93 | +
|
| 94 | +> Block quotes are |
| 95 | +> written like so. |
| 96 | +> |
| 97 | +> They can span multiple paragraphs, |
| 98 | +> if you like. |
| 99 | +
|
| 100 | +Use 3 dashes for an em-dash. Use 2 dashes for ranges (ex., "it's all |
| 101 | +in chapters 12--14"). Three dots ... will be converted to an ellipsis. |
| 102 | +Unicode is supported. ☺ |
| 103 | +` |
| 104 | + |
| 105 | + someMarkdownWithShortCode := someMarkdown + ` |
| 106 | +
|
| 107 | +{{< myShortcode >}} |
| 108 | +
|
| 109 | +` |
| 110 | + |
| 111 | + pageTemplate := `+++ |
| 112 | +title = "%s" |
| 113 | +tags = %s |
| 114 | ++++ |
| 115 | +%s |
| 116 | +
|
| 117 | +` |
| 118 | + |
| 119 | + siteConfig := ` |
| 120 | +baseURL = "http://example.com/blog" |
| 121 | +
|
| 122 | +paginate = 10 |
| 123 | +defaultContentLanguage = "en" |
| 124 | +
|
| 125 | +[Taxonomies] |
| 126 | +tag = "tags" |
| 127 | +category = "categories" |
| 128 | +` |
| 129 | + var ( |
| 130 | + contentPagesContent [3]string |
| 131 | + tags = make([]string, cfg.TagsPerPage) |
| 132 | + ) |
| 133 | + |
| 134 | + for i := 0; i < len(tags); i++ { |
| 135 | + tags[i] = fmt.Sprintf("Hugo %d", i) |
| 136 | + } |
| 137 | + |
| 138 | + tagsStr := "[]" |
| 139 | + if cfg.TagsPerPage > 0 { |
| 140 | + tagsStr = strings.Replace(fmt.Sprintf("%q", tags[0:cfg.TagsPerPage]), " ", ", ", -1) |
| 141 | + } |
| 142 | + |
| 143 | + if cfg.Shortcodes { |
| 144 | + contentPagesContent = [3]string{ |
| 145 | + someMarkdownWithShortCode, |
| 146 | + strings.Repeat(someMarkdownWithShortCode, 2), |
| 147 | + strings.Repeat(someMarkdownWithShortCode, 3), |
| 148 | + } |
| 149 | + } else { |
| 150 | + contentPagesContent = [3]string{ |
| 151 | + someMarkdown, |
| 152 | + strings.Repeat(someMarkdown, 2), |
| 153 | + strings.Repeat(someMarkdown, 3), |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + sites := make([]*HugoSites, count) |
| 158 | + for i := 0; i < count; i++ { |
| 159 | + // Maybe consider reusing the Source fs |
| 160 | + mf := afero.NewMemMapFs() |
| 161 | + th, h := newTestSitesFromConfig(b, mf, siteConfig, |
| 162 | + "layouts/_default/single.html", `Single HTML|{{ .Title }}|{{ .Content }}`, |
| 163 | + "layouts/_default/list.html", `List HTML|{{ .Title }}|{{ .Content }}`, |
| 164 | + "layouts/shortcodes/myShortcode.html", `<p>MyShortcode</p>`) |
| 165 | + |
| 166 | + fs := th.Fs |
| 167 | + |
| 168 | + pagesPerSection := cfg.NumPages / cfg.RootSections |
| 169 | + |
| 170 | + for i := 0; i < cfg.RootSections; i++ { |
| 171 | + for j := 0; j < pagesPerSection; j++ { |
| 172 | + content := fmt.Sprintf(pageTemplate, fmt.Sprintf("Title%d_%d", i, j), tagsStr, contentPagesContent[rand.Intn(3)]) |
| 173 | + |
| 174 | + writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), content) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + sites[i] = h |
| 179 | + } |
| 180 | + |
| 181 | + return sites |
| 182 | +} |
0 commit comments