|
14 | 14 | package partials_test
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "bytes" |
| 18 | + "fmt" |
| 19 | + "regexp" |
| 20 | + "sort" |
| 21 | + "strings" |
17 | 22 | "testing"
|
18 | 23 |
|
| 24 | + "github.com/gohugoio/hugo/htesting/hqt" |
19 | 25 | "github.com/gohugoio/hugo/hugolib"
|
20 | 26 | )
|
21 | 27 |
|
@@ -68,3 +74,128 @@ partialCached: foo
|
68 | 74 | partialCached: foo
|
69 | 75 | `)
|
70 | 76 | }
|
| 77 | + |
| 78 | +func TestIncludeCacheHints(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + |
| 81 | + files := ` |
| 82 | +-- config.toml -- |
| 83 | +baseURL = 'http://example.com/' |
| 84 | +templateMetrics=true |
| 85 | +templateMetricsHints=true |
| 86 | +disableKinds = ["page", "section", "taxonomy", "term", "sitemap"] |
| 87 | +[outputs] |
| 88 | +home = ["HTML"] |
| 89 | +-- layouts/index.html -- |
| 90 | +{{ partials.IncludeCached "static1.html" . }} |
| 91 | +{{ partials.IncludeCached "static1.html" . }} |
| 92 | +{{ partials.Include "static2.html" . }} |
| 93 | +
|
| 94 | +D1I: {{ partials.Include "dynamic1.html" . }} |
| 95 | +D1C: {{ partials.IncludeCached "dynamic1.html" . }} |
| 96 | +D1C: {{ partials.IncludeCached "dynamic1.html" . }} |
| 97 | +D1C: {{ partials.IncludeCached "dynamic1.html" . }} |
| 98 | +H1I: {{ partials.Include "halfdynamic1.html" . }} |
| 99 | +H1C: {{ partials.IncludeCached "halfdynamic1.html" . }} |
| 100 | +H1C: {{ partials.IncludeCached "halfdynamic1.html" . }} |
| 101 | +
|
| 102 | +-- layouts/partials/static1.html -- |
| 103 | +P1 |
| 104 | +-- layouts/partials/static2.html -- |
| 105 | +P2 |
| 106 | +-- layouts/partials/dynamic1.html -- |
| 107 | +{{ math.Counter }} |
| 108 | +-- layouts/partials/halfdynamic1.html -- |
| 109 | +D1 |
| 110 | +{{ math.Counter }} |
| 111 | +
|
| 112 | +
|
| 113 | + ` |
| 114 | + |
| 115 | + b := hugolib.NewIntegrationTestBuilder( |
| 116 | + hugolib.IntegrationTestConfig{ |
| 117 | + T: t, |
| 118 | + TxtarString: files, |
| 119 | + }, |
| 120 | + ).Build() |
| 121 | + |
| 122 | + // fmt.Println(b.FileContent("public/index.html")) |
| 123 | + |
| 124 | + var buf bytes.Buffer |
| 125 | + b.H.Metrics.WriteMetrics(&buf) |
| 126 | + |
| 127 | + got := buf.String() |
| 128 | + |
| 129 | + // Get rid of all the durations, they are never the same. |
| 130 | + durationRe := regexp.MustCompile(`\b[\.\d]*(ms|µs|s)\b`) |
| 131 | + |
| 132 | + normalize := func(s string) string { |
| 133 | + s = durationRe.ReplaceAllString(s, "") |
| 134 | + linesIn := strings.Split(s, "\n")[3:] |
| 135 | + var lines []string |
| 136 | + for _, l := range linesIn { |
| 137 | + l = strings.TrimSpace(l) |
| 138 | + if l == "" { |
| 139 | + continue |
| 140 | + } |
| 141 | + lines = append(lines, l) |
| 142 | + } |
| 143 | + |
| 144 | + sort.Strings(lines) |
| 145 | + |
| 146 | + return strings.Join(lines, "\n") |
| 147 | + } |
| 148 | + |
| 149 | + got = normalize(got) |
| 150 | + |
| 151 | + expect := ` |
| 152 | + 0 0 0 1 index.html |
| 153 | + 100 0 0 1 partials/static2.html |
| 154 | + 100 50 1 2 partials/static1.html |
| 155 | + 25 50 2 4 partials/dynamic1.html |
| 156 | + 66 33 1 3 partials/halfdynamic1.html |
| 157 | + ` |
| 158 | + |
| 159 | + b.Assert(got, hqt.IsSameString, expect) |
| 160 | +} |
| 161 | + |
| 162 | +// gobench --package ./tpl/partials |
| 163 | +func BenchmarkIncludeCached(b *testing.B) { |
| 164 | + files := ` |
| 165 | +-- config.toml -- |
| 166 | +baseURL = 'http://example.com/' |
| 167 | +-- layouts/index.html -- |
| 168 | +-- layouts/_default/single.html -- |
| 169 | +{{ partialCached "heavy.html" "foo" }} |
| 170 | +-- layouts/partials/heavy.html -- |
| 171 | +{{ $result := slice }} |
| 172 | +{{ range site.RegularPages }} |
| 173 | +{{ $result = $result | append (dict "title" .Title "link" .RelPermalink "readingTime" .ReadingTime) }} |
| 174 | +{{ end }} |
| 175 | +{{ range $result }} |
| 176 | +* {{ .title }} {{ .link }} {{ .readingTime }} |
| 177 | +{{ end }} |
| 178 | +
|
| 179 | +
|
| 180 | +` |
| 181 | + |
| 182 | + for i := 1; i < 100; i++ { |
| 183 | + files += fmt.Sprintf("\n-- content/p%d.md --\n---\ntitle: page\n---\n"+strings.Repeat("FOO ", i), i) |
| 184 | + } |
| 185 | + |
| 186 | + cfg := hugolib.IntegrationTestConfig{ |
| 187 | + T: b, |
| 188 | + TxtarString: files, |
| 189 | + } |
| 190 | + builders := make([]*hugolib.IntegrationTestBuilder, b.N) |
| 191 | + |
| 192 | + for i, _ := range builders { |
| 193 | + builders[i] = hugolib.NewIntegrationTestBuilder(cfg) |
| 194 | + } |
| 195 | + |
| 196 | + b.ResetTimer() |
| 197 | + |
| 198 | + for i := 0; i < b.N; i++ { |
| 199 | + builders[i].Build() |
| 200 | + } |
| 201 | +} |
0 commit comments