github.com/uber-go/tally/v4@v4.1.17/m3/reporter_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 m3
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  	"time"
    27  
    28  	tally "github.com/uber-go/tally/v4"
    29  )
    30  
    31  func BenchmarkNewMetric(b *testing.B) {
    32  	r, _ := NewReporter(Options{
    33  		HostPorts:  []string{"127.0.0.1:9052"},
    34  		Service:    "test-service",
    35  		CommonTags: defaultCommonTags,
    36  	})
    37  	defer r.Close()
    38  	benchReporter := r.(*reporter)
    39  	b.ResetTimer()
    40  
    41  	for n := 0; n < b.N; n++ {
    42  		benchReporter.newMetric("foo", nil, counterType)
    43  	}
    44  }
    45  
    46  func BenchmarkEmitMetrics(b *testing.B) {
    47  	r, _ := NewReporter(Options{
    48  		HostPorts:  []string{"127.0.0.1:9052"},
    49  		Service:    "test-service",
    50  		CommonTags: defaultCommonTags,
    51  	})
    52  	defer r.Close()
    53  
    54  	benchReporter := r.(*reporter)
    55  
    56  	counters := make([]tally.CachedCount, 100)
    57  	for i := range counters {
    58  		counters[i] = r.AllocateCounter(fmt.Sprintf("foo-%v", i), nil /* tags */)
    59  	}
    60  
    61  	for n := 0; n < b.N; n++ {
    62  		for _, c := range counters {
    63  			c.ReportCount(1)
    64  		}
    65  
    66  		benchReporter.Flush()
    67  	}
    68  }
    69  
    70  func BenchmarkCalulateSize(b *testing.B) {
    71  	r, _ := NewReporter(Options{
    72  		HostPorts:  []string{"127.0.0.1:9052"},
    73  		Service:    "test-service",
    74  		CommonTags: defaultCommonTags,
    75  	})
    76  	defer r.Close()
    77  	benchReporter := r.(*reporter)
    78  
    79  	met := benchReporter.newMetric("foo", map[string]string{"domain": "foo"}, counterType)
    80  	met.Value.Count = 123456
    81  
    82  	b.ResetTimer()
    83  
    84  	for n := 0; n < b.N; n++ {
    85  		benchReporter.calculateSize(met)
    86  	}
    87  }
    88  
    89  func BenchmarkTimer(b *testing.B) {
    90  	r, _ := NewReporter(Options{
    91  		HostPorts:    []string{"127.0.0.1:9052"},
    92  		Service:      "test-service",
    93  		CommonTags:   defaultCommonTags,
    94  		MaxQueueSize: DefaultMaxQueueSize,
    95  	})
    96  
    97  	defer r.Close()
    98  
    99  	benchReporter := r.(*reporter)
   100  
   101  	go func() {
   102  		// Blindly consume metrics
   103  		for range benchReporter.metCh {
   104  			// nop
   105  		}
   106  	}()
   107  
   108  	timer := benchReporter.AllocateTimer("foo", nil)
   109  
   110  	b.ResetTimer()
   111  
   112  	for n := 0; n < b.N; n++ {
   113  		timer.ReportTimer(time.Duration(n) * time.Millisecond)
   114  	}
   115  
   116  	b.StopTimer()
   117  }