github.com/gogo/protobuf@v1.3.2/test/mixbench/mixbench.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 main 30 31 import ( 32 "flag" 33 "fmt" 34 "io/ioutil" 35 "os/exec" 36 "sort" 37 "strings" 38 ) 39 40 func bench(folder, rgx, outFileName string) { 41 var test = exec.Command("go", "test", "-test.timeout=20m", "-test.v", "-test.run=XXX", "-test.bench="+rgx, folder) 42 fmt.Printf("benching %v - %v - %v\n", folder, rgx, outFileName) 43 out, err := test.CombinedOutput() 44 fmt.Printf("bench output: %v\n", string(out)) 45 if err != nil { 46 panic(err) 47 } 48 if err := ioutil.WriteFile(outFileName, out, 0666); err != nil { 49 panic(err) 50 } 51 } 52 53 func main() { 54 flag.Parse() 55 fmt.Printf("Running benches: %v\n", benchList) 56 for _, bench := range strings.Split(benchList, " ") { 57 b, ok := benches[bench] 58 if !ok { 59 fmt.Printf("No benchmark with name: %v\n", bench) 60 continue 61 } 62 b() 63 } 64 if strings.Contains(benchList, "all") { 65 fmt.Println("Running benchcmp will show the performance difference between using reflect and generated code for marshalling and unmarshalling of protocol buffers") 66 fmt.Println("benchcmp ./test/mixbench/marshal.txt ./test/mixbench/marshaler.txt") 67 fmt.Println("benchcmp ./test/mixbench/unmarshal.txt ./test/mixbench/unmarshaler.txt") 68 } 69 } 70 71 var benches = make(map[string]func()) 72 73 var benchList string 74 75 func init() { 76 benches["marshaler"] = func() { bench("./test/combos/both/", "ProtoMarshal", "./test/mixbench/marshaler.txt") } 77 benches["marshal"] = func() { bench("./test/", "ProtoMarshal", "./test/mixbench/marshal.txt") } 78 benches["unmarshaler"] = func() { bench("./test/combos/both/", "ProtoUnmarshal", "./test/mixbench/unmarshaler.txt") } 79 benches["unmarshal"] = func() { bench("./test/", "ProtoUnmarshal", "./test/mixbench/unmarshal.txt") } 80 var ops []string 81 for k := range benches { 82 ops = append(ops, k) 83 } 84 sort.Strings(ops) 85 benches["all"] = benchall(ops) 86 ops = append(ops, "all") 87 flag.StringVar(&benchList, "benchlist", "all", fmt.Sprintf("List of benchmarks to run. Options: %v", ops)) 88 } 89 90 func benchall(ops []string) func() { 91 return func() { 92 for _, o := range ops { 93 benches[o]() 94 } 95 } 96 }