go_benchmark_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "flag"
  6. "testing"
  7. "os"
  8. benchmarkWrapper "./tmp"
  9. proto "github.com/golang/protobuf/proto"
  10. googleMessage1Proto3 "./tmp/datasets/google_message1/proto3"
  11. googleMessage1Proto2 "./tmp/datasets/google_message1/proto2"
  12. googleMessage2 "./tmp/datasets/google_message2"
  13. googleMessage3 "./tmp/datasets/google_message3"
  14. googleMessage4 "./tmp/datasets/google_message4"
  15. )
  16. // Data is returned by the Load function.
  17. type Data struct {
  18. // Marshalled is a slice of marshalled protocol
  19. // buffers. 1:1 with Unmarshalled.
  20. Marshalled [][]byte
  21. // Unmarshalled is a slice of unmarshalled protocol
  22. // buffers. 1:1 with Marshalled.
  23. Unmarshalled []proto.Message
  24. count int
  25. }
  26. var data *Data
  27. var counter int
  28. type GetDefaultInstanceFunction func() proto.Message
  29. var getDefaultInstance GetDefaultInstanceFunction
  30. // This is used to getDefaultInstance for a message type.
  31. func generateGetDefaltInstanceFunction(dataset benchmarkWrapper.BenchmarkDataset) error {
  32. switch dataset.MessageName {
  33. case "benchmarks.proto3.GoogleMessage1":
  34. getDefaultInstance = func() proto.Message { return &googleMessage1Proto3.GoogleMessage1{} }
  35. return nil
  36. case "benchmarks.proto2.GoogleMessage1":
  37. getDefaultInstance = func() proto.Message { return &googleMessage1Proto2.GoogleMessage1{} }
  38. return nil
  39. case "benchmarks.proto2.GoogleMessage2":
  40. getDefaultInstance = func() proto.Message { return &googleMessage2.GoogleMessage2{} }
  41. return nil
  42. case "benchmarks.google_message3.GoogleMessage3":
  43. getDefaultInstance = func() proto.Message { return &googleMessage3.GoogleMessage3{} }
  44. return nil
  45. case "benchmarks.google_message4.GoogleMessage4":
  46. getDefaultInstance = func() proto.Message { return &googleMessage4.GoogleMessage4{} }
  47. return nil
  48. default:
  49. return errors.New("Unknown message type: " + dataset.MessageName)
  50. }
  51. }
  52. func TestMain(m *testing.M) {
  53. flag.Parse()
  54. data = new(Data)
  55. rawData, error := ioutil.ReadFile(flag.Arg(0))
  56. if error != nil {
  57. panic("Couldn't find file" + flag.Arg(0))
  58. }
  59. var dataset benchmarkWrapper.BenchmarkDataset
  60. if err1 := proto.Unmarshal(rawData, &dataset); err1 != nil {
  61. panic("The raw input data can't be parse into BenchmarkDataset message.")
  62. }
  63. generateGetDefaltInstanceFunction(dataset)
  64. for _, payload := range dataset.Payload {
  65. data.Marshalled = append(data.Marshalled, payload)
  66. m := getDefaultInstance()
  67. proto.Unmarshal(payload, m)
  68. data.Unmarshalled = append(data.Unmarshalled, m)
  69. }
  70. data.count = len(data.Unmarshalled)
  71. os.Exit(m.Run())
  72. }
  73. func BenchmarkUnmarshal(b *testing.B) {
  74. b.ReportAllocs()
  75. for i := 0; i < b.N; i++ {
  76. payload := data.Marshalled[counter % data.count]
  77. out := getDefaultInstance()
  78. if err := proto.Unmarshal(payload, out); err != nil {
  79. b.Fatalf("can't unmarshal message %d %v", counter % data.count, err)
  80. }
  81. counter++
  82. }
  83. }
  84. func BenchmarkMarshal(b *testing.B) {
  85. b.ReportAllocs()
  86. for i := 0; i < b.N; i++ {
  87. m := data.Unmarshalled[counter % data.count]
  88. if _, err := proto.Marshal(m); err != nil {
  89. b.Fatalf("can't marshal message %d %+v: %v", counter % data.count, m, err)
  90. }
  91. counter++
  92. }
  93. }
  94. func BenchmarkSize(b *testing.B) {
  95. b.ReportAllocs()
  96. for i := 0; i < b.N; i++ {
  97. proto.Size(data.Unmarshalled[counter % data.count])
  98. counter++
  99. }
  100. }
  101. func BenchmarkClone(b *testing.B) {
  102. b.ReportAllocs()
  103. for i := 0; i < b.N; i++ {
  104. proto.Clone(data.Unmarshalled[counter % data.count])
  105. counter++
  106. }
  107. }
  108. func BenchmarkMerge(b *testing.B) {
  109. b.ReportAllocs()
  110. for i := 0; i < b.N; i++ {
  111. out := getDefaultInstance()
  112. proto.Merge(out, data.Unmarshalled[counter % data.count])
  113. counter++
  114. }
  115. }