-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgax_bench_test.go
356 lines (316 loc) · 12.5 KB
/
gax_bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package gax
import (
"html/template"
"io"
"regexp"
"strings"
"testing"
"time"
)
func Test_static_gax(t *testing.T) {
eqs(
t,
renderedStatic,
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="icon" href="data:;base64,="><title>test markup</title></head><body class="stretch-to-viewport"><h1 class="title">mock markup</h1><div class="main">hello world!</div></body></html>`,
)
}
func Test_static_template(t *testing.T) {
eq(
t,
templateToString(tplStatic, nil),
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="icon" href="data:;base64,="><title>test markup</title></head><body class="stretch-to-viewport"><h1 class="title">mock markup</h1><div class="main">hello world!</div></body></html>`,
)
}
func Test_static_equiv(t *testing.T) {
eqs(
t,
renderedStatic,
templateToString(tplStatic, nil),
)
}
func Test_dynamic_gax(t *testing.T) {
eqs(
t,
renderDynamic(mockDat),
`<!doctype html><html lang="en"><head><link rel="icon" href="data:;base64,="><title>Posts</title><meta property="og:title" content="Posts"><meta name="description" content="Random notes and thoughts"></head><body><nav><a href="/">home</a><a href="/works">works</a><a href="/posts" aria-current="page">posts</a><a href="/demos">demos</a><span>Updated Apr 05 3123</span></nav><div role="main"><h1>Posts</h1><h2><a href="/posts/post1.html">post 1</a></h2><h2><a href="/posts/post2.html">post 2</a></h2><h2><a href="/posts/post3.html">post 3</a></h2><h2><a href="/posts/post4.html">post 4</a></h2><h2><a href="/posts/post5.html">post 5</a></h2><h2><a href="/posts/post6.html">post 6</a></h2><h2><a href="/posts/post7.html">post 7</a></h2><h2><a href="/posts/post8.html">post 8</a></h2><h2><a href="/posts/post9.html">post 9</a></h2><h2><a href="/posts/post10.html">post 10</a></h2><h2><a href="/posts/post11.html">post 11</a></h2><h2><a href="/posts/post12.html">post 12</a></h2><h2><a href="/posts/post13.html">post 13</a></h2><h2><a href="/posts/post14.html">post 14</a></h2><h2><a href="/posts/post15.html">post 15</a></h2><h2><a href="/posts/post16.html">post 16</a></h2><h2><a href="/posts/post17.html">post 17</a></h2><h2><a href="/posts/post18.html">post 18</a></h2><h2><a href="/posts/post19.html">post 19</a></h2><h2><a href="/posts/post20.html">post 20</a></h2><h2><a href="/posts/post21.html">post 21</a></h2><h2><a href="/posts/post22.html">post 22</a></h2><h2><a href="/posts/post23.html">post 23</a></h2><h2><a href="/posts/post24.html">post 24</a></h2></div></body></html>`,
)
}
func Test_dynamic_template(t *testing.T) {
eq(
t,
templateToString(tplDynamic, mockDat),
`<!doctype html><html lang="en"><head><link rel="icon" href="data:;base64,="><title>Posts</title><meta property="og:title" content="Posts"><meta name="description" content="Random notes and thoughts"></head><body><nav><a href="/">home</a><a href="/works">works</a><a href="/posts" aria-current="page">posts</a><a href="/demos">demos</a><span>Updated Apr 05 3123</span></nav><div role="main"><h1>Posts</h1><h2><a href="/posts/post1.html">post 1</a></h2><h2><a href="/posts/post2.html">post 2</a></h2><h2><a href="/posts/post3.html">post 3</a></h2><h2><a href="/posts/post4.html">post 4</a></h2><h2><a href="/posts/post5.html">post 5</a></h2><h2><a href="/posts/post6.html">post 6</a></h2><h2><a href="/posts/post7.html">post 7</a></h2><h2><a href="/posts/post8.html">post 8</a></h2><h2><a href="/posts/post9.html">post 9</a></h2><h2><a href="/posts/post10.html">post 10</a></h2><h2><a href="/posts/post11.html">post 11</a></h2><h2><a href="/posts/post12.html">post 12</a></h2><h2><a href="/posts/post13.html">post 13</a></h2><h2><a href="/posts/post14.html">post 14</a></h2><h2><a href="/posts/post15.html">post 15</a></h2><h2><a href="/posts/post16.html">post 16</a></h2><h2><a href="/posts/post17.html">post 17</a></h2><h2><a href="/posts/post18.html">post 18</a></h2><h2><a href="/posts/post19.html">post 19</a></h2><h2><a href="/posts/post20.html">post 20</a></h2><h2><a href="/posts/post21.html">post 21</a></h2><h2><a href="/posts/post22.html">post 22</a></h2><h2><a href="/posts/post23.html">post 23</a></h2><h2><a href="/posts/post24.html">post 24</a></h2></div></body></html>`,
)
}
func Test_dynamic_equiv(t *testing.T) {
eqs(
t,
renderDynamic(mockDat),
templateToString(tplDynamic, mockDat),
)
}
func Benchmark_template_static(b *testing.B) {
for range iter(b.N) {
must(tplStatic.Execute(io.Discard, nil))
}
}
func Benchmark_gax_static(b *testing.B) {
for range iter(b.N) {
_, _ = io.Discard.Write(renderedStatic)
}
}
func Benchmark_template_dynamic(b *testing.B) {
for range iter(b.N) {
must(tplDynamic.Execute(io.Discard, mockDat))
}
}
func Benchmark_gax_dynamic(b *testing.B) {
for range iter(b.N) {
_ = renderDynamic(mockDat)
}
}
type MockDat struct {
Page
Posts []Post
}
type Page struct {
Path string
Title string
Desc string
Type string
Image string
ColorScheme string
Posts []Post
}
type Post struct {
Page
HtmlBody []byte
PublishedAt *time.Time
UpdatedAt *time.Time
IsListed bool
}
var mockDat = MockDat{
Page: Page{
Path: `posts.html`,
Title: `Posts`,
Desc: `Random notes and thoughts`,
},
Posts: []Post{
Post{Page: Page{Title: `post 0`, Path: `/posts/post0.html`}, IsListed: false},
Post{Page: Page{Title: `post 1`, Path: `/posts/post1.html`}, IsListed: true},
Post{Page: Page{Title: `post 2`, Path: `/posts/post2.html`}, IsListed: true},
Post{Page: Page{Title: `post 3`, Path: `/posts/post3.html`}, IsListed: true},
Post{Page: Page{Title: `post 4`, Path: `/posts/post4.html`}, IsListed: true},
Post{Page: Page{Title: `post 5`, Path: `/posts/post5.html`}, IsListed: true},
Post{Page: Page{Title: `post 6`, Path: `/posts/post6.html`}, IsListed: true},
Post{Page: Page{Title: `post 7`, Path: `/posts/post7.html`}, IsListed: true},
Post{Page: Page{Title: `post 8`, Path: `/posts/post8.html`}, IsListed: true},
Post{Page: Page{Title: `post 9`, Path: `/posts/post9.html`}, IsListed: true},
Post{Page: Page{Title: `post 10`, Path: `/posts/post10.html`}, IsListed: true},
Post{Page: Page{Title: `post 11`, Path: `/posts/post11.html`}, IsListed: true},
Post{Page: Page{Title: `post 12`, Path: `/posts/post12.html`}, IsListed: true},
Post{Page: Page{Title: `post 13`, Path: `/posts/post13.html`}, IsListed: true},
Post{Page: Page{Title: `post 14`, Path: `/posts/post14.html`}, IsListed: true},
Post{Page: Page{Title: `post 15`, Path: `/posts/post15.html`}, IsListed: true},
Post{Page: Page{Title: `post 16`, Path: `/posts/post16.html`}, IsListed: true},
Post{Page: Page{Title: `post 17`, Path: `/posts/post17.html`}, IsListed: true},
Post{Page: Page{Title: `post 18`, Path: `/posts/post18.html`}, IsListed: true},
Post{Page: Page{Title: `post 19`, Path: `/posts/post19.html`}, IsListed: true},
Post{Page: Page{Title: `post 20`, Path: `/posts/post20.html`}, IsListed: true},
Post{Page: Page{Title: `post 21`, Path: `/posts/post21.html`}, IsListed: true},
Post{Page: Page{Title: `post 22`, Path: `/posts/post22.html`}, IsListed: true},
Post{Page: Page{Title: `post 23`, Path: `/posts/post23.html`}, IsListed: true},
Post{Page: Page{Title: `post 24`, Path: `/posts/post24.html`}, IsListed: true},
},
}
var renderedStatic = F(
Str(Doctype),
E(`html`, AP(`lang`, `en`),
E(`head`, nil,
E(`meta`, AP(`charset`, `utf-8`)),
E(`meta`, AP(`http-equiv`, `X-UA-Compatible`, `content`, `IE=edge`)),
E(`meta`, AP(`name`, `viewport`, `content`, `width=device-width, initial-scale=1`)),
E(`link`, AP(`rel`, `icon`, `href`, `data:;base64,=`)),
E(`title`, nil, `test markup`),
),
E(`body`, AP(`class`, `stretch-to-viewport`),
E(`h1`, AP(`class`, `title`), `mock markup`),
E(`div`, AP(`class`, `main`), `hello world!`),
),
),
)
var tplStatic = templateMake(nil, `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:;base64,=">
<title>test markup</title>
</head>
<body class="stretch-to-viewport">
<h1 class="title">mock markup</h1>
<div class="main">hello world!</div>
</body>
</html>
`)
func renderDynamic(dat MockDat) Bui {
var scheme Attr
if dat.ColorScheme != `` {
scheme = Attr{`class`, dat.ColorScheme}
}
return F(
Str(Doctype),
E(`html`, AP(`lang`, `en`).A(scheme),
E(`head`, nil, func(b *Bui) {
b.E(`link`, AP(`rel`, `icon`, `href`, `data:;base64,=`))
if dat.Title != `` {
b.E(`title`, nil, dat.Title)
b.E(`meta`, AP(`property`, `og:title`, `content`, dat.Title))
} else {
b.E(`title`, nil, `mock site`)
}
if dat.Desc != `` {
b.E(`meta`, AP(`name`, `description`, `content`, dat.Desc))
}
if dat.Image != `` {
b.E(`meta`, AP(`property`, `og:image`, `content`, `/images/`+dat.Image))
}
if dat.Type != `` {
b.E(`meta`, AP(`property`, `og:type`, `content`, dat.Type))
b.E(`meta`, AP(`property`, `og:site_name`, `content`, `mock site`))
}
}),
E(`body`, nil,
E(`nav`, nil,
E(`a`, AP(`href`, `/`).A(cur(dat, `index.html`)), `home`),
E(`a`, AP(`href`, `/works`).A(cur(dat, `works.html`)), `works`),
E(`a`, AP(`href`, `/posts`).A(cur(dat, `posts.html`)), `posts`),
E(`a`, AP(`href`, `/demos`).A(cur(dat, `demos.html`)), `demos`),
E(`span`, nil, `Updated `+inst),
),
E(`div`, AP(`role`, `main`),
E(`h1`, nil, `Posts`),
func(b *Bui) {
if len(dat.Posts) > 0 {
for _, post := range dat.Posts {
if !post.IsListed {
continue
}
b.E(`h2`, nil, func() {
b.E(`a`, AP(`href`, post.Path), post.Title)
})
if post.Desc != `` {
b.E(`p`, nil, post.Desc)
}
}
} else {
b.E(`p`, nil, `Oops! It appears there are no public posts yet.`)
}
},
),
),
),
)
}
var tplDynamic = templateMake(
template.FuncMap{
"cur": curFun,
"now": func() string { return inst },
"listedPosts": listedPosts,
},
`
{{define "site-top.html"}}
<!doctype html>
<html lang="en"{{if .ColorScheme}} class="{{.ColorScheme}}"{{end}}>
<head>
<link rel="icon" href="data:;base64,=">
{{if .Title}}
<title>{{.Title}}</title>
<meta property="og:title" content="{{.Title}}">
{{else}}
<title>mock site</title>
{{end}}
{{if .Desc}}
<meta name="description" content="{{.Desc}}">
{{end}}
{{if .Image}}
<meta property="og:image" content="/images/{{.Image}}">
{{end}}
{{if .Type}}
<meta property="og:type" content="{{.Type}}">
<meta property="og:site_name" content="about:mitranim">
{{end}}
</head>
<body>
{{end}}
{{define "site-bottom.html"}}
</body>
</html>
{{end}}
{{define "nav.html"}}
<nav>
<a href="/" {{- cur . "index.html"}}>home</a>
<a href="/works" {{- cur . "works.html"}}>works</a>
<a href="/posts" {{- cur . "posts.html"}}>posts</a>
<a href="/demos" {{- cur . "demos.html"}}>demos</a>
<span>Updated {{now}}</span>
</nav>
{{end}}
{{template "site-top.html" .}}
{{template "nav.html" .}}
<div role="main">
<h1>Posts</h1>
{{range $post := listedPosts .Posts}}
<h2><a href="{{$post.Path}}">{{$post.Title}}</a></h2>
{{if $post.Desc}}<p>{{$post.Desc}}</p>{{end}}
{{else}}
<p>Oops! It appears there are no public posts yet.</p>
{{end}}
</div>
{{template "site-bottom.html" .}}
`,
)
func templateMake(funs template.FuncMap, str string) *template.Template {
tpl := template.New(``)
tpl.Option(`missingkey=error`)
tpl.Funcs(funs)
template.Must(tpl.Parse(trimLines(str)))
return tpl
}
func templateToString(tpl *template.Template, val any) string {
var buf strings.Builder
must(tpl.Execute(&buf, val))
return buf.String()
}
func cur(dat MockDat, path string) Attr {
if dat.Path == path {
return Attr{`aria-current`, `page`}
}
return Attr{}
}
func curFun(dat MockDat, path string) template.HTMLAttr {
if dat.Path == path {
return ` aria-current="page"`
}
return ``
}
func trimLines(str string) string {
return strings.TrimSpace(strings.Join(reLines.Split(str, -1), ``))
}
var reLines = regexp.MustCompile(`\s*(?:\r|\n)\s*`)
var inst = time.Date(3123, 4, 5, 6, 7, 8, 9, time.UTC).Format("Jan 02 2006")
func listedPosts(vals []Post) (out []Post) {
for _, val := range vals {
if val.IsListed {
out = append(out, val)
}
}
return out
}
func must(err error) {
if err != nil {
panic(err)
}
}