...

Source file src/go.opencensus.io/stats/benchmark_test.go

Documentation: go.opencensus.io/stats

     1  // Copyright 2017, OpenCensus Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package stats_test
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.opencensus.io/stats"
    22  	"go.opencensus.io/stats/view"
    23  	_ "go.opencensus.io/stats/view" // enable collection
    24  	"go.opencensus.io/tag"
    25  )
    26  
    27  var m = makeMeasure()
    28  
    29  func BenchmarkRecord0(b *testing.B) {
    30  	ctx := context.Background()
    31  	b.ResetTimer()
    32  
    33  	for i := 0; i < b.N; i++ {
    34  		stats.Record(ctx)
    35  	}
    36  }
    37  
    38  func BenchmarkRecord1(b *testing.B) {
    39  	ctx := context.Background()
    40  	b.ResetTimer()
    41  
    42  	for i := 0; i < b.N; i++ {
    43  		stats.Record(ctx, m.M(1))
    44  	}
    45  }
    46  
    47  func BenchmarkRecord8(b *testing.B) {
    48  	ctx := context.Background()
    49  	b.ResetTimer()
    50  
    51  	for i := 0; i < b.N; i++ {
    52  		stats.Record(ctx, m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1))
    53  	}
    54  }
    55  
    56  func BenchmarkRecord8_WithRecorder(b *testing.B) {
    57  	ctx := context.Background()
    58  	meter := view.NewMeter()
    59  	meter.Start()
    60  	defer meter.Stop()
    61  	b.ResetTimer()
    62  
    63  	for i := 0; i < b.N; i++ {
    64  		// Note that this benchmark has one extra allocation for stats.WithRecorder.
    65  		// If you cache the recorder option, this benchmark should be equally fast as BenchmarkRecord8
    66  		stats.RecordWithOptions(ctx, stats.WithRecorder(meter), stats.WithMeasurements(m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1)))
    67  	}
    68  
    69  	b.StopTimer()
    70  }
    71  
    72  func BenchmarkRecord8_Parallel(b *testing.B) {
    73  	ctx := context.Background()
    74  	b.ResetTimer()
    75  
    76  	b.RunParallel(func(pb *testing.PB) {
    77  		for pb.Next() {
    78  			stats.Record(ctx, m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1))
    79  		}
    80  	})
    81  }
    82  
    83  func BenchmarkRecord8_8Tags(b *testing.B) {
    84  	ctx := context.Background()
    85  	key1 := tag.MustNewKey("key1")
    86  	key2 := tag.MustNewKey("key2")
    87  	key3 := tag.MustNewKey("key3")
    88  	key4 := tag.MustNewKey("key4")
    89  	key5 := tag.MustNewKey("key5")
    90  	key6 := tag.MustNewKey("key6")
    91  	key7 := tag.MustNewKey("key7")
    92  	key8 := tag.MustNewKey("key8")
    93  
    94  	tag.New(ctx,
    95  		tag.Insert(key1, "value"),
    96  		tag.Insert(key2, "value"),
    97  		tag.Insert(key3, "value"),
    98  		tag.Insert(key4, "value"),
    99  		tag.Insert(key5, "value"),
   100  		tag.Insert(key6, "value"),
   101  		tag.Insert(key7, "value"),
   102  		tag.Insert(key8, "value"),
   103  	)
   104  	b.ResetTimer()
   105  
   106  	for i := 0; i < b.N; i++ {
   107  		stats.Record(ctx, m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1), m.M(1))
   108  	}
   109  }
   110  
   111  func makeMeasure() *stats.Int64Measure {
   112  	m := stats.Int64("m", "test measure", "")
   113  	v := &view.View{
   114  		Measure:     m,
   115  		Aggregation: view.Sum(),
   116  	}
   117  	if err := view.Register(v); err != nil {
   118  		panic(err.Error())
   119  	}
   120  	return m
   121  }
   122  

View as plain text