github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/conv/j2t/conv_timing_test.go (about) 1 /** 2 * Copyright 2023 CloudWeGo Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package j2t 18 19 import ( 20 "context" 21 "encoding/json" 22 "testing" 23 24 "github.com/cloudwego/dynamicgo/conv" 25 "github.com/cloudwego/dynamicgo/testdata/kitex_gen/example3" 26 27 "github.com/stretchr/testify/require" 28 ) 29 30 func BenchmarkConvJSON2Thrift_DynamicGo(b *testing.B) { 31 desc := getExampleDesc() 32 data := getExampleData() 33 cv := NewBinaryConv(conv.Options{}) 34 ctx := context.Background() 35 out, err := cv.Do(ctx, desc, data) 36 require.NoError(b, err) 37 exp := example3.NewExampleReq() 38 err = json.Unmarshal(data, exp) 39 require.Nil(b, err) 40 act := example3.NewExampleReq() 41 _, err = act.FastRead(out) 42 require.Nil(b, err) 43 require.Equal(b, exp, act) 44 b.ResetTimer() 45 for i := 0; i < b.N; i++ { 46 out = out[:0] 47 _ = cv.DoInto(ctx, desc, data, &out) 48 } 49 } 50 51 func BenchmarkConvHTTP2Thrift_DynamicGo(b *testing.B) { 52 desc := getExampleDesc() 53 data := getExampleData() 54 exp := example3.NewExampleReq() 55 err := json.Unmarshal(data, exp) 56 require.Nil(b, err) 57 req := getExampleReq(exp, true, data) 58 cv := NewBinaryConv(conv.Options{ 59 EnableHttpMapping: true, 60 }) 61 ctx := context.Background() 62 ctx = context.WithValue(ctx, conv.CtxKeyHTTPRequest, req) 63 out, err := cv.Do(ctx, desc, data) 64 require.NoError(b, err) 65 act := example3.NewExampleReq() 66 _, err = act.FastRead(out) 67 require.Nil(b, err) 68 require.Equal(b, exp, act) 69 70 b.ResetTimer() 71 for i := 0; i < b.N; i++ { 72 _, _ = cv.Do(ctx, desc, data) 73 } 74 } 75 76 func BenchmarkConvJSON2Thrift_Parallel_DynamicGo(b *testing.B) { 77 desc := getExampleDesc() 78 data := getExampleData() 79 cv := NewBinaryConv(conv.Options{}) 80 ctx := context.Background() 81 out, err := cv.Do(ctx, desc, data) 82 require.NoError(b, err) 83 exp := example3.NewExampleReq() 84 err = json.Unmarshal(data, exp) 85 require.Nil(b, err) 86 act := example3.NewExampleReq() 87 _, err = act.FastRead(out) 88 require.Nil(b, err) 89 require.Equal(b, exp, act) 90 91 b.ResetTimer() 92 b.RunParallel(func(b *testing.PB) { 93 buf := make([]byte, 0, len(out)) 94 for b.Next() { 95 buf = buf[:0] 96 _ = cv.DoInto(ctx, desc, data, &buf) 97 } 98 }) 99 } 100 101 func BenchmarkConvHTTP2Thrift_Parallel_DynamicGo(b *testing.B) { 102 desc := getExampleDesc() 103 data := getExampleData() 104 exp := example3.NewExampleReq() 105 err := json.Unmarshal(data, exp) 106 require.Nil(b, err) 107 req := getExampleReq(exp, true, data) 108 cv := NewBinaryConv(conv.Options{ 109 EnableHttpMapping: true, 110 }) 111 ctx := context.Background() 112 ctx = context.WithValue(ctx, conv.CtxKeyHTTPRequest, req) 113 out, err := cv.Do(ctx, desc, data) 114 require.NoError(b, err) 115 act := example3.NewExampleReq() 116 _, err = act.FastRead(out) 117 require.Nil(b, err) 118 require.Equal(b, exp, act) 119 120 b.ResetTimer() 121 b.RunParallel(func(b *testing.PB) { 122 for b.Next() { 123 _, _ = cv.Do(ctx, desc, data) 124 } 125 }) 126 }