Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tests for generic call using dynamicgo and data size types #59

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
add for map and ordinary generic
  • Loading branch information
Marina-Sakai committed Mar 31, 2023
commit ba6e97740b590918afb624a8cb1a7aba32cb14d7
89 changes: 69 additions & 20 deletions generic/data/data.go
Original file line number Diff line number Diff line change
@@ -25,44 +25,60 @@ import (
)

var (
SmallData, MediumData, LargeData string
SmallReq, MediumReq, LargeReq *echo.ObjReq
SmallMap, MediumMap, LargeMap map[string]interface{}
SmallString, MediumString, LargeString string
actionSidx, msgSidx, actionMidx, msgMidx, actionLidx, msgLidx int
)

type Size int

const (
Small Size = iota
Medium
Large
Small Size = 6
Medium Size = 33
Large Size = 67
)

func init() {
//TODO: omit action and msg for http?
SmallData = getReqValue(1)
MediumData = getReqValue(10)
LargeData = getReqValue(100)
actionSidx = strings.Index(SmallData, `"action":""`) + len(`"action":""`) - 1
actionMidx = strings.Index(MediumData, `"action":""`) + len(`"action":""`) - 1
actionLidx = strings.Index(LargeData, `"action":""`) + len(`"action":""`) - 1
msgSidx = strings.Index(SmallData, `"msg":""`) + len(`"msg":""`) - 1
msgMidx = strings.Index(MediumData, `"msg":""`) + len(`"msg":""`) - 1
msgLidx = strings.Index(LargeData, `"msg":""`) + len(`"msg":""`) - 1
// data size: small 1027B, medium 5035B, large 10101B
SmallReq = getReqValue(int(Small))
MediumReq = getReqValue(int(Medium))
LargeReq = getReqValue(int(Large))
SmallString = reqToString(SmallReq)
MediumString = reqToString(MediumReq)
LargeString = reqToString(LargeReq)
SmallMap = getReqMap(int(Small))
MediumMap = getReqMap(int(Medium))
LargeMap = getReqMap(int(Large))
actionSidx = strings.Index(SmallString, `"action":""`) + len(`"action":""`) - 1
actionMidx = strings.Index(MediumString, `"action":""`) + len(`"action":""`) - 1
actionLidx = strings.Index(LargeString, `"action":""`) + len(`"action":""`) - 1
msgSidx = strings.Index(SmallString, `"msg":""`) + len(`"msg":""`) - 1
msgMidx = strings.Index(MediumString, `"msg":""`) + len(`"msg":""`) - 1
msgLidx = strings.Index(LargeString, `"msg":""`) + len(`"msg":""`) - 1
}

func GetJsonString(action, msg string, size Size) string {
switch size {
case Small:
return SmallData[:actionSidx] + action + SmallData[actionSidx:msgSidx] + msg + SmallData[msgSidx:]
return SmallString[:actionSidx] + action + SmallString[actionSidx:msgSidx] + msg + SmallString[msgSidx:]
case Medium:
return MediumData[:actionMidx] + action + MediumData[actionMidx:msgMidx] + msg + MediumData[msgMidx:]
return MediumString[:actionMidx] + action + MediumString[actionMidx:msgMidx] + msg + MediumString[msgMidx:]
case Large:
return LargeData[:actionLidx] + action + LargeData[actionLidx:msgLidx] + msg + LargeData[msgLidx:]
return LargeString[:actionLidx] + action + LargeString[actionLidx:msgLidx] + msg + LargeString[msgLidx:]
}
return ""
}

func getReqValue(size int) string {
func reqToString(req *echo.ObjReq) string {
data, err := json.Marshal(req)
if err != nil {
panic(err)
}
return string(data)
}

func getReqValue(size int) *echo.ObjReq {
req := &echo.ObjReq{
Action: "",
Msg: "",
@@ -79,8 +95,7 @@ func getReqValue(size int) string {
req.FlagMsg = getMessage(int64(i))
}

data, _ := json.Marshal(req)
return string(data)
return req
}

func getSubMessage(i int64) *echo.SubMessage {
@@ -102,3 +117,37 @@ func getMessage(i int64) *echo.Message {
ret.SubMessages = append(ret.SubMessages, getSubMessage(2))
return ret
}

func getReqMap(size int) map[string]interface{} {
var msgMap map[interface{}]interface{}
var subMsgs []interface{}
var msgSet []interface{}
var flagMsg map[string]interface{}
for i := 0; i < size; i++ {
msgMap[strconv.Itoa(i)] = getSubMessageMap(int64(i))
subMsgs = append(subMsgs, getSubMessageMap(int64(i)))
msgSet = append(msgSet, getMessageMap(int64(i)))
flagMsg = getMessageMap(int64(i))
}
return map[string]interface{}{
"msgMap": msgMap,
"subMsgs": subMsgs,
"msgSet": msgSet,
"flagMsg": flagMsg,
}
}

func getSubMessageMap(i int64) map[string]interface{} {
return map[string]interface{}{
"id": i,
"value": "hello",
}
}

func getMessageMap(i int64) map[string]interface{} {
return map[string]interface{}{
"id": i,
"value": "hello",
"subMessages": []interface{}{getSubMessageMap(1), getSubMessageMap(2)},
}
}
6 changes: 3 additions & 3 deletions generic/http/client/default/kitex_client.go
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ func (cli *genericHTTPSmallClient) Echo(action, msg string) error {
ctx := context.Background()

url := fmt.Sprintf("http://example.com/test/obj/%s", action)
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.SmallData)))
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.SmallString)))
if err != nil {
return err
}
@@ -122,7 +122,7 @@ func (cli *genericHTTPMediumClient) Echo(action, msg string) error {
ctx := context.Background()

url := fmt.Sprintf("http://example.com/test/obj/%s", action)
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.MediumData)))
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.MediumString)))
if err != nil {
return err
}
@@ -176,7 +176,7 @@ func (cli *genericHTTPLargeClient) Echo(action, msg string) error {
ctx := context.Background()

url := fmt.Sprintf("http://example.com/test/obj/%s", action)
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.LargeData)))
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.LargeString)))
if err != nil {
return err
}
6 changes: 3 additions & 3 deletions generic/http/client/fallback/kitex_client.go
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ func (cli *genericHTTPSmallClient) Echo(action, msg string) error {
ctx := context.Background()

url := fmt.Sprintf("http://example.com/test/obj/%s", action)
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.SmallData)))
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.SmallString)))
if err != nil {
return err
}
@@ -118,7 +118,7 @@ func (cli *genericHTTPMediumClient) Echo(action, msg string) error {
ctx := context.Background()

url := fmt.Sprintf("http://example.com/test/obj/%s", action)
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.MediumData)))
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.MediumString)))
if err != nil {
return err
}
@@ -170,7 +170,7 @@ func (cli *genericHTTPLargeClient) Echo(action, msg string) error {
ctx := context.Background()

url := fmt.Sprintf("http://example.com/test/obj/%s", action)
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.LargeData)))
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer([]byte(data.LargeString)))
if err != nil {
return err
}
84 changes: 57 additions & 27 deletions generic/map/client/kitex_client.go
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* limitations under the License.
*/

package main
package kclient

import (
"context"
@@ -28,31 +28,69 @@ import (
"github.com/cloudwego/kitex/pkg/transmeta"
"github.com/cloudwego/kitex/transport"

"github.com/cloudwego/kitex-benchmark/generic/data"
"github.com/cloudwego/kitex-benchmark/runner"
)

var (
subMsg1 = map[string]interface{}{
"id": int64(123),
"value": "hello",
func NewGenericMapSmallClient(opt *runner.Options) runner.Client {
p, err := generic.NewThriftFileProvider("./codec/thrift/echo.thrift")
if err != nil {
panic(err)
}
subMsg2 = map[string]interface{}{
"id": int64(321),
"value": "world",
// 构造map 请求和返回类型的泛化调用
g, err := generic.MapThriftGeneric(p)
if err != nil {
panic(err)
}
msg1 = map[string]interface{}{
"id": int64(123),
"value": "hello",
"subMessages": []interface{}{subMsg1, subMsg2},
cli := &genericMapClient{}
cli.client, err = genericclient.NewClient("test.echo.kitex", g,
client.WithTransportProtocol(transport.TTHeader),
client.WithHostPorts(opt.Address),
client.WithMetaHandler(transmeta.ClientTTHeaderHandler),
client.WithLongConnection(
connpool.IdleConfig{MaxIdlePerAddress: 1000, MaxIdleGlobal: 1000, MaxIdleTimeout: time.Minute}),
)
if err != nil {
panic(err)
}
msg2 = map[string]interface{}{
"id": int64(321),
"value": "world",
"subMessages": []interface{}{subMsg2, subMsg1},
cli.reqPool = &sync.Pool{
New: func() interface{} {
return data.SmallMap
},
}
)
return cli
}

func NewGenericMapMediumClient(opt *runner.Options) runner.Client {
p, err := generic.NewThriftFileProvider("./codec/thrift/echo.thrift")
if err != nil {
panic(err)
}
// 构造map 请求和返回类型的泛化调用
g, err := generic.MapThriftGeneric(p)
if err != nil {
panic(err)
}
cli := &genericMapClient{}
cli.client, err = genericclient.NewClient("test.echo.kitex", g,
client.WithTransportProtocol(transport.TTHeader),
client.WithHostPorts(opt.Address),
client.WithMetaHandler(transmeta.ClientTTHeaderHandler),
client.WithLongConnection(
connpool.IdleConfig{MaxIdlePerAddress: 1000, MaxIdleGlobal: 1000, MaxIdleTimeout: time.Minute}),
)
if err != nil {
panic(err)
}
cli.reqPool = &sync.Pool{
New: func() interface{} {
return data.MediumMap
},
}
return cli
}

func NewGenericMapClient(opt *runner.Options) runner.Client {
func NewGenericMapLargeClient(opt *runner.Options) runner.Client {
p, err := generic.NewThriftFileProvider("./codec/thrift/echo.thrift")
if err != nil {
panic(err)
@@ -75,15 +113,7 @@ func NewGenericMapClient(opt *runner.Options) runner.Client {
}
cli.reqPool = &sync.Pool{
New: func() interface{} {
return map[string]interface{}{
"msgMap": map[interface{}]interface{}{
"v1": subMsg1,
"v2": subMsg2,
},
"subMsgs": []interface{}{subMsg1, subMsg2},
"msgSet": []interface{}{msg1, msg2},
"flagMsg": msg1,
}
return data.LargeMap
},
}
return cli
Original file line number Diff line number Diff line change
@@ -17,10 +17,11 @@
package main

import (
kclient "github.com/cloudwego/kitex-benchmark/generic/map/client"
"github.com/cloudwego/kitex-benchmark/runner"
)

// main is use for routing.
func main() {
runner.Main("GenericMap", NewGenericMapClient)
runner.Main("GenericMap", kclient.NewGenericMapLargeClient)
}
27 changes: 27 additions & 0 deletions generic/map/client/medium/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2022 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 main

import (
kclient "github.com/cloudwego/kitex-benchmark/generic/map/client"
"github.com/cloudwego/kitex-benchmark/runner"
)

// main is use for routing.
func main() {
runner.Main("GenericMap", kclient.NewGenericMapMediumClient)
}
Original file line number Diff line number Diff line change
@@ -17,10 +17,11 @@
package main

import (
kclient "github.com/cloudwego/kitex-benchmark/generic/map/client"
"github.com/cloudwego/kitex-benchmark/runner"
)

// main is use for routing.
func main() {
runner.Main("GenericOrdinary", NewGenericOrdinaryClient)
runner.Main("GenericMap", kclient.NewGenericMapSmallClient)
}
Loading