github.com/gogo/protobuf@v1.3.2/plugin/size/sizetest.go (about)

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