Skip to content

Commit 78c5604

Browse files
committed
add benchmark
1 parent 59c7a86 commit 78c5604

File tree

7 files changed

+10958
-0
lines changed

7 files changed

+10958
-0
lines changed

codegen/fieldmask/baseline.thrift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace go baseline
2+
3+
struct Simple {
4+
1: byte ByteField
5+
2: i64 I64Field (api.js_conv = "")
6+
3: double DoubleField
7+
4: i32 I32Field
8+
5: string StringField,
9+
6: binary BinaryField
10+
}
11+
12+
struct PartialSimple {
13+
1: byte ByteField
14+
3: double DoubleField
15+
6: binary BinaryField
16+
}
17+
18+
struct Nesting {
19+
1: string String (api.header = "String")
20+
2: list<Simple> ListSimple
21+
3: double Double (api.path = "double")
22+
4: i32 I32 (api.http_code = "", api.body = "I32")
23+
5: list<i32> ListI32 (api.query = "ListI32")
24+
6: i64 I64
25+
7: map<string, string> MapStringString
26+
8: Simple SimpleStruct
27+
9: map<i32, i64> MapI32I64
28+
10: list<string> ListString
29+
11: binary Binary
30+
12: map<i64, string> MapI64String
31+
13: list<i64> ListI64 (api.cookie = "list_i64"),
32+
14: byte Byte
33+
15: map<string, Simple> MapStringSimple
34+
}
35+
36+
struct PartialNesting {
37+
2: list<PartialSimple> ListSimple
38+
8: PartialSimple SimpleStruct
39+
15: map<string, PartialSimple> MapStringSimple
40+
}
41+
42+
struct Nesting2 {
43+
1: map<Simple, Nesting> MapSimpleNesting
44+
2: Simple SimpleStruct
45+
3: byte Byte
46+
4: double Double
47+
5: list<Nesting> ListNesting
48+
6: i64 I64
49+
7: Nesting NestingStruct
50+
8: binary Binary
51+
9: string String
52+
10: set<Nesting> SetNesting
53+
11: i32 I32
54+
}
55+
56+
service BaselineService {
57+
Simple SimpleMethod(1: Simple req) (api.post = "/simple")
58+
PartialSimple PartialSimpleMethod(1: PartialSimple req)
59+
Nesting NestingMethod(1: Nesting req) (api.post = "/nesting")
60+
PartialNesting PartialNestingMethod(1: PartialNesting req)
61+
Nesting2 Nesting2Method(1: Nesting2 req) (api.post = "/nesting2")
62+
}

codegen/fieldmask/baseline_test.go

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
// Copyright 2023 CloudWeGo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package test
16+
17+
import (
18+
"bytes"
19+
"math"
20+
"strconv"
21+
"strings"
22+
"testing"
23+
24+
"test/test_gen/baseline"
25+
26+
"github.com/cloudwego/thriftgo/fieldmask"
27+
)
28+
29+
var (
30+
bytesCount int = 2
31+
stringCount int = 2
32+
listCount int = 16
33+
)
34+
35+
func getString() string {
36+
return strings.Repeat("你好,\b\n\r\t世界", stringCount)
37+
}
38+
39+
func getBytes() []byte {
40+
return bytes.Repeat([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, bytesCount)
41+
}
42+
43+
func getSimpleValue() *baseline.Simple {
44+
return &baseline.Simple{
45+
ByteField: math.MaxInt8,
46+
I64Field: math.MaxInt64,
47+
DoubleField: math.MaxFloat64,
48+
I32Field: math.MaxInt32,
49+
StringField: getString(),
50+
BinaryField: getBytes(),
51+
}
52+
}
53+
54+
func getNestingValue() *baseline.Nesting {
55+
var ret = &baseline.Nesting{
56+
String_: getString(),
57+
ListSimple: []*baseline.Simple{},
58+
Double: math.MaxFloat64,
59+
I32: math.MaxInt32,
60+
ListI32: []int32{},
61+
I64: math.MaxInt64,
62+
MapStringString: map[string]string{},
63+
SimpleStruct: getSimpleValue(),
64+
MapI32I64: map[int32]int64{},
65+
ListString: []string{},
66+
Binary: getBytes(),
67+
MapI64String: map[int64]string{},
68+
ListI64: []int64{},
69+
Byte: math.MaxInt8,
70+
MapStringSimple: map[string]*baseline.Simple{},
71+
}
72+
73+
for i := 0; i < listCount; i++ {
74+
ret.ListSimple = append(ret.ListSimple, getSimpleValue())
75+
ret.ListI32 = append(ret.ListI32, math.MinInt32)
76+
ret.ListI64 = append(ret.ListI64, math.MinInt64)
77+
ret.ListString = append(ret.ListString, getString())
78+
}
79+
80+
for i := 0; i < listCount; i++ {
81+
ret.MapStringString[strconv.Itoa(i)] = getString()
82+
ret.MapI32I64[int32(i)] = math.MinInt64
83+
ret.MapI64String[int64(i)] = getString()
84+
ret.MapStringSimple[strconv.Itoa(i)] = getSimpleValue()
85+
}
86+
87+
return ret
88+
}
89+
90+
func BenchmarkFastWriteSimple(b *testing.B) {
91+
b.Run("full", func(b *testing.B) {
92+
obj := getSimpleValue()
93+
data := make([]byte, obj.BLength())
94+
ret := obj.FastWriteNocopy(data, nil)
95+
if ret != len(data) {
96+
b.Fatal(ret)
97+
}
98+
b.ResetTimer()
99+
for i := 0; i < b.N; i++ {
100+
_ = obj.BLength()
101+
_ = obj.FastWriteNocopy(data, nil)
102+
}
103+
})
104+
b.Run("half", func(b *testing.B) {
105+
obj := getSimpleValue()
106+
fm, err := fieldmask.NewFieldMask(obj.GetTypeDescriptor(), "$.ByteField", "$.DoubleField", "$.StringField")
107+
if err != nil {
108+
b.Fatal(err)
109+
}
110+
obj.SetFieldMask(fm)
111+
data := make([]byte, obj.BLength())
112+
ret := obj.FastWriteNocopy(data, nil)
113+
if ret != len(data) {
114+
b.Fatal(ret)
115+
}
116+
b.ResetTimer()
117+
for i := 0; i < b.N; i++ {
118+
obj.SetFieldMask(fm)
119+
_ = obj.BLength()
120+
_ = obj.FastWriteNocopy(data, nil)
121+
}
122+
})
123+
}
124+
125+
func BenchmarkFastReadSimple(b *testing.B) {
126+
b.Run("full", func(b *testing.B) {
127+
obj := getSimpleValue()
128+
data := make([]byte, obj.BLength())
129+
ret := obj.FastWriteNocopy(data, nil)
130+
if ret != len(data) {
131+
b.Fatal(ret)
132+
}
133+
obj = baseline.NewSimple()
134+
n, err := obj.FastRead(data)
135+
if n != len(data) {
136+
b.Fatal(err)
137+
}
138+
b.ResetTimer()
139+
for i := 0; i < b.N; i++ {
140+
_, _ = obj.FastRead(data)
141+
}
142+
})
143+
b.Run("half", func(b *testing.B) {
144+
obj := getSimpleValue()
145+
fm, err := fieldmask.NewFieldMask(obj.GetTypeDescriptor(),
146+
"$.ByteField", "$.DoubleField", "$.StringField")
147+
if err != nil {
148+
b.Fatal(err)
149+
}
150+
data := make([]byte, obj.BLength())
151+
ret := obj.FastWriteNocopy(data, nil)
152+
if ret != len(data) {
153+
b.Fatal(ret)
154+
}
155+
obj = baseline.NewSimple()
156+
obj.SetFieldMask(fm)
157+
n, err := obj.FastRead(data)
158+
if n != len(data) {
159+
b.Fatal(err)
160+
}
161+
b.ResetTimer()
162+
for i := 0; i < b.N; i++ {
163+
obj.SetFieldMask(fm)
164+
_, _ = obj.FastRead(data)
165+
}
166+
})
167+
}
168+
169+
func BenchmarkFastWriteNesting(b *testing.B) {
170+
b.Run("full", func(b *testing.B) {
171+
obj := getNestingValue()
172+
data := make([]byte, obj.BLength())
173+
ret := obj.FastWriteNocopy(data, nil)
174+
if ret != len(data) {
175+
b.Fatal(ret)
176+
}
177+
// println("full data size: ", len(data))
178+
b.ResetTimer()
179+
for i := 0; i < b.N; i++ {
180+
_ = obj.BLength()
181+
_ = obj.FastWriteNocopy(data, nil)
182+
}
183+
})
184+
b.Run("half", func(b *testing.B) {
185+
obj := getNestingValue()
186+
// ins := []string{}
187+
// for i := 0; i < listCount/2; i++ {
188+
// ins = append(ins, strconv.Itoa(i))
189+
// }
190+
// is := strings.Join(ins, ",")
191+
// ss := strings.Join(ins, `","`)
192+
fm, err := fieldmask.NewFieldMask(obj.GetTypeDescriptor(),
193+
// "$.ListSimple["+is+"]", "$.I32", "$.ListI32["+is+"]", `$.MapStringString{"`+ss+`"}`,
194+
// "$.MapI32I64{"+is+"}", "$.Binary", "$.ListI64["+is+"]", `$.MapStringSimple{"`+ss+`"}`,
195+
"$.ListSimple", "$.I32", "$.ListI32", `$.MapStringString`,
196+
"$.MapI32I64", "$.Binary",
197+
)
198+
if err != nil {
199+
b.Fatal(err)
200+
}
201+
obj.SetFieldMask(fm)
202+
data := make([]byte, obj.BLength())
203+
ret := obj.FastWriteNocopy(data, nil)
204+
if ret != len(data) {
205+
b.Fatal(ret)
206+
}
207+
// println("half data size: ", len(data))
208+
b.ResetTimer()
209+
for i := 0; i < b.N; i++ {
210+
obj.SetFieldMask(fm)
211+
_ = obj.BLength()
212+
_ = obj.FastWriteNocopy(data, nil)
213+
}
214+
})
215+
}
216+
217+
func BenchmarkFastReadNesting(b *testing.B) {
218+
b.Run("full", func(b *testing.B) {
219+
obj := getNestingValue()
220+
data := make([]byte, obj.BLength())
221+
ret := obj.FastWriteNocopy(data, nil)
222+
if ret != len(data) {
223+
b.Fatal(ret)
224+
}
225+
obj = baseline.NewNesting()
226+
n, err := obj.FastRead(data)
227+
if n != len(data) {
228+
b.Fatal(err)
229+
}
230+
b.ResetTimer()
231+
for i := 0; i < b.N; i++ {
232+
_, _ = obj.FastRead(data)
233+
}
234+
})
235+
b.Run("half", func(b *testing.B) {
236+
obj := getNestingValue()
237+
// ins := []string{}
238+
// for i := 0; i < listCount/2; i++ {
239+
// ins = append(ins, strconv.Itoa(i))
240+
// }
241+
// is := strings.Join(ins, ",")
242+
// ss := strings.Join(ins, `","`)
243+
fm, err := fieldmask.NewFieldMask(obj.GetTypeDescriptor(),
244+
// "$.ListSimple["+is+"]", "$.I32", "$.ListI32["+is+"]", `$.MapStringString{"`+ss+`"}`,
245+
// "$.MapI32I64{"+is+"}", "$.Binary", "$.ListI64["+is+"]", `$.MapStringSimple{"`+ss+`"}`,
246+
"$.ListSimple", "$.I32", "$.ListI32", `$.MapStringString`,
247+
"$.MapI32I64", "$.Binary",
248+
)
249+
if err != nil {
250+
b.Fatal(err)
251+
}
252+
data := make([]byte, obj.BLength())
253+
ret := obj.FastWriteNocopy(data, nil)
254+
if ret != len(data) {
255+
b.Fatal(ret)
256+
}
257+
obj = baseline.NewNesting()
258+
obj.SetFieldMask(fm)
259+
n, err := obj.FastRead(data)
260+
if n != len(data) {
261+
b.Fatal(err)
262+
}
263+
b.ResetTimer()
264+
for i := 0; i < b.N; i++ {
265+
obj.SetFieldMask(fm)
266+
_, _ = obj.FastRead(data)
267+
}
268+
})
269+
}

codegen/fieldmask/nesting.prof

5.26 KB
Binary file not shown.

0 commit comments

Comments
 (0)