github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/conv/t2j/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 t2j 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/cloudwego/dynamicgo/conv" 24 cv "github.com/cloudwego/dynamicgo/conv" 25 "github.com/cloudwego/dynamicgo/http" 26 "github.com/cloudwego/dynamicgo/thrift" 27 ) 28 29 func BenchmarkThrift2JSON_DynamicGo(t *testing.B) { 30 desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{})) 31 conv := NewBinaryConv(conv.Options{}) 32 in := getExample3Data() 33 ctx := context.Background() 34 out, err := conv.Do(ctx, desc, in) 35 if err != nil { 36 t.Fatal(err) 37 } 38 t.SetBytes(int64(len(in))) 39 t.ResetTimer() 40 for i := 0; i < t.N; i++ { 41 out = out[:0] 42 _ = conv.DoInto(ctx, desc, in, &out) 43 } 44 } 45 46 func BenchmarkThrift2HTTP_DynamicGo(b *testing.B) { 47 desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{})) 48 conv := NewBinaryConv(conv.Options{ 49 EnableValueMapping: true, 50 EnableHttpMapping: true, 51 OmitHttpMappingErrors: true, 52 }) 53 in := getExample3Data() 54 ctx := context.Background() 55 resp := http.NewHTTPResponse() 56 ctx = context.WithValue(ctx, cv.CtxKeyHTTPResponse, resp) 57 out, err := conv.Do(ctx, desc, in) 58 if err != nil { 59 b.Fatal(err) 60 } 61 b.SetBytes(int64(len(in))) 62 b.ResetTimer() 63 for i := 0; i < b.N; i++ { 64 ctx := context.Background() 65 resp := http.NewHTTPResponse() 66 ctx = context.WithValue(ctx, cv.CtxKeyHTTPResponse, resp) 67 out = out[:0] 68 _, _ = conv.Do(ctx, desc, in) 69 } 70 } 71 72 func BenchmarkThrift2JSON_Parallel_DynamicGo(t *testing.B) { 73 desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{})) 74 conv := NewBinaryConv(conv.Options{}) 75 in := getExample3Data() 76 ctx := context.Background() 77 out, err := conv.Do(ctx, desc, in) 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 t.SetBytes(int64(len(in))) 83 t.ResetTimer() 84 t.RunParallel(func(p *testing.PB) { 85 buf := make([]byte, len(out)) 86 for p.Next() { 87 buf = buf[:0] 88 _ = conv.DoInto(ctx, desc, in, &buf) 89 } 90 }) 91 } 92 93 func BenchmarkThrift2HTTP_Parallel_DynamicGo(b *testing.B) { 94 desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{})) 95 conv := NewBinaryConv(conv.Options{ 96 EnableValueMapping: true, 97 EnableHttpMapping: true, 98 OmitHttpMappingErrors: true, 99 }) 100 in := getExample3Data() 101 ctx := context.Background() 102 resp := http.NewHTTPResponse() 103 ctx = context.WithValue(ctx, cv.CtxKeyHTTPResponse, resp) 104 _, err := conv.Do(ctx, desc, in) 105 if err != nil { 106 b.Fatal(err) 107 } 108 109 b.SetBytes(int64(len(in))) 110 b.ResetTimer() 111 b.RunParallel(func(p *testing.PB) { 112 for p.Next() { 113 ctx := context.Background() 114 resp := http.NewHTTPResponse() 115 ctx = context.WithValue(ctx, cv.CtxKeyHTTPResponse, resp) 116 _, _ = conv.Do(ctx, desc, in) 117 } 118 }) 119 }