1
2
3
4
5
6
7
8
9
10
11
12
13
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"
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
65
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