File: marshal_timing_test.go

package info (click to toggle)
golang-github-valyala-quicktemplate 1.8.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 492 kB
  • sloc: makefile: 16; xml: 15
file content (195 lines) | stat: -rw-r--r-- 4,565 bytes parent folder | download | duplicates (3)
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
package tests

import (
	"bytes"
	"encoding/json"
	"encoding/xml"
	"fmt"
	"log"
	"testing"

	"github.com/valyala/quicktemplate"
	"github.com/valyala/quicktemplate/testdata/templates"
)

func init() {
	// Make sure both encoding/json and templates generate identical output.
	d := newTemplatesData(3)
	bb := quicktemplate.AcquireByteBuffer()

	expectedData, err := json.Marshal(d)
	if err != nil {
		log.Fatalf("unexpected error: %s", err)
	}

	e := json.NewEncoder(bb)
	if err := e.Encode(d); err != nil {
		log.Fatalf("unexpected error: %s", err)
	}
	bb.B = bytes.TrimSpace(bb.B)
	if !bytes.Equal(bb.B, expectedData) {
		log.Fatalf("unexpected data generated with encoding/json:\n%q\n. Expecting\n%q\n", bb.B, expectedData)
	}

	bb.Reset()
	d.WriteJSON(bb)
	if !bytes.Equal(bb.B, expectedData) {
		log.Fatalf("unexpected data generated with quicktemplate:\n%q\n. Expecting\n%q\n", bb.B, expectedData)
	}

	// make sure both encoding/xml and templates generate identical output.
	expectedData, err = xml.Marshal(d)
	if err != nil {
		log.Fatalf("unexpected error: %s", err)
	}

	bb.Reset()
	xe := xml.NewEncoder(bb)
	if err := xe.Encode(d); err != nil {
		log.Fatalf("unexpected error: %s", err)
	}
	if !bytes.Equal(bb.B, expectedData) {
		log.Fatalf("unexpected data generated with encoding/xml:\n%q\n. Expecting\n%q\n", bb.B, expectedData)
	}

	bb.Reset()
	d.WriteXML(bb)
	if !bytes.Equal(bb.B, expectedData) {
		log.Fatalf("unexpected data generated with quicktemplate:\n%q\n. Expecting\n%q\n", bb.B, expectedData)
	}

	quicktemplate.ReleaseByteBuffer(bb)
}

func BenchmarkMarshalJSONStd1(b *testing.B) {
	benchmarkMarshalJSONStd(b, 1)
}

func BenchmarkMarshalJSONStd10(b *testing.B) {
	benchmarkMarshalJSONStd(b, 10)
}

func BenchmarkMarshalJSONStd100(b *testing.B) {
	benchmarkMarshalJSONStd(b, 100)
}

func BenchmarkMarshalJSONStd1000(b *testing.B) {
	benchmarkMarshalJSONStd(b, 1000)
}

func benchmarkMarshalJSONStd(b *testing.B, n int) {
	d := newTemplatesData(n)
	b.RunParallel(func(pb *testing.PB) {
		bb := quicktemplate.AcquireByteBuffer()
		e := json.NewEncoder(bb)
		for pb.Next() {
			if err := e.Encode(d); err != nil {
				b.Fatalf("unexpected error: %s", err)
			}
			bb.Reset()
		}
		quicktemplate.ReleaseByteBuffer(bb)
	})
}

func BenchmarkMarshalJSONQuickTemplate1(b *testing.B) {
	benchmarkMarshalJSONQuickTemplate(b, 1)
}

func BenchmarkMarshalJSONQuickTemplate10(b *testing.B) {
	benchmarkMarshalJSONQuickTemplate(b, 10)
}

func BenchmarkMarshalJSONQuickTemplate100(b *testing.B) {
	benchmarkMarshalJSONQuickTemplate(b, 100)
}

func BenchmarkMarshalJSONQuickTemplate1000(b *testing.B) {
	benchmarkMarshalJSONQuickTemplate(b, 1000)
}

func benchmarkMarshalJSONQuickTemplate(b *testing.B, n int) {
	d := newTemplatesData(n)
	b.RunParallel(func(pb *testing.PB) {
		bb := quicktemplate.AcquireByteBuffer()
		for pb.Next() {
			d.WriteJSON(bb)
			bb.Reset()
		}
		quicktemplate.ReleaseByteBuffer(bb)
	})
}

func BenchmarkMarshalXMLStd1(b *testing.B) {
	benchmarkMarshalXMLStd(b, 1)
}

func BenchmarkMarshalXMLStd10(b *testing.B) {
	benchmarkMarshalXMLStd(b, 10)
}

func BenchmarkMarshalXMLStd100(b *testing.B) {
	benchmarkMarshalXMLStd(b, 100)
}

func BenchmarkMarshalXMLStd1000(b *testing.B) {
	benchmarkMarshalXMLStd(b, 1000)
}

func benchmarkMarshalXMLStd(b *testing.B, n int) {
	d := newTemplatesData(n)
	b.RunParallel(func(pb *testing.PB) {
		bb := quicktemplate.AcquireByteBuffer()
		e := xml.NewEncoder(bb)
		for pb.Next() {
			if err := e.Encode(d); err != nil {
				b.Fatalf("unexpected error: %s", err)
			}
			bb.Reset()
		}
		quicktemplate.ReleaseByteBuffer(bb)
	})
}

func BenchmarkMarshalXMLQuickTemplate1(b *testing.B) {
	benchmarkMarshalXMLQuickTemplate(b, 1)
}

func BenchmarkMarshalXMLQuickTemplate10(b *testing.B) {
	benchmarkMarshalXMLQuickTemplate(b, 10)
}

func BenchmarkMarshalXMLQuickTemplate100(b *testing.B) {
	benchmarkMarshalXMLQuickTemplate(b, 100)
}

func BenchmarkMarshalXMLQuickTemplate1000(b *testing.B) {
	benchmarkMarshalXMLQuickTemplate(b, 1000)
}

func benchmarkMarshalXMLQuickTemplate(b *testing.B, n int) {
	d := newTemplatesData(n)
	b.RunParallel(func(pb *testing.PB) {
		bb := quicktemplate.AcquireByteBuffer()
		for pb.Next() {
			d.WriteXML(bb)
			bb.Reset()
		}
		quicktemplate.ReleaseByteBuffer(bb)
	})
}

func newTemplatesData(n int) *templates.MarshalData {
	var rows []templates.MarshalRow
	for i := 0; i < n; i++ {
		rows = append(rows, templates.MarshalRow{
			Msg: fmt.Sprintf("ั‚ะตัั‚ %d", i),
			N:   i,
		})
	}
	return &templates.MarshalData{
		Foo:  1,
		Bar:  "foobar",
		Rows: rows,
	}
}