1
2
3
4
5
6
7
8
9
10
11
12
13
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
47
48 func BenchmarkRecordReqCommand(b *testing.B) {
49 w := NewMeter().(*worker)
50
51 register := ®isterViewReq{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