sizetest.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
  2. // http://github.com/gogo/protobuf
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. package size
  27. import (
  28. "github.com/gogo/protobuf/gogoproto"
  29. "github.com/gogo/protobuf/plugin/testgen"
  30. "github.com/gogo/protobuf/protoc-gen-gogo/generator"
  31. )
  32. type test struct {
  33. *generator.Generator
  34. }
  35. func NewTest(g *generator.Generator) testgen.TestPlugin {
  36. return &test{g}
  37. }
  38. func (p *test) Generate(imports generator.PluginImports, file *generator.FileDescriptor) bool {
  39. used := false
  40. randPkg := imports.NewImport("math/rand")
  41. timePkg := imports.NewImport("time")
  42. testingPkg := imports.NewImport("testing")
  43. protoPkg := imports.NewImport("github.com/gogo/protobuf/proto")
  44. if !gogoproto.ImportsGoGoProto(file.FileDescriptorProto) {
  45. protoPkg = imports.NewImport("github.com/golang/protobuf/proto")
  46. }
  47. for _, message := range file.Messages() {
  48. ccTypeName := generator.CamelCaseSlice(message.TypeName())
  49. sizeName := ""
  50. if gogoproto.IsSizer(file.FileDescriptorProto, message.DescriptorProto) {
  51. sizeName = "Size"
  52. } else if gogoproto.IsProtoSizer(file.FileDescriptorProto, message.DescriptorProto) {
  53. sizeName = "ProtoSize"
  54. } else {
  55. continue
  56. }
  57. if message.DescriptorProto.GetOptions().GetMapEntry() {
  58. continue
  59. }
  60. if gogoproto.HasTestGen(file.FileDescriptorProto, message.DescriptorProto) {
  61. used = true
  62. p.P(`func Test`, ccTypeName, sizeName, `(t *`, testingPkg.Use(), `.T) {`)
  63. p.In()
  64. p.P(`seed := `, timePkg.Use(), `.Now().UnixNano()`)
  65. p.P(`popr := `, randPkg.Use(), `.New(`, randPkg.Use(), `.NewSource(seed))`)
  66. p.P(`p := NewPopulated`, ccTypeName, `(popr, true)`)
  67. p.P(`size2 := `, protoPkg.Use(), `.Size(p)`)
  68. p.P(`data, err := `, protoPkg.Use(), `.Marshal(p)`)
  69. p.P(`if err != nil {`)
  70. p.In()
  71. p.P(`t.Fatalf("seed = %d, err = %v", seed, err)`)
  72. p.Out()
  73. p.P(`}`)
  74. p.P(`size := p.`, sizeName, `()`)
  75. p.P(`if len(data) != size {`)
  76. p.In()
  77. p.P(`t.Errorf("seed = %d, size %v != marshalled size %v", seed, size, len(data))`)
  78. p.Out()
  79. p.P(`}`)
  80. p.P(`if size2 != size {`)
  81. p.In()
  82. p.P(`t.Errorf("seed = %d, size %v != before marshal proto.Size %v", seed, size, size2)`)
  83. p.Out()
  84. p.P(`}`)
  85. p.P(`size3 := `, protoPkg.Use(), `.Size(p)`)
  86. p.P(`if size3 != size {`)
  87. p.In()
  88. p.P(`t.Errorf("seed = %d, size %v != after marshal proto.Size %v", seed, size, size3)`)
  89. p.Out()
  90. p.P(`}`)
  91. p.Out()
  92. p.P(`}`)
  93. p.P()
  94. }
  95. if gogoproto.HasBenchGen(file.FileDescriptorProto, message.DescriptorProto) {
  96. used = true
  97. p.P(`func Benchmark`, ccTypeName, sizeName, `(b *`, testingPkg.Use(), `.B) {`)
  98. p.In()
  99. p.P(`popr := `, randPkg.Use(), `.New(`, randPkg.Use(), `.NewSource(616))`)
  100. p.P(`total := 0`)
  101. p.P(`pops := make([]*`, ccTypeName, `, 1000)`)
  102. p.P(`for i := 0; i < 1000; i++ {`)
  103. p.In()
  104. p.P(`pops[i] = NewPopulated`, ccTypeName, `(popr, false)`)
  105. p.Out()
  106. p.P(`}`)
  107. p.P(`b.ResetTimer()`)
  108. p.P(`for i := 0; i < b.N; i++ {`)
  109. p.In()
  110. p.P(`total += pops[i%1000].`, sizeName, `()`)
  111. p.Out()
  112. p.P(`}`)
  113. p.P(`b.SetBytes(int64(total / b.N))`)
  114. p.Out()
  115. p.P(`}`)
  116. p.P()
  117. }
  118. }
  119. return used
  120. }
  121. func init() {
  122. testgen.RegisterTestPlugin(NewTest)
  123. }