-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatshouse_test.go
155 lines (135 loc) · 4.08 KB
/
statshouse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2022 V Kontakte LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package statshouse_test
import (
"context"
"fmt"
"runtime"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/vkcom/statshouse-go"
)
func TestCountRace(t *testing.T) {
c := statshouse.NewClient(t.Logf, "udp", "" /* avoid sending anything */, "")
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 1000; j++ {
c.Count("test_stat", statshouse.Tags{1: "hello", 2: "world"}, float64(j))
}
}()
}
wg.Wait()
}
func TestBucketRelease(t *testing.T) {
var wg sync.WaitGroup
c := statshouse.NewClient(t.Logf, "udp", "" /* avoid sending anything */, "")
c.TrackBucketCount()
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second))
defer cancel()
for i := 1; i <= 10; i++ {
wg.Add(2)
go func(i int) {
defer wg.Done()
name := fmt.Sprintf("test_metric%d", i)
var tags statshouse.NamedTags
for j := 0; j < i; j++ {
tags = append(tags, [2]string{strconv.Itoa(j), fmt.Sprintf("name%d", j)})
}
n := 0
for ; ctx.Err() == nil; n++ {
c.MetricNamed(name, tags).Count(float64(i))
}
t.Logf("send # %d", n)
}(i)
go func(i int) {
defer wg.Done()
name := fmt.Sprintf("test_metric%d", i)
var tags statshouse.Tags
for j := 0; j < i; j++ {
tags[j] = strconv.Itoa(j)
}
n := 0
for ; ctx.Err() == nil; n++ {
c.Metric(name, tags).Count(float64(i))
}
t.Logf("send # %d", n)
}(i)
}
wg.Wait()
runtime.GC()
for i := 0; i < 10 && c.BucketCount() != 0; i++ {
time.Sleep(time.Second)
runtime.GC()
}
require.Zero(t, c.BucketCount())
}
func BenchmarkValue2(b *testing.B) {
c := statshouse.NewClient(b.Logf, "udp", "" /* avoid sending anything */, "")
defer c.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Value("test_stat", statshouse.Tags{1: "hello", 2: "world"}, float64(i))
}
}
func BenchmarkRawValue(b *testing.B) {
c := statshouse.NewClient(b.Logf, "udp", "" /* avoid sending anything */, "")
s := c.MetricRef("test_stat", statshouse.Tags{1: "hello", 2: "world"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Value(float64(i))
}
}
func BenchmarkCount4(b *testing.B) {
c := statshouse.NewClient(b.Logf, "udp", "" /* avoid sending anything */, "")
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Count("test_stat", statshouse.Tags{1: "hello", 2: "brave", 3: "new", 4: "world"}, float64(i))
}
}
func BenchmarkRawCount(b *testing.B) {
c := statshouse.NewClient(b.Logf, "udp", "" /* avoid sending anything */, "")
s := c.MetricRef("test_stat", statshouse.Tags{1: "hello", 2: "brave", 3: "new", 4: "world"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Count(float64(i))
}
}
func BenchmarkLabeledValue2(b *testing.B) {
c := statshouse.NewClient(b.Logf, "udp", "" /* avoid sending anything */, "")
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.NamedValue("test_stat", statshouse.NamedTags{{"hello", "world"}, {"world", "hello"}}, float64(i))
}
}
func BenchmarkRawLabeledValue(b *testing.B) {
c := statshouse.NewClient(b.Logf, "udp", "" /* avoid sending anything */, "")
s := c.MetricNamedRef("test_stat", statshouse.NamedTags{{"hello", "world"}, {"world", "hello"}})
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Value(float64(i))
}
}
func BenchmarkLabeledCount4(b *testing.B) {
c := statshouse.NewClient(b.Logf, "udp", "" /* avoid sending anything */, "")
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.NamedCount("test_stat", statshouse.NamedTags{{"hello", "world"}, {"world", "hello"}, {"hello1", "world"}, {"world1", "hello"}}, float64(i))
}
}
func BenchmarkRawLabeledCount(b *testing.B) {
c := statshouse.NewClient(b.Logf, "udp", "" /* avoid sending anything */, "")
s := c.MetricNamedRef("test_stat", statshouse.NamedTags{{"hello", "world"}, {"world", "hello"}, {"hello1", "world"}, {"world1", "hello"}})
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Count(float64(i))
}
}