github.com/fananchong/cstruct-go@v0.0.0-20220616060855-b65d9a2f2e17/benchmarks/cstrucgo_test.go (about)

     1  package benchmarks
     2  
     3  import (
     4  	"testing"
     5  
     6  	cstruct "github.com/fananchong/cstruct-go"
     7  	"github.com/gogo/protobuf/proto"
     8  )
     9  
    10  type mystruct2 struct {
    11  	F3 float64
    12  	F4 string
    13  }
    14  
    15  type mystruct1 struct {
    16  	F1  bool
    17  	F2  float32
    18  	F3  float64
    19  	F4  string
    20  	F5  []byte
    21  	F6  int8
    22  	F7  int16
    23  	F9  int32
    24  	F11 int64
    25  	F12 uint8
    26  	F13 uint16
    27  	F15 uint32
    28  	F17 uint64
    29  	S0  mystruct2
    30  	F29 []string
    31  	F30 []*mystruct2
    32  }
    33  
    34  func Benchmark_CStructGO(b *testing.B) {
    35  	a0 := &mystruct1{}
    36  	a0.F1 = true
    37  	a0.F2 = 0.98
    38  	a0.F3 = 999.777
    39  	a0.F4 = "hellohellohellohellohellohellohellohellohellohello"
    40  	a0.F5 = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    41  	a0.F6 = 126
    42  	a0.F7 = 32766
    43  	a0.F9 = -2147483646
    44  	a0.F11 = -9223372036854774807
    45  	a0.F12 = 254
    46  	a0.F13 = 65534
    47  	a0.F15 = 4294967295
    48  	a0.F17 = 18446744073709551614
    49  	a0.S0.F3 = 988.07
    50  	a0.S0.F4 = "world1world1world1world1world1world1world1world1world1"
    51  	a0.F29 = []string{"hello", "", "world", "", "123"}
    52  	a0.F30 = []*mystruct2{&mystruct2{111, "1111111111"}, &mystruct2{222, "2222222222"}, &mystruct2{333, "3333333333"}}
    53  
    54  	for i := 0; i < b.N; i++ {
    55  		buf_l, _ := cstruct.Marshal(a0)
    56  		a1 := &mystruct1{}
    57  		cstruct.Unmarshal(buf_l, a1)
    58  	}
    59  }
    60  
    61  func Benchmark_Protobuf(b *testing.B) {
    62  	a0 := &Myproto1{}
    63  	a0.F1 = true
    64  	a0.F2 = 0.98
    65  	a0.F3 = 999.777
    66  	a0.F4 = "hellohellohellohellohellohellohellohellohellohello"
    67  	a0.F5 = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    68  	a0.F6 = 126
    69  	a0.F7 = 32766
    70  	a0.F9 = -2147483646
    71  	a0.F11 = -9223372036854774807
    72  	a0.F12 = 254
    73  	a0.F13 = 65534
    74  	a0.F15 = 4294967295
    75  	a0.F17 = 18446744073709551614
    76  	a0.S0 = &Myproto2{}
    77  	a0.S0.F3 = 988.07
    78  	a0.S0.F4 = "world1world1world1world1world1world1world1world1world1"
    79  	a0.F29 = []string{"hello", "", "world", "", "123"}
    80  	a0.F30 = []*Myproto2{&Myproto2{111, "1111111111"}, &Myproto2{222, "2222222222"}, &Myproto2{333, "3333333333"}}
    81  	for i := 0; i < b.N; i++ {
    82  		buf_l, _ := proto.Marshal(a0)
    83  		a1 := &Myproto1{}
    84  		proto.Unmarshal(buf_l, a1)
    85  	}
    86  }
    87  
    88  func Benchmark_GoGoProtobuf(b *testing.B) {
    89  	a0 := &Myproto3{}
    90  	a0.F1 = true
    91  	a0.F2 = 0.98
    92  	a0.F3 = 999.777
    93  	a0.F4 = "hellohellohellohellohellohellohellohellohellohello"
    94  	a0.F5 = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    95  	a0.F6 = 126
    96  	a0.F7 = 32766
    97  	a0.F9 = -2147483646
    98  	a0.F11 = -9223372036854774807
    99  	a0.F12 = 254
   100  	a0.F13 = 65534
   101  	a0.F15 = 4294967295
   102  	a0.F17 = 18446744073709551614
   103  	a0.S0 = &Myproto4{}
   104  	a0.S0.F3 = 988.07
   105  	a0.S0.F4 = "world1world1world1world1world1world1world1world1world1"
   106  	a0.F29 = []string{"hello", "", "world", "", "123"}
   107  	a0.F30 = []*Myproto4{&Myproto4{111, "1111111111"}, &Myproto4{222, "2222222222"}, &Myproto4{333, "3333333333"}}
   108  	for i := 0; i < b.N; i++ {
   109  		buf_l, _ := proto.Marshal(a0)
   110  		a1 := &Myproto3{}
   111  		proto.Unmarshal(buf_l, a1)
   112  	}
   113  }