github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/conv/p2j/conv_timing_test.go (about)

     1  package p2j
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/cloudwego/dynamicgo/conv"
     8  	"github.com/cloudwego/dynamicgo/internal/util_test"
     9  	"github.com/cloudwego/dynamicgo/proto"
    10  )
    11  
    12  func BenchmarkProtobuf2JSON_DynamicGo(t *testing.B) {
    13  	includeDirs := util_test.MustGitPath("testdata/idl/") // includeDirs is used to find the include files.
    14  	desc := proto.FnRequest(proto.GetFnDescFromFile("testdata/idl/example2.proto", "ExampleMethod", proto.Options{}, includeDirs))
    15  	conv := NewBinaryConv(conv.Options{})
    16  	in := readExampleReqProtoBufData()
    17  	ctx := context.Background()
    18  	out, err := conv.Do(ctx, desc, in)
    19  	//print(string(out))
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	t.SetBytes(int64(len(in)))
    24  	t.ResetTimer()
    25  	for i := 0; i < t.N; i++ {
    26  		out = out[:0]
    27  		_ = conv.DoInto(ctx, desc, in, &out)
    28  	}
    29  }
    30  
    31  func BenchmarkProtobuf2JSON_Parallel_DynamicGo(t *testing.B) {
    32  	includeDirs := util_test.MustGitPath("testdata/idl/") // includeDirs is used to find the include files.
    33  	desc := proto.FnRequest(proto.GetFnDescFromFile("testdata/idl/example2.proto", "ExampleMethod", proto.Options{}, includeDirs))
    34  	conv := NewBinaryConv(conv.Options{})
    35  	in := readExampleReqProtoBufData()
    36  	ctx := context.Background()
    37  	out, err := conv.Do(ctx, desc, in)
    38  	//print(string(out))
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	t.SetBytes(int64(len(in)))
    43  	t.ResetTimer()
    44  	t.RunParallel(func(p *testing.PB) {
    45  		buf := make([]byte, len(out))
    46  		for p.Next() {
    47  			buf = buf[:0]
    48  			_ = conv.DoInto(ctx, desc, in, &buf)
    49  		}
    50  	})
    51  }