github.com/SkycoinProject/gomobile@v0.0.0-20190312151609-d3739f865fa6/bind/testdata/benchmark/benchmark.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package benchmark contains benchmarking bound functions for internal use. 6 package benchmark 7 8 import ( 9 "log" 10 "time" 11 ) 12 13 type Benchmarks interface { 14 // It seems to be much faster to call a native function from Java 15 // when there is already a native call earlier in the stack. 16 17 // Run runs a named benchmark from a different thread, with 18 // no native call prior in the stack. 19 Run(name string, n int) 20 // RunDirect runs a named benchmark directly, with the native 21 // context from the call itself. 22 RunDirect(name string, n int) 23 24 // Callbacks for Go benchmarks 25 NewI() I 26 Noargs() 27 Onearg(_ int) 28 Oneret() int 29 Ref(_ I) 30 Manyargs(_, _, _, _, _, _, _, _, _, _ int) 31 String(_ string) 32 StringRetShort() string 33 StringRetLong() string 34 Slice(_ []byte) 35 } 36 37 type ( 38 I interface { 39 F() 40 } 41 42 AnI struct { 43 } 44 ) 45 46 func (_ *AnI) F() { 47 } 48 49 func NewI() I { 50 return new(AnI) 51 } 52 53 func runBenchmark(name string, f func(n int)) { 54 // Run once for warmup 55 f(1) 56 n := 1000 57 var dt time.Duration 58 minDuration := 1 * time.Second 59 for dt < minDuration { 60 n *= 2 61 t0 := time.Now() 62 f(n) 63 dt = time.Since(t0) 64 } 65 log.Printf("Benchmark%s %d %d ns/op\n", name, n, dt.Nanoseconds()/int64(n)) 66 } 67 68 func runGoBenchmark(name string, f func()) { 69 runBenchmark("Go"+name, func(n int) { 70 for i := 0; i < n; i++ { 71 f() 72 } 73 }) 74 runBenchmark("Go"+name+"Direct", func(n int) { 75 done := make(chan struct{}) 76 go func() { 77 for i := 0; i < n; i++ { 78 f() 79 } 80 close(done) 81 }() 82 <-done 83 }) 84 } 85 86 func RunBenchmarks(b Benchmarks) { 87 names := []string{ 88 "Empty", 89 "Noargs", 90 "Onearg", 91 "Oneret", 92 "Manyargs", 93 "Refforeign", 94 "Refgo", 95 "StringShort", 96 "StringLong", 97 "StringShortUnicode", 98 "StringLongUnicode", 99 "StringRetShort", 100 "StringRetLong", 101 "SliceShort", 102 "SliceLong", 103 } 104 for _, name := range names { 105 runBenchmark("Foreign"+name, func(n int) { 106 b.Run(name, n) 107 }) 108 runBenchmark("Foreign"+name+"Direct", func(n int) { 109 b.RunDirect(name, n) 110 }) 111 } 112 runGoBenchmark("Empty", func() {}) 113 runGoBenchmark("Noarg", func() { b.Noargs() }) 114 runGoBenchmark("Onearg", func() { b.Onearg(0) }) 115 runGoBenchmark("Oneret", func() { b.Oneret() }) 116 foreignRef := b.NewI() 117 runGoBenchmark("Refforeign", func() { b.Ref(foreignRef) }) 118 goRef := NewI() 119 runGoBenchmark("Refgo", func() { b.Ref(goRef) }) 120 runGoBenchmark("Manyargs", func() { b.Manyargs(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) }) 121 runGoBenchmark("StringShort", func() { b.String(ShortString) }) 122 runGoBenchmark("StringLong", func() { b.String(LongString) }) 123 runGoBenchmark("StringShortUnicode", func() { b.String(ShortStringUnicode) }) 124 runGoBenchmark("StringLongUnicode", func() { b.String(LongStringUnicode) }) 125 runGoBenchmark("StringRetShort", func() { b.StringRetShort() }) 126 runGoBenchmark("StringRetLong", func() { b.StringRetLong() }) 127 runGoBenchmark("SliceShort", func() { b.Slice(ShortSlice) }) 128 runGoBenchmark("SliceLong", func() { b.Slice(LongSlice) }) 129 } 130 131 func Noargs() { 132 } 133 134 func Onearg(_ int) { 135 } 136 137 func Manyargs(_, _, _, _, _, _, _, _, _, _ int) { 138 } 139 140 func Oneret() int { 141 return 0 142 } 143 144 func String(_ string) { 145 } 146 147 func StringRetShort() string { 148 return ShortString 149 } 150 151 func StringRetLong() string { 152 return LongString 153 } 154 155 func Slice(_ []byte) { 156 } 157 158 func Ref(_ I) { 159 } 160 161 var ( 162 ShortSlice = make([]byte, 10) 163 LongSlice = make([]byte, 100000) 164 ) 165 166 const ( 167 ShortString = "Hello, World!" 168 LongString = "Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!, World!" 169 ShortStringUnicode = "Hello, 世界!" 170 LongStringUnicode = "Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界! Hello, 世界!, 世界!" 171 )