-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchell_benchmark_test.go
256 lines (219 loc) · 5.79 KB
/
chell_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
package portal
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/ifaceless/portal/field"
)
type ManagerModel struct {
ID int
Name string
CreatedAt time.Time
UpdatedAt time.Time
}
func (m *ManagerModel) Fullname() string {
return m.Name + "xixi_haha"
}
type CompanyModel struct {
ID int
ManagerID int
Name string
Addr string
CreatedAt time.Time
UpdatedAt time.Time
}
func (c *CompanyModel) Manager() *ManagerModel {
// perform a db query, and return result
time.Sleep(5 * time.Millisecond)
return &ManagerModel{
ID: c.ManagerID,
Name: fmt.Sprintf("manager_%d", time.Now().Unix()),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
}
type ProductModel struct {
ID int
CompanyID int
Name string
Price int
CreatedAt time.Time
UpdatedAt time.Time
}
func (p *ProductModel) Company() *CompanyModel {
// perform a db query, and return result
time.Sleep(5 * time.Millisecond)
return &CompanyModel{
ID: p.CompanyID,
ManagerID: rand.Intn(1024) + 1,
Name: fmt.Sprintf("company_%d", rand.Intn(100)),
Addr: "addr_company",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
}
func makeProducts(count int) (ret []*ProductModel) {
for i := 0; i < count; i++ {
ret = append(ret, &ProductModel{
ID: i,
CompanyID: i + rand.Intn(100),
Name: fmt.Sprintf("name_%d", i),
Price: i * 100,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
}
return
}
type ManagerSchema struct {
ID string `json:"id"`
Name string `json:"name" portal:"attr:Fullname"`
CreatedAt *field.Timestamp `json:"created_at"`
UpdatedAt *field.Timestamp `json:"updated_at"`
}
type CompanySchema struct {
ID string `json:"id,omitempty"`
Manager *ManagerSchema `json:"manager,omitempty"`
Name string `json:"name,omitempty"`
Addr string `json:"addr,omitempty" portal:"meth:GetAddr"`
CreatedAt *field.Timestamp `json:"created_at,omitempty"`
UpdatedAt *field.Timestamp `json:"updated_at,omitempty"`
}
func (c *CompanySchema) GetAddr(company *CompanyModel) string {
return fmt.Sprintf("custom_%s", company.Addr)
}
type ProductSchema struct {
ID string `json:"id,omitempty"`
Company *CompanySchema `json:"company_id,omitempty"`
Name string `json:"name,omitempty"`
Price int `json:"price,omitempty"`
CreatedAt *field.Timestamp `json:"created_at,omitempty"`
UpdatedAt *field.Timestamp `json:"updated_at,omitempty"`
}
// BenchmarkDumpManyLargeWorkerPool-4 165 7373380 ns/op
func BenchmarkDumpManyLargeWorkerPool(b *testing.B) {
SetMaxPoolSize(1000)
products := makeProducts(100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var schemas []*ProductSchema
_ = Dump(&schemas, products)
}
}
// BenchmarkDumpOneLargeWorkerPool-4 199 6059241 ns/op
func BenchmarkDumpOneLargeWorkerPool(b *testing.B) {
SetMaxPoolSize(1000)
products := makeProducts(1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var schemas *ProductSchema
_ = Dump(&schemas, products[0])
}
}
// BenchmarkDumpManyIgnoreDBQueryLargeWorkerPool-4 11694 99205 ns/op
func BenchmarkDumpManyIgnoreDBQueryLargeWorkerPool(b *testing.B) {
SetMaxPoolSize(1000)
products := makeProducts(10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var schemas []*ProductSchema
_ = Dump(&schemas, products, Exclude("Company"))
}
}
// BenchmarkDumpOneIgnoreDBQueryLargeWorkerPool-4 92692 11246 ns/op
func BenchmarkDumpOneIgnoreDBQueryLargeWorkerPool(b *testing.B) {
SetMaxPoolSize(1000)
products := makeProducts(1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var schemas *ProductSchema
_ = Dump(&schemas, products[0], Exclude("Company"))
}
}
// BenchmarkDumpManyIgnoreDBQuerySmallWorkerPool-4 7856 144661 ns/op
func BenchmarkDumpManyIgnoreDBQuerySmallWorkerPool(b *testing.B) {
SetMaxPoolSize(1)
products := makeProducts(10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var schemas []*ProductSchema
_ = Dump(&schemas, products, Exclude("Company"))
}
}
// BenchmarkDumpOneIgnoreDBQuerySmallWorkerPool-4 110576 11076 ns/op
func BenchmarkDumpOneIgnoreDBQuerySmallWorkerPool(b *testing.B) {
SetMaxPoolSize(1)
products := makeProducts(1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var schemas *ProductSchema
_ = Dump(&schemas, products[0], Exclude("Company"))
}
}
type Hogwarts struct {
Houses []*House
}
func (h *Hogwarts) Addr() string {
return "addr"
}
type House struct {
ID int
}
type HogwartsSchema struct {
Houses []*HouseSchema `portal:"nested;async"`
Addr string `portal:"attr:Addr"`
}
type HouseSchema struct {
Name string `portal:"meth:GetMeta.Name;async"`
Color string `portal:"meth:GetMeta.Color;async"`
Desc string `portal:"meth:GetMeta.Desc;async"`
}
type Meta struct {
Name string
Color string
Desc string
}
func makeHouses(count int) (ret []*House) {
for i := 0; i < count; i++ {
ret = append(ret, &House{
ID: i,
})
}
return
}
func (s *HouseSchema) GetMeta(m *House) Meta {
time.Sleep(100 * time.Millisecond)
return Meta{
Name: "name",
Color: "red",
Desc: "desc",
}
}
// BenchmarkDumpManyWithCache-4 33654 39108 ns/op
func BenchmarkDumpManyWithCache(b *testing.B) {
SetCache(DefaultCache)
defer SetCache(nil)
houses := makeHouses(1)
hogwarts := Hogwarts{
Houses: houses,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
var schemas HogwartsSchema
_ = Dump(&schemas, hogwarts)
}
}
// BenchmarkDumpManyWithoutCache-4 2 607578280 ns/op
func BenchmarkDumpManyWithoutCache(b *testing.B) {
SetCache(nil)
houses := makeHouses(1)
hogwarts := Hogwarts{
Houses: houses,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
var schemas HogwartsSchema
_ = Dump(&schemas, hogwarts)
}
}