...

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

Documentation: go.opencensus.io/stats/view

     1  // Copyright 2018, 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  
    16  package view
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"testing"
    22  	"time"
    23  
    24  	"go.opencensus.io/stats"
    25  	"go.opencensus.io/tag"
    26  )
    27  
    28  var (
    29  	m  = stats.Float64("m", "", "")
    30  	k1 = tag.MustNewKey("k1")
    31  	k2 = tag.MustNewKey("k2")
    32  	k3 = tag.MustNewKey("k3")
    33  	k4 = tag.MustNewKey("k4")
    34  	k5 = tag.MustNewKey("k5")
    35  	k6 = tag.MustNewKey("k6")
    36  	k7 = tag.MustNewKey("k7")
    37  	k8 = tag.MustNewKey("k8")
    38  
    39  	view = &View{
    40  		Measure:     m,
    41  		Aggregation: Distribution(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
    42  		TagKeys:     []tag.Key{k1, k2},
    43  	}
    44  )
    45  
    46  // BenchmarkRecordReqCommand benchmarks calling the internal recording machinery
    47  // directly.
    48  func BenchmarkRecordReqCommand(b *testing.B) {
    49  	w := NewMeter().(*worker)
    50  
    51  	register := &registerViewReq{views: []*View{view}, err: make(chan error, 1)}
    52  	register.handleCommand(w)
    53  	if err := <-register.err; err != nil {
    54  		b.Fatal(err)
    55  	}
    56  
    57  	ctxs := prepareContexts(10)
    58  
    59  	b.ReportAllocs()
    60  	b.ResetTimer()
    61  
    62  	for i := 0; i < b.N; i++ {
    63  		record := &recordReq{
    64  			ms: []stats.Measurement{
    65  				m.M(1),
    66  				m.M(1),
    67  				m.M(1),
    68  				m.M(1),
    69  				m.M(1),
    70  				m.M(1),
    71  				m.M(1),
    72  				m.M(1),
    73  			},
    74  			tm: tag.FromContext(ctxs[i%len(ctxs)]),
    75  			t:  time.Now(),
    76  		}
    77  		record.handleCommand(w)
    78  	}
    79  }
    80  
    81  func BenchmarkRecordViaStats(b *testing.B) {
    82  
    83  	meter := NewMeter()
    84  	meter.Start()
    85  	defer meter.Stop()
    86  	meter.Register(view)
    87  	defer meter.Unregister(view)
    88  
    89  	ctxs := prepareContexts(10)
    90  	rec := stats.WithRecorder(meter)
    91  	b.ReportAllocs()
    92  	b.ResetTimer()
    93  
    94  	for i := 0; i < b.N; i++ {
    95  		stats.RecordWithOptions(ctxs[i%len(ctxs)], rec, 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)))
    96  	}
    97  
    98  }
    99  
   100  func prepareContexts(tagCount int) []context.Context {
   101  	ctxs := make([]context.Context, 0, tagCount)
   102  	for i := 0; i < tagCount; i++ {
   103  		ctx, _ := tag.New(context.Background(),
   104  			tag.Upsert(k1, fmt.Sprintf("v%d", i)),
   105  			tag.Upsert(k2, fmt.Sprintf("v%d", i)),
   106  			tag.Upsert(k3, fmt.Sprintf("v%d", i)),
   107  			tag.Upsert(k4, fmt.Sprintf("v%d", i)),
   108  			tag.Upsert(k5, fmt.Sprintf("v%d", i)),
   109  			tag.Upsert(k6, fmt.Sprintf("v%d", i)),
   110  			tag.Upsert(k7, fmt.Sprintf("v%d", i)),
   111  			tag.Upsert(k8, fmt.Sprintf("v%d", i)),
   112  		)
   113  		ctxs = append(ctxs, ctx)
   114  	}
   115  
   116  	return ctxs
   117  }
   118  

View as plain text