| 1 | // Copyright (c) 2012 The Go Authors. All rights reserved. |
| 2 | // |
| 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are |
| 5 | // met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above |
| 10 | // copyright notice, this list of conditions and the following disclaimer |
| 11 | // in the documentation and/or other materials provided with the |
| 12 | // distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 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 checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/ |
| 30 | |
| 31 | import ( |
| 32 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 33 | "runtimescip-go gomod github.com/golang/go/src go1.13 runtime/" |
| 34 | "timescip-go gomod github.com/golang/go/src go1.13 time/" |
| 35 | ) |
| 36 | |
| 37 | var memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats. runtimescip-go gomod github.com/golang/go/src go1.13 runtime/.MemStatsscip-go gomod github.com/golang/go/src go1.13 runtime/MemStats# |
| 38 | |
| 39 | // testingB is a type passed to Benchmark functions to manage benchmark |
| 40 | // timing and to specify the number of iterations to run. |
| 41 | type timerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer# struct { |
| 42 | startscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#start. timescip-go gomod github.com/golang/go/src go1.13 time/.Timescip-go gomod github.com/golang/go/src go1.13 time/Time# // Time test or benchmark started |
| 43 | durationscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#duration. timescip-go gomod github.com/golang/go/src go1.13 time/.Durationscip-go gomod github.com/golang/go/src go1.13 time/Duration# |
| 44 | Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N. int |
| 45 | bytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#bytes. int64 |
| 46 | timerOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#timerOn. bool |
| 47 | benchTimescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#benchTime. timescip-go gomod github.com/golang/go/src go1.13 time/.Durationscip-go gomod github.com/golang/go/src go1.13 time/Duration# |
| 48 | // The initial states of memStats.Mallocs and memStats.TotalAlloc. |
| 49 | startAllocsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#startAllocs. uint64 |
| 50 | startBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#startBytes. uint64 |
| 51 | // The net total of this test after being run. |
| 52 | netAllocsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#netAllocs. uint64 |
| 53 | netBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#netBytes. uint64 |
| 54 | } |
| 55 | |
| 56 | // StartTimer starts timing a test. This function is called automatically |
| 57 | // before a benchmark starts, but it can also used to resume timing after |
| 58 | // a call to StopTimer. |
| 59 | func (c *Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) StartTimerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#StartTimer().() { |
| 60 | if !c.timerOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#timerOn. { |
| 61 | c.startscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#start. = timescip-go gomod github.com/golang/go/src go1.13 time/.Nowscip-go gomod github.com/golang/go/src go1.13 time/Now().() |
| 62 | c.timerOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#timerOn. = true |
| 63 | |
| 64 | runtimescip-go gomod github.com/golang/go/src go1.13 runtime/.ReadMemStatsscip-go gomod github.com/golang/go/src go1.13 runtime/ReadMemStats().(&memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats.) |
| 65 | c.startAllocsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#startAllocs. = memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats..Mallocsscip-go gomod github.com/golang/go/src go1.13 runtime/MemStats#Mallocs. |
| 66 | c.startBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#startBytes. = memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats..TotalAllocscip-go gomod github.com/golang/go/src go1.13 runtime/MemStats#TotalAlloc. |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // StopTimer stops timing a test. This can be used to pause the timer |
| 71 | // while performing complex initialization that you don't |
| 72 | // want to measure. |
| 73 | func (c *Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) StopTimerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#StopTimer().() { |
| 74 | if c.timerOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#timerOn. { |
| 75 | c.durationscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#duration. += timescip-go gomod github.com/golang/go/src go1.13 time/.Nowscip-go gomod github.com/golang/go/src go1.13 time/Now().().Subscip-go gomod github.com/golang/go/src go1.13 time/Time#Sub().(c.startscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#start.) |
| 76 | c.timerOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#timerOn. = false |
| 77 | runtimescip-go gomod github.com/golang/go/src go1.13 runtime/.ReadMemStatsscip-go gomod github.com/golang/go/src go1.13 runtime/ReadMemStats().(&memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats.) |
| 78 | c.netAllocsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#netAllocs. += memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats..Mallocsscip-go gomod github.com/golang/go/src go1.13 runtime/MemStats#Mallocs. - c.startAllocsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#startAllocs. |
| 79 | c.netBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#netBytes. += memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats..TotalAllocscip-go gomod github.com/golang/go/src go1.13 runtime/MemStats#TotalAlloc. - c.startBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#startBytes. |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // ResetTimer sets the elapsed benchmark time to zero. |
| 84 | // It does not affect whether the timer is running. |
| 85 | func (c *Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) ResetTimerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#ResetTimer().() { |
| 86 | if c.timerOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#timerOn. { |
| 87 | c.startscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#start. = timescip-go gomod github.com/golang/go/src go1.13 time/.Nowscip-go gomod github.com/golang/go/src go1.13 time/Now().() |
| 88 | runtimescip-go gomod github.com/golang/go/src go1.13 runtime/.ReadMemStatsscip-go gomod github.com/golang/go/src go1.13 runtime/ReadMemStats().(&memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats.) |
| 89 | c.startAllocsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#startAllocs. = memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats..Mallocsscip-go gomod github.com/golang/go/src go1.13 runtime/MemStats#Mallocs. |
| 90 | c.startBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#startBytes. = memStatsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/memStats..TotalAllocscip-go gomod github.com/golang/go/src go1.13 runtime/MemStats#TotalAlloc. |
| 91 | } |
| 92 | c.durationscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#duration. = 0 |
| 93 | c.netAllocsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#netAllocs. = 0 |
| 94 | c.netBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#netBytes. = 0 |
| 95 | } |
| 96 | |
| 97 | // SetBytes informs the number of bytes that the benchmark processes |
| 98 | // on each iteration. If this is called in a benchmark it will also |
| 99 | // report MB/s. |
| 100 | func (c *Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) SetBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#SetBytes().(n int64) { |
| 101 | c.bytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#bytes. = n |
| 102 | } |
| 103 | |
| 104 | func (c *Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) nsPerOpscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#nsPerOp().() int64 { |
| 105 | if c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N. <= 0 { |
| 106 | return 0 |
| 107 | } |
| 108 | return c.durationscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#duration..Nanosecondsscip-go gomod github.com/golang/go/src go1.13 time/Duration#Nanoseconds().() / int64(c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N.) |
| 109 | } |
| 110 | |
| 111 | func (c *Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) mbPerSecscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#mbPerSec().() float64 { |
| 112 | if c.bytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#bytes. <= 0 || c.durationscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#duration. <= 0 || c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N. <= 0 { |
| 113 | return 0 |
| 114 | } |
| 115 | return (float64(c.bytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#bytes.) * float64(c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N.) / 1e6) / c.durationscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#duration..Secondsscip-go gomod github.com/golang/go/src go1.13 time/Duration#Seconds().() |
| 116 | } |
| 117 | |
| 118 | func (c *Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) timerStringscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#timerString().() string { |
| 119 | if c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N. <= 0 { |
| 120 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("%3.3fs", float64(c.durationscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#duration..Nanosecondsscip-go gomod github.com/golang/go/src go1.13 time/Duration#Nanoseconds().())/1e9) |
| 121 | } |
| 122 | mbs := c.mbPerSecscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#mbPerSec().() |
| 123 | mb := "" |
| 124 | if mbs != 0 { |
| 125 | mb = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("\t%7.2f MB/s", mbs) |
| 126 | } |
| 127 | nsop := c.nsPerOpscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#nsPerOp().() |
| 128 | ns := fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("%10d ns/op", nsop) |
| 129 | if c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N. > 0 && nsop < 100 { |
| 130 | // The format specifiers here make sure that |
| 131 | // the ones digits line up for all three possible formats. |
| 132 | if nsop < 10 { |
| 133 | ns = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("%13.2f ns/op", float64(c.durationscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#duration..Nanosecondsscip-go gomod github.com/golang/go/src go1.13 time/Duration#Nanoseconds().())/float64(c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N.)) |
| 134 | } else { |
| 135 | ns = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("%12.1f ns/op", float64(c.durationscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#duration..Nanosecondsscip-go gomod github.com/golang/go/src go1.13 time/Duration#Nanoseconds().())/float64(c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N.)) |
| 136 | } |
| 137 | } |
| 138 | memStats := "" |
| 139 | if c.benchMemscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#benchMem. { |
| 140 | allocedBytes := fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("%8d B/op", int64(c.netBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#netBytes.)/int64(c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N.)) |
| 141 | allocs := fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("%8d allocs/op", int64(c.netAllocsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#netAllocs.)/int64(c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N.)) |
| 142 | memStats = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("\t%s\t%s", allocedBytes, allocs) |
| 143 | } |
| 144 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("%8d\t%s%s%s", c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N., ns, mb, memStats) |
| 145 | } |
| 146 | |
| 147 | func minscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/min().(x, y int) int { |
| 148 | if x > y { |
| 149 | return y |
| 150 | } |
| 151 | return x |
| 152 | } |
| 153 | |
| 154 | func maxscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/max().(x, y int) int { |
| 155 | if x < y { |
| 156 | return y |
| 157 | } |
| 158 | return x |
| 159 | } |
| 160 | |
| 161 | // roundDown10 rounds a number down to the nearest power of 10. |
| 162 | func roundDown10scip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/roundDown10().(n int) int { |
| 163 | var tens = 0 |
| 164 | // tens = floor(log_10(n)) |
| 165 | for n > 10 { |
| 166 | n = n / 10 |
| 167 | tens++ |
| 168 | } |
| 169 | // result = 10^tens |
| 170 | result := 1 |
| 171 | for i := 0; i < tens; i++ { |
| 172 | result *= 10 |
| 173 | } |
| 174 | return result |
| 175 | } |
| 176 | |
| 177 | // roundUp rounds x up to a number of the form [1eX, 2eX, 5eX]. |
| 178 | func roundUpscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/roundUp().(n int) int { |
| 179 | base := roundDown10scip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/roundDown10().(n) |
| 180 | if n < (2 * base) { |
| 181 | return 2 * base |
| 182 | } |
| 183 | if n < (5 * base) { |
| 184 | return 5 * base |
| 185 | } |
| 186 | return 10 * base |
| 187 | } |