github.com/uber-go/tally/v4@v4.1.17/stats_benchmark_test.go (about) 1 // Copyright (c) 2021 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package tally 22 23 import ( 24 "testing" 25 "time" 26 ) 27 28 func BenchmarkCounterInc(b *testing.B) { 29 c := &counter{} 30 for n := 0; n < b.N; n++ { 31 c.Inc(1) 32 } 33 } 34 35 func BenchmarkReportCounterNoData(b *testing.B) { 36 c := &counter{} 37 for n := 0; n < b.N; n++ { 38 c.report("foo", nil, NullStatsReporter) 39 } 40 } 41 42 func BenchmarkReportCounterWithData(b *testing.B) { 43 c := &counter{} 44 for n := 0; n < b.N; n++ { 45 c.Inc(1) 46 c.report("foo", nil, NullStatsReporter) 47 } 48 } 49 50 func BenchmarkGaugeSet(b *testing.B) { 51 g := &gauge{} 52 for n := 0; n < b.N; n++ { 53 g.Update(42) 54 } 55 } 56 57 func BenchmarkReportGaugeNoData(b *testing.B) { 58 g := &gauge{} 59 for n := 0; n < b.N; n++ { 60 g.report("bar", nil, NullStatsReporter) 61 } 62 } 63 64 func BenchmarkReportGaugeWithData(b *testing.B) { 65 g := &gauge{} 66 for n := 0; n < b.N; n++ { 67 g.Update(73) 68 g.report("bar", nil, NullStatsReporter) 69 } 70 } 71 72 func BenchmarkTimerStopwatch(b *testing.B) { 73 t := &timer{ 74 name: "bencher", 75 tags: nil, 76 reporter: NullStatsReporter, 77 } 78 for n := 0; n < b.N; n++ { 79 t.Start().Stop() // start and stop 80 } 81 } 82 83 func BenchmarkTimerReport(b *testing.B) { 84 t := &timer{ 85 name: "bencher", 86 tags: nil, 87 reporter: NullStatsReporter, 88 } 89 for n := 0; n < b.N; n++ { 90 start := time.Now() 91 t.Record(time.Since(start)) 92 } 93 }