Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Nov 24, 2023
1 parent 59c7a86 commit 78c5604
Show file tree
Hide file tree
Showing 7 changed files with 10,958 additions and 0 deletions.
62 changes: 62 additions & 0 deletions codegen/fieldmask/baseline.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace go baseline

struct Simple {
1: byte ByteField
2: i64 I64Field (api.js_conv = "")
3: double DoubleField
4: i32 I32Field
5: string StringField,
6: binary BinaryField
}

struct PartialSimple {
1: byte ByteField
3: double DoubleField
6: binary BinaryField
}

struct Nesting {
1: string String (api.header = "String")
2: list<Simple> ListSimple
3: double Double (api.path = "double")
4: i32 I32 (api.http_code = "", api.body = "I32")
5: list<i32> ListI32 (api.query = "ListI32")
6: i64 I64
7: map<string, string> MapStringString
8: Simple SimpleStruct
9: map<i32, i64> MapI32I64
10: list<string> ListString
11: binary Binary
12: map<i64, string> MapI64String
13: list<i64> ListI64 (api.cookie = "list_i64"),
14: byte Byte
15: map<string, Simple> MapStringSimple
}

struct PartialNesting {
2: list<PartialSimple> ListSimple
8: PartialSimple SimpleStruct
15: map<string, PartialSimple> MapStringSimple
}

struct Nesting2 {
1: map<Simple, Nesting> MapSimpleNesting
2: Simple SimpleStruct
3: byte Byte
4: double Double
5: list<Nesting> ListNesting
6: i64 I64
7: Nesting NestingStruct
8: binary Binary
9: string String
10: set<Nesting> SetNesting
11: i32 I32
}

service BaselineService {
Simple SimpleMethod(1: Simple req) (api.post = "/simple")
PartialSimple PartialSimpleMethod(1: PartialSimple req)
Nesting NestingMethod(1: Nesting req) (api.post = "/nesting")
PartialNesting PartialNestingMethod(1: PartialNesting req)
Nesting2 Nesting2Method(1: Nesting2 req) (api.post = "/nesting2")
}
269 changes: 269 additions & 0 deletions codegen/fieldmask/baseline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
// Copyright 2023 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"bytes"
"math"
"strconv"
"strings"
"testing"

"test/test_gen/baseline"

"github.com/cloudwego/thriftgo/fieldmask"
)

var (
bytesCount int = 2
stringCount int = 2
listCount int = 16
)

func getString() string {
return strings.Repeat("你好,\b\n\r\t世界", stringCount)
}

func getBytes() []byte {
return bytes.Repeat([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, bytesCount)
}

func getSimpleValue() *baseline.Simple {
return &baseline.Simple{
ByteField: math.MaxInt8,
I64Field: math.MaxInt64,
DoubleField: math.MaxFloat64,
I32Field: math.MaxInt32,
StringField: getString(),
BinaryField: getBytes(),
}
}

func getNestingValue() *baseline.Nesting {
var ret = &baseline.Nesting{
String_: getString(),
ListSimple: []*baseline.Simple{},
Double: math.MaxFloat64,
I32: math.MaxInt32,
ListI32: []int32{},
I64: math.MaxInt64,
MapStringString: map[string]string{},
SimpleStruct: getSimpleValue(),
MapI32I64: map[int32]int64{},
ListString: []string{},
Binary: getBytes(),
MapI64String: map[int64]string{},
ListI64: []int64{},
Byte: math.MaxInt8,
MapStringSimple: map[string]*baseline.Simple{},
}

for i := 0; i < listCount; i++ {
ret.ListSimple = append(ret.ListSimple, getSimpleValue())
ret.ListI32 = append(ret.ListI32, math.MinInt32)
ret.ListI64 = append(ret.ListI64, math.MinInt64)
ret.ListString = append(ret.ListString, getString())
}

for i := 0; i < listCount; i++ {
ret.MapStringString[strconv.Itoa(i)] = getString()
ret.MapI32I64[int32(i)] = math.MinInt64
ret.MapI64String[int64(i)] = getString()
ret.MapStringSimple[strconv.Itoa(i)] = getSimpleValue()
}

return ret
}

func BenchmarkFastWriteSimple(b *testing.B) {
b.Run("full", func(b *testing.B) {
obj := getSimpleValue()
data := make([]byte, obj.BLength())
ret := obj.FastWriteNocopy(data, nil)
if ret != len(data) {
b.Fatal(ret)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = obj.BLength()
_ = obj.FastWriteNocopy(data, nil)
}
})
b.Run("half", func(b *testing.B) {
obj := getSimpleValue()
fm, err := fieldmask.NewFieldMask(obj.GetTypeDescriptor(), "$.ByteField", "$.DoubleField", "$.StringField")
if err != nil {
b.Fatal(err)
}
obj.SetFieldMask(fm)
data := make([]byte, obj.BLength())
ret := obj.FastWriteNocopy(data, nil)
if ret != len(data) {
b.Fatal(ret)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
obj.SetFieldMask(fm)
_ = obj.BLength()
_ = obj.FastWriteNocopy(data, nil)
}
})
}

func BenchmarkFastReadSimple(b *testing.B) {
b.Run("full", func(b *testing.B) {
obj := getSimpleValue()
data := make([]byte, obj.BLength())
ret := obj.FastWriteNocopy(data, nil)
if ret != len(data) {
b.Fatal(ret)
}
obj = baseline.NewSimple()
n, err := obj.FastRead(data)
if n != len(data) {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = obj.FastRead(data)
}
})
b.Run("half", func(b *testing.B) {
obj := getSimpleValue()
fm, err := fieldmask.NewFieldMask(obj.GetTypeDescriptor(),
"$.ByteField", "$.DoubleField", "$.StringField")
if err != nil {
b.Fatal(err)
}
data := make([]byte, obj.BLength())
ret := obj.FastWriteNocopy(data, nil)
if ret != len(data) {
b.Fatal(ret)
}
obj = baseline.NewSimple()
obj.SetFieldMask(fm)
n, err := obj.FastRead(data)
if n != len(data) {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
obj.SetFieldMask(fm)
_, _ = obj.FastRead(data)
}
})
}

func BenchmarkFastWriteNesting(b *testing.B) {
b.Run("full", func(b *testing.B) {
obj := getNestingValue()
data := make([]byte, obj.BLength())
ret := obj.FastWriteNocopy(data, nil)
if ret != len(data) {
b.Fatal(ret)
}
// println("full data size: ", len(data))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = obj.BLength()
_ = obj.FastWriteNocopy(data, nil)
}
})
b.Run("half", func(b *testing.B) {
obj := getNestingValue()
// ins := []string{}
// for i := 0; i < listCount/2; i++ {
// ins = append(ins, strconv.Itoa(i))
// }
// is := strings.Join(ins, ",")
// ss := strings.Join(ins, `","`)
fm, err := fieldmask.NewFieldMask(obj.GetTypeDescriptor(),
// "$.ListSimple["+is+"]", "$.I32", "$.ListI32["+is+"]", `$.MapStringString{"`+ss+`"}`,
// "$.MapI32I64{"+is+"}", "$.Binary", "$.ListI64["+is+"]", `$.MapStringSimple{"`+ss+`"}`,
"$.ListSimple", "$.I32", "$.ListI32", `$.MapStringString`,
"$.MapI32I64", "$.Binary",
)
if err != nil {
b.Fatal(err)
}
obj.SetFieldMask(fm)
data := make([]byte, obj.BLength())
ret := obj.FastWriteNocopy(data, nil)
if ret != len(data) {
b.Fatal(ret)
}
// println("half data size: ", len(data))
b.ResetTimer()
for i := 0; i < b.N; i++ {
obj.SetFieldMask(fm)
_ = obj.BLength()
_ = obj.FastWriteNocopy(data, nil)
}
})
}

func BenchmarkFastReadNesting(b *testing.B) {
b.Run("full", func(b *testing.B) {
obj := getNestingValue()
data := make([]byte, obj.BLength())
ret := obj.FastWriteNocopy(data, nil)
if ret != len(data) {
b.Fatal(ret)
}
obj = baseline.NewNesting()
n, err := obj.FastRead(data)
if n != len(data) {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = obj.FastRead(data)
}
})
b.Run("half", func(b *testing.B) {
obj := getNestingValue()
// ins := []string{}
// for i := 0; i < listCount/2; i++ {
// ins = append(ins, strconv.Itoa(i))
// }
// is := strings.Join(ins, ",")
// ss := strings.Join(ins, `","`)
fm, err := fieldmask.NewFieldMask(obj.GetTypeDescriptor(),
// "$.ListSimple["+is+"]", "$.I32", "$.ListI32["+is+"]", `$.MapStringString{"`+ss+`"}`,
// "$.MapI32I64{"+is+"}", "$.Binary", "$.ListI64["+is+"]", `$.MapStringSimple{"`+ss+`"}`,
"$.ListSimple", "$.I32", "$.ListI32", `$.MapStringString`,
"$.MapI32I64", "$.Binary",
)
if err != nil {
b.Fatal(err)
}
data := make([]byte, obj.BLength())
ret := obj.FastWriteNocopy(data, nil)
if ret != len(data) {
b.Fatal(ret)
}
obj = baseline.NewNesting()
obj.SetFieldMask(fm)
n, err := obj.FastRead(data)
if n != len(data) {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
obj.SetFieldMask(fm)
_, _ = obj.FastRead(data)
}
})
}
Binary file added codegen/fieldmask/nesting.prof
Binary file not shown.
Loading

0 comments on commit 78c5604

Please sign in to comment.