-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmapper_benchmark_test.go
281 lines (276 loc) · 7.05 KB
/
mapper_benchmark_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
package set_test
import (
"encoding/json"
"testing"
"github.com/nofeaturesonlybugs/set"
)
func BenchmarkMapper(b *testing.B) {
rowsDecoded, size := loadBenchmarkMapperData(b)
var jsonRows [][]byte
for _, decoded := range rowsDecoded {
if encoded, err := json.Marshal(decoded); err != nil {
b.Fatalf("During json.Marshal: %v", err.Error())
} else {
jsonRows = append(jsonRows, encoded)
}
}
//
b.ResetTimer()
//
b.Run("json", func(b *testing.B) {
type CustomerContact struct {
Id int `json:"customer_id"`
First string `json:"customer_first"`
Last string `json:"customer_last"`
}
type VendorContact struct {
Id int `json:"vendor_contact_id"`
First string `json:"vendor_contact_first"`
Last string `json:"vendor_contact_last"`
}
type Vendor struct {
Id int `json:"vendor_id"`
Name string `json:"vendor_name"`
Description string `json:"vendor_description"`
VendorContact
}
type T struct {
Id int `json:"id"`
CreatedTime string `json:"created_time"`
ModifiedTime string `json:"modified_time"`
Price int `json:"price"`
Quantity int `json:"quantity"`
Total int `json:"total"`
CustomerContact
Vendor
}
var jsonRow []byte
var k int
dest := make([]T, 100)
for n := 0; n < b.N; n++ {
k = n % size
jsonRow = jsonRows[k]
if err := json.Unmarshal(jsonRow, &dest[k]); err != nil {
b.Fatalf("json unmarshal error %v", err.Error())
}
}
})
//
type Common struct {
Id int
}
type Timestamps struct {
CreatedTime string
ModifiedTime string
}
type Person struct {
Common
Timestamps // Not used but present anyways
First string
Last string
}
type Vendor struct {
Common
Timestamps // Not used but present anyways
Name string
Description string
Contact Person
}
type T struct {
Common
Timestamps
//
Price int
Quantity int
Total int
//
Customer Person
Vendor Vendor
}
//
b.Run("Bind no Rebind", func(b *testing.B) {
mapper := &set.Mapper{
Elevated: set.NewTypeList(Common{}, Timestamps{}),
Join: "_",
}
//
var bound set.BoundMapping
var row MapperBenchmarkJsonRow
var k int
var err error
dest := make([]T, 100)
for n := 0; n < b.N; n++ {
k = n % size
row = rowsDecoded[k]
bound, err = mapper.Bind(&dest[k])
if err != nil {
b.Fatalf("Unable to bind: %v", err.Error())
}
//
_ = bound.Set("Id", row.Id)
_ = bound.Set("CreatedTime", row.CreatedTime)
_ = bound.Set("ModifiedTime", row.ModifiedTime)
_ = bound.Set("Price", row.Price)
_ = bound.Set("Quantity", row.Quantity)
_ = bound.Set("Total", row.Total)
//
_ = bound.Set("Customer_Id", row.CustomerId)
_ = bound.Set("Customer_First", row.CustomerFirst)
_ = bound.Set("Customer_Last", row.CustomerLast)
//
_ = bound.Set("Vendor_Id", row.VendorId)
_ = bound.Set("Vendor_Name", row.VendorName)
_ = bound.Set("Vendor_Description", row.VendorDescription)
_ = bound.Set("Vendor_Contact_Id", row.VendorContactId)
_ = bound.Set("Vendor_Contact_First", row.VendorContactFirst)
_ = bound.Set("Vendor_Contact_Last", row.VendorContactLast)
//
if err := bound.Err(); err != nil {
b.Fatalf("Unable to set: %v", err.Error())
}
}
})
//
b.Run("Bind Rebind", func(b *testing.B) {
mapper := &set.Mapper{
Elevated: set.NewTypeList(Common{}, Timestamps{}),
Join: "_",
}
//
var row MapperBenchmarkJsonRow
var k int
dest := make([]T, 100)
bound, err := mapper.Bind(&dest[0])
if err != nil {
b.Fatalf("Unable to bind: %v", err.Error())
}
for n := 0; n < b.N; n++ {
k = n % size
row = rowsDecoded[k]
bound.Rebind(&dest[k])
//
_ = bound.Set("Id", row.Id)
_ = bound.Set("CreatedTime", row.CreatedTime)
_ = bound.Set("ModifiedTime", row.ModifiedTime)
_ = bound.Set("Price", row.Price)
_ = bound.Set("Quantity", row.Quantity)
_ = bound.Set("Total", row.Total)
//
_ = bound.Set("Customer_Id", row.CustomerId)
_ = bound.Set("Customer_First", row.CustomerFirst)
_ = bound.Set("Customer_Last", row.CustomerLast)
//
_ = bound.Set("Vendor_Id", row.VendorId)
_ = bound.Set("Vendor_Name", row.VendorName)
_ = bound.Set("Vendor_Description", row.VendorDescription)
_ = bound.Set("Vendor_Contact_Id", row.VendorContactId)
_ = bound.Set("Vendor_Contact_First", row.VendorContactFirst)
_ = bound.Set("Vendor_Contact_Last", row.VendorContactLast)
//
if err := bound.Err(); err != nil {
b.Fatalf("Unable to set: %v", err.Error())
}
}
})
//
b.Run("Prepare Rebind", func(b *testing.B) {
mapper := &set.Mapper{
Elevated: set.NewTypeList(Common{}, Timestamps{}),
Join: "_",
}
//
var row MapperBenchmarkJsonRow
var k int
dest := make([]T, 100)
prepared, err := mapper.Prepare(&dest[0])
if err != nil {
b.Fatalf("error preparing %v", err.Error())
}
err = prepared.Plan(
"Id", "CreatedTime", "ModifiedTime",
"Price", "Quantity", "Total",
"Customer_Id", "Customer_First", "Customer_Last",
"Vendor_Id", "Vendor_Name", "Vendor_Description", "Vendor_Contact_Id", "Vendor_Contact_First", "Vendor_Contact_Last")
if err != nil {
b.Fatalf("error preparing plan %v", err.Error())
}
for n := 0; n < b.N; n++ {
k = n % size
row = rowsDecoded[k]
prepared.Rebind(&dest[k])
//
//
_ = prepared.Set(row.Id)
_ = prepared.Set(row.CreatedTime)
_ = prepared.Set(row.ModifiedTime)
_ = prepared.Set(row.Price)
_ = prepared.Set(row.Quantity)
_ = prepared.Set(row.Total)
//
_ = prepared.Set(row.CustomerId)
_ = prepared.Set(row.CustomerFirst)
_ = prepared.Set(row.CustomerLast)
//
_ = prepared.Set(row.VendorId)
_ = prepared.Set(row.VendorName)
_ = prepared.Set(row.VendorDescription)
_ = prepared.Set(row.VendorContactId)
_ = prepared.Set(row.VendorContactFirst)
_ = prepared.Set(row.VendorContactLast)
//
if err := prepared.Err(); err != nil {
b.Fatalf("Unable to set: %v", err.Error())
}
}
})
}
func BenchmarkMapperBaseline(b *testing.B) {
rows, size := loadBenchmarkMapperData(b)
//
type Person struct {
Id int
First string
Last string
}
type Vendor struct {
Id int
Name string
Description string
Contact Person
}
type T struct {
Id int
CreatedTime string
ModifiedTime string
Price int
Quantity int
Total int
Customer Person
Vendor Vendor
}
//
b.ResetTimer()
//
for k := 0; k < b.N; k++ {
row := rows[k%size]
dest := new(T)
//
dest.Id = row.Id
dest.CreatedTime = row.CreatedTime
dest.ModifiedTime = row.ModifiedTime
dest.Price = row.Price
dest.Quantity = row.Quantity
dest.Total = row.Total
//
dest.Customer.Id = row.CustomerId
dest.Customer.First = row.CustomerFirst
dest.Customer.Last = row.CustomerLast
//
dest.Vendor.Id = row.VendorId
dest.Vendor.Name = row.VendorName
dest.Vendor.Description = row.VendorDescription
dest.Vendor.Contact.Id = row.VendorContactId
dest.Vendor.Contact.First = row.VendorContactFirst
dest.Vendor.Contact.Last = row.VendorContactLast
}
}