forked from trpc-ecosystem/go-metrics-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus_test.go
108 lines (98 loc) · 1.88 KB
/
prometheus_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
package prometheus
import (
"fmt"
"io/ioutil"
"net/http"
"sync"
"testing"
"time"
"gopkg.in/yaml.v3"
)
var (
testCfg = &Config{
IP: "127.0.0.1",
Port: 9090,
Path: "/metrics",
}
once sync.Once
wg sync.WaitGroup
)
func setup(t *testing.T) {
cfg := &yaml.Node{}
p := &Plugin{}
_ = p.Setup(pluginName, cfg)
wg.Add(1)
go func(w *sync.WaitGroup) {
w.Done()
once.Do(
func() {
// Waiting for a concurrent process to start
err := initMetrics(testCfg.IP, testCfg.Port, testCfg.Path)
if err != nil {
t.Error(err)
return
}
})
}(&wg)
}
func getMetrics(t *testing.T) string {
time.Sleep(time.Second)
wg.Wait()
resp, err := http.Get(fmt.Sprintf("http://%s:%d%s", testCfg.IP, testCfg.Port, testCfg.Path))
if err != nil {
t.Fatal(err)
return ""
}
if resp.StatusCode != 200 {
t.Fatal(resp.StatusCode)
return ""
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
return ""
}
return string(body)
}
func TestInitMetrics(t *testing.T) {
setup(t)
t.Log(getMetrics(t))
}
func TestConvertSpecialChars(t *testing.T) {
in := "trpc.Chinese Indicators"
out := convertSpecialChars(in)
t.Log(out)
if !checkMetricsValid(out) {
t.Fatal()
}
}
func TestConvertSpecialCharsWithCache(t *testing.T) {
in := "trpc.Chinese Indicators"
out := convertSpecialCharsWithCache(in)
out1 := convertSpecialCharsWithCache(in)
t.Log(out)
t.Log(out1)
if !checkMetricsValid(out) || out != out1 {
t.Fatal()
}
}
func BenchmarkConvertSpecialChars(b *testing.B) {
in := "trpc.Chinese Indicators"
var out string
for n := 0; n < b.N; n++ {
out = convertSpecialChars(in)
}
if !checkMetricsValid(out) {
b.Fatal()
}
}
func BenchmarkConvertSpecialCharsWithCache(b *testing.B) {
in := "trpc.Chinese Indicators"
var out string
for n := 0; n < b.N; n++ {
out = convertSpecialCharsWithCache(in)
}
if !checkMetricsValid(out) {
b.Fatal()
}
}