github.com/RomiChan/protobuf@v0.1.1-0.20230204044148-2ed269a2e54d/internal/benchmark/bench_test.go (about)

     1  package benchmark
     2  
     3  import (
     4  	"testing"
     5  
     6  	"google.golang.org/protobuf/proto"
     7  
     8  	proto2 "github.com/RomiChan/protobuf/proto"
     9  )
    10  
    11  var (
    12  	PBSmall = &BenchSmall{
    13  		Action: proto.String("benchmark"),
    14  		Key:    []byte("data to be sent"),
    15  	}
    16  
    17  	PBMedium = &BenchMedium{
    18  		Name:   proto.String("Tester"),
    19  		Age:    proto.Int64(20),
    20  		Height: proto.Float32(5.8),
    21  		Weight: proto.Float64(180.7),
    22  		Alive:  proto.Bool(true),
    23  		Desc: []byte(`If you’ve ever heard of ProtoBuf you may be thinking 
    24  		that the results of this benchmarking experiment will be obvious,
    25  		JSON < ProtoBuf.`),
    26  	}
    27  
    28  	PBLarge = &BenchLarge{
    29  		Name:     proto.String("Tester"),
    30  		Age:      proto.Int64(20),
    31  		Height:   proto.Float32(5.8),
    32  		Weight:   proto.Float64(180.7),
    33  		Alive:    proto.Bool(true),
    34  		Desc:     []byte("Lets benchmark some json and protobuf"),
    35  		Nickname: proto.String("Another name"),
    36  		Num:      proto.Int64(2314),
    37  		Flt:      proto.Float32(123451231.1234),
    38  		Data: []byte(`If you’ve ever heard of ProtoBuf you may be thinking that
    39  		the results of this benchmarking experiment will be obvious, JSON < ProtoBuf.
    40  		My interest was in how much they actually differ in practice.
    41  		How do they compare on a couple of different metrics, specifically serialization
    42  		and de-serialization speeds, and the memory footprint of encoding the data.
    43  		I was also curious about how the different serialization methods would
    44  		behave under small, medium, and large chunks of data.`),
    45  	}
    46  
    47  	PBNested = &BenchNested{
    48  		Small:  PBSmall,
    49  		Medium: PBMedium,
    50  		Large:  PBLarge,
    51  	}
    52  )
    53  
    54  func BenchmarkGoogleProtobufMarshal(b *testing.B) {
    55  	s := PBSmall
    56  	m := PBMedium
    57  	l := PBLarge
    58  	nested := PBNested
    59  
    60  	b.ResetTimer()
    61  
    62  	b.Run("SmallData", func(b *testing.B) {
    63  		b.ReportAllocs()
    64  		var d []byte
    65  		for n := 0; n < b.N; n++ {
    66  			d, _ = proto.Marshal(s)
    67  		}
    68  		b.SetBytes(int64(len(d)))
    69  	})
    70  	b.Run("MediumData", func(b *testing.B) {
    71  		b.ReportAllocs()
    72  		var d []byte
    73  		for n := 0; n < b.N; n++ {
    74  			d, _ = proto.Marshal(m)
    75  		}
    76  		b.SetBytes(int64(len(d)))
    77  	})
    78  	b.Run("LargeData", func(b *testing.B) {
    79  		b.ReportAllocs()
    80  		var d []byte
    81  		for n := 0; n < b.N; n++ {
    82  			d, _ = proto.Marshal(l)
    83  		}
    84  		b.SetBytes(int64(len(d)))
    85  	})
    86  	b.Run("AllData", func(b *testing.B) {
    87  		b.ReportAllocs()
    88  		var d []byte
    89  		for n := 0; n < b.N; n++ {
    90  			d, _ = proto.Marshal(nested)
    91  		}
    92  		b.SetBytes(int64(len(d)))
    93  	})
    94  }
    95  
    96  func BenchmarkRomiChanProtobufMarshal(b *testing.B) {
    97  	s := PBSmall
    98  	m := PBMedium
    99  	l := PBLarge
   100  	nested := PBNested
   101  
   102  	b.ResetTimer()
   103  
   104  	b.Run("SmallData", func(b *testing.B) {
   105  		b.ReportAllocs()
   106  		var d []byte
   107  		for n := 0; n < b.N; n++ {
   108  			d, _ = proto2.Marshal(s)
   109  		}
   110  		b.SetBytes(int64(len(d)))
   111  	})
   112  	b.Run("MediumData", func(b *testing.B) {
   113  		b.ReportAllocs()
   114  		var d []byte
   115  		for n := 0; n < b.N; n++ {
   116  			d, _ = proto2.Marshal(m)
   117  		}
   118  		b.SetBytes(int64(len(d)))
   119  	})
   120  	b.Run("LargeData", func(b *testing.B) {
   121  		b.ReportAllocs()
   122  		var d []byte
   123  		for n := 0; n < b.N; n++ {
   124  			d, _ = proto2.Marshal(l)
   125  		}
   126  		b.SetBytes(int64(len(d)))
   127  	})
   128  	b.Run("AllData", func(b *testing.B) {
   129  		b.ReportAllocs()
   130  		var d []byte
   131  		for n := 0; n < b.N; n++ {
   132  			d, _ = proto2.Marshal(nested)
   133  		}
   134  		b.SetBytes(int64(len(d)))
   135  	})
   136  }
   137  
   138  func BenchmarkGoogleProtobufUnmarshal(b *testing.B) {
   139  	s := PBSmall
   140  	m := PBMedium
   141  	l := PBLarge
   142  
   143  	sd, _ := proto.Marshal(s)
   144  	md, _ := proto.Marshal(m)
   145  	ld, _ := proto.Marshal(l)
   146  
   147  	var sf BenchSmall
   148  	var mf BenchMedium
   149  	var lf BenchLarge
   150  
   151  	b.ResetTimer()
   152  
   153  	b.Run("SmallData", func(b *testing.B) {
   154  		b.ReportAllocs()
   155  		b.SetBytes(int64(len(sd)))
   156  		for n := 0; n < b.N; n++ {
   157  			_ = proto.Unmarshal(sd, &sf)
   158  		}
   159  	})
   160  	b.Run("MediumData", func(b *testing.B) {
   161  		b.ReportAllocs()
   162  		b.SetBytes(int64(len(md)))
   163  		for n := 0; n < b.N; n++ {
   164  			_ = proto.Unmarshal(md, &mf)
   165  		}
   166  	})
   167  	b.Run("LargeData", func(b *testing.B) {
   168  		b.ReportAllocs()
   169  		b.SetBytes(int64(len(ld)))
   170  		for n := 0; n < b.N; n++ {
   171  			_ = proto.Unmarshal(ld, &lf)
   172  		}
   173  	})
   174  }
   175  
   176  func BenchmarkRomiChanProtobufUnmarshal(b *testing.B) {
   177  	s := PBSmall
   178  	m := PBMedium
   179  	l := PBLarge
   180  
   181  	sd, _ := proto.Marshal(s)
   182  	md, _ := proto.Marshal(m)
   183  	ld, _ := proto.Marshal(l)
   184  
   185  	var sf BenchSmall
   186  	var mf BenchMedium
   187  	var lf BenchLarge
   188  
   189  	b.ResetTimer()
   190  
   191  	b.Run("SmallData", func(b *testing.B) {
   192  		b.ReportAllocs()
   193  		b.SetBytes(int64(len(sd)))
   194  		for n := 0; n < b.N; n++ {
   195  			_ = proto2.Unmarshal(sd, &sf)
   196  		}
   197  	})
   198  	b.Run("MediumData", func(b *testing.B) {
   199  		b.ReportAllocs()
   200  		b.SetBytes(int64(len(md)))
   201  		for n := 0; n < b.N; n++ {
   202  			_ = proto2.Unmarshal(md, &mf)
   203  		}
   204  	})
   205  	b.Run("LargeData", func(b *testing.B) {
   206  		b.ReportAllocs()
   207  		b.SetBytes(int64(len(ld)))
   208  		for n := 0; n < b.N; n++ {
   209  			_ = proto2.Unmarshal(ld, &lf)
   210  		}
   211  	})
   212  }