forked from slinso/goTemplateBenchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates_complex_test.go
384 lines (309 loc) · 10.1 KB
/
templates_complex_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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package main_test
import (
"bytes"
"html/template"
"path/filepath"
"testing"
"github.com/SlinSo/goTemplateBenchmark/model"
"github.com/SlinSo/goTemplateBenchmark/ego"
"github.com/SlinSo/goTemplateBenchmark/egon"
"github.com/SlinSo/goTemplateBenchmark/egonslinso"
"github.com/SlinSo/goTemplateBenchmark/ftmpl"
"github.com/SlinSo/goTemplateBenchmark/gorazor"
herotmpl "github.com/SlinSo/goTemplateBenchmark/hero"
"github.com/SlinSo/goTemplateBenchmark/quicktemplate"
"github.com/hoisie/mustache"
)
type tmplData struct {
User *model.User
Nav []*model.Navigation
Title string
Messages []struct {
I int
Plural bool
}
}
var (
testComplexUser = &model.User{
FirstName: "Bob",
FavoriteColors: []string{"blue", "green", "mauve"},
RawContent: "<div><p>Raw Content to be displayed</p></div>",
EscapedContent: "<div><div><div>Escaped</div></div></div>",
}
testComplexNav = []*model.Navigation{{
Item: "Link 1",
Link: "http://www.mytest.com/"}, {
Item: "Link 2",
Link: "http://www.mytest.com/"}, {
Item: "Link 3",
Link: "http://www.mytest.com/"},
}
testComplexTitle = testComplexUser.FirstName
testComplexData = tmplData{
User: testComplexUser,
Nav: testComplexNav,
Title: testComplexTitle,
Messages: []struct {
I int
Plural bool
}{{1, false}, {2, true}, {3, true}, {4, true}, {5, true}},
}
expectedtComplexResult = `<!DOCTYPE html>
<html>
<body>
<header><title>Bob's Home Page</title>
<div class="header">Page Header</div>
</header>
<nav>
<ul class="navigation"><li><a href="http://www.mytest.com/">Link 1</a></li>
<li><a href="http://www.mytest.com/">Link 2</a></li>
<li><a href="http://www.mytest.com/">Link 3</a></li>
</ul>
</nav>
<section>
<div class="content">
<div class="welcome">
<h4>Hello Bob</h4>
<div class="raw"><div><p>Raw Content to be displayed</p></div></div>
<div class="enc"><div><div><div>Escaped</div></div></div></div>
</div><p>Bob has 1 message</p><p>Bob has 2 messages</p><p>Bob has 3 messages</p><p>Bob has 4 messages</p><p>Bob has 5 messages</p>
</div>
</section>
<footer><div class="footer">copyright 2016</div>
</footer>
</body>
</html>`
expectedtComplexResultMinified = "<html><body><h1>Bob</h1><p>Here's a list of your favorite colors:</p><ul><li>blue</li><li>green</li><li>mauve</li></ul></body></html>"
)
/******************************************************************************
** Go
******************************************************************************/
func TestComplexGolang(t *testing.T) {
var buf bytes.Buffer
funcMap := template.FuncMap{
"safehtml": func(text string) template.HTML { return template.HTML(text) },
}
templates := make(map[string]*template.Template)
templatesDir := "go/"
layouts, err := filepath.Glob(templatesDir + "layout/*.tmpl")
if err != nil {
panic(err)
}
includes, err := filepath.Glob(templatesDir + "includes/*.tmpl")
if err != nil {
panic(err)
}
// Generate our templates map from our layouts/ and includes/ directories
for _, layout := range layouts {
files := append(includes, layout)
templates[filepath.Base(layout)] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...))
}
templates["index.tmpl"].ExecuteTemplate(&buf, "base", testComplexData)
if msg, ok := linesEquals(buf.String(), expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexGolang(b *testing.B) {
var buf bytes.Buffer
funcMap := template.FuncMap{
"safehtml": func(text string) template.HTML { return template.HTML(text) },
}
templates := make(map[string]*template.Template)
templatesDir := "go/"
layouts, err := filepath.Glob(templatesDir + "layout/*.tmpl")
if err != nil {
panic(err)
}
includes, err := filepath.Glob(templatesDir + "includes/*.tmpl")
if err != nil {
panic(err)
}
// Generate our templates map from our layouts/ and includes/ directories
for _, layout := range layouts {
files := append(includes, layout)
templates[filepath.Base(layout)] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
templates["index.tmpl"].ExecuteTemplate(&buf, "base", testComplexData)
buf.Reset()
}
}
/******************************************************************************
** Ego
******************************************************************************/
func TestComplexEgo(t *testing.T) {
var buf bytes.Buffer
ego.EgoComplex(&buf, testComplexUser, testComplexNav, testComplexTitle)
if msg, ok := linesEquals(buf.String(), expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexEgo(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
ego.EgoComplex(&buf, testComplexUser, testComplexNav, testComplexTitle)
buf.Reset()
}
}
/******************************************************************************
** Quicktemplate
******************************************************************************/
func TestComplexQuicktemplate(t *testing.T) {
var buf bytes.Buffer
quicktemplate.WriteIndex(&buf, testComplexUser, testComplexNav, testComplexTitle)
if msg, ok := linesEquals(buf.String(), expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexQuicktemplate(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
quicktemplate.WriteIndex(&buf, testComplexUser, testComplexNav, testComplexTitle)
buf.Reset()
}
}
/******************************************************************************
** Egon
******************************************************************************/
func TestComplexEgon(t *testing.T) {
var buf bytes.Buffer
egon.IndexTemplate(&buf, testComplexUser, testComplexNav, testComplexTitle)
if msg, ok := linesEquals(buf.String(), expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexEgon(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
egon.IndexTemplate(&buf, testComplexUser, testComplexNav, testComplexTitle)
buf.Reset()
}
}
/******************************************************************************
** EgoSlinso
******************************************************************************/
func TestComplexEgoSlinso(t *testing.T) {
var buf bytes.Buffer
egonslinso.IndexTemplate(&buf, testComplexUser, testComplexNav, testComplexTitle)
if msg, ok := linesEquals(buf.String(), expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexEgoSlinso(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
egonslinso.IndexTemplate(&buf, testComplexUser, testComplexNav, testComplexTitle)
buf.Reset()
}
}
/******************************************************************************
** ftmpl
******************************************************************************/
func TestComplexFtmpl(t *testing.T) {
result := ftmpl.TMPLindex(testComplexUser, testComplexNav, testComplexTitle)
if msg, ok := linesEquals(result, expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexFtmpl(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ftmpl.TMPLindex(testComplexUser, testComplexNav, testComplexTitle)
}
}
func TestComplexFtmplInclude(t *testing.T) {
result := ftmpl.TMPLindex2(testComplexUser, testComplexNav, testComplexTitle)
if msg, ok := linesEquals(result, expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexFtmplInclude(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ftmpl.TMPLindex2(testComplexUser, testComplexNav, testComplexTitle)
}
}
/******************************************************************************
** Mustache
******************************************************************************/
func TestComplexMustache(t *testing.T) {
layoutTmpl, err := mustache.ParseFile("mustache/base.mustache")
if err != nil {
t.Error(err)
}
tmpl, err := mustache.ParseFile("mustache/index.mustache")
if err != nil {
t.Error(err)
}
result := tmpl.RenderInLayout(layoutTmpl, testComplexData)
if msg, ok := linesEquals(result, expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexMustache(b *testing.B) {
layoutTmpl, _ := mustache.ParseFile("mustache/base.mustache")
tmpl, _ := mustache.ParseFile("mustache/index.mustache")
b.ResetTimer()
for i := 0; i < b.N; i++ {
tmpl.RenderInLayout(layoutTmpl, testComplexData)
}
}
/******************************************************************************
** gorazor
******************************************************************************/
func TestComplexGorazor(t *testing.T) {
result := gorazor.Index(testComplexUser, testComplexNav, testComplexTitle)
if msg, ok := linesEquals(result, expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexGorazor(b *testing.B) {
for i := 0; i < b.N; i++ {
gorazor.Index(testComplexUser, testComplexNav, testComplexTitle)
}
}
/******************************************************************************
** Jet
******************************************************************************/
func TestComplexJetHTML(t *testing.T) {
var buf bytes.Buffer
tmpl, err := jetSet.GetTemplate("index.jet")
if err != nil {
t.Error(err)
}
err = tmpl.Execute(&buf, nil, testComplexData)
if err != nil {
t.Error(err)
}
if msg, ok := linesEquals(buf.String(), expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexJetHTML(b *testing.B) {
var buf bytes.Buffer
tmpl, _ := jetSet.GetTemplate("index.jet")
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := tmpl.Execute(&buf, nil, testComplexData)
if err != nil {
b.Fatal(err)
}
buf.Reset()
}
}
/******************************************************************************
** Hero
******************************************************************************/
func TestComplexHero(t *testing.T) {
var buf bytes.Buffer
herotmpl.Index(testComplexUser, testComplexNav, testComplexTitle, &buf)
if msg, ok := linesEquals(buf.String(), expectedtComplexResult); !ok {
t.Error(msg)
}
}
func BenchmarkComplexHero(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
herotmpl.Index(testComplexUser, testComplexNav, testComplexTitle, &buf)
buf.Reset()
}
}