-
Notifications
You must be signed in to change notification settings - Fork 55
/
http_test.go
114 lines (102 loc) · 2.62 KB
/
http_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
package main
import (
"bytes"
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"testing"
"time"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)
func BenchmarkReverseProxyHandler(b *testing.B) {
body := genRandomMetricsResponse(10000, 10)
testExporter := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
reader := bytes.NewReader(body.Bytes())
io.Copy(w, reader)
}))
defer testExporter.Close()
URL, _ := url.Parse(testExporter.URL)
verify := true
port, _ := strconv.ParseInt(URL.Port(), 0, 0)
modCfg := &moduleConfig{
Method: "http",
Timeout: 5 * time.Second,
HTTP: httpConfig{
Verify: &verify,
Scheme: URL.Scheme,
Address: URL.Hostname(),
Port: int(port),
Path: "/",
},
}
if err := checkModuleConfig("test", modCfg); err != nil {
b.Fatalf("Failed to check module config: %v", err)
}
cfg := &config{
Modules: map[string]*moduleConfig{
"test": modCfg,
},
}
req := httptest.NewRequest("GET", "/proxy?module=test", nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
rr := httptest.NewRecorder()
cfg.doProxy(rr, req)
if rr.Code != http.StatusOK {
b.Fatalf("Bad response status %d", rr.Code)
}
if len(rr.Body.Bytes()) <= 0 {
b.Fatal("Response body is absent")
}
}
}
// genRandomMetricsResponse generates http response body which contains random set of
// prometheus metrics. mf_num sets number of metric families in response which has
// metric names in format 'metric{random number}'. m_num controls number of metrics
// inside each metric family. Metrics inside metric families differ in values of
// label 'label'.
func genRandomMetricsResponse(mfNum int, mNum int) *bytes.Buffer {
rand.Seed(time.Now().UnixNano())
helpMsg := "help msg"
labelName := "label"
metricFamilies := make([]*dto.MetricFamily, mfNum)
metricType := dto.MetricType_GAUGE
for i := range metricFamilies {
metrics := make([]*dto.Metric, mNum)
for i := range metrics {
labelValue := fmt.Sprint(rand.Int63())
value := rand.Float64()
ts := time.Now().UnixNano()
metrics[i] = &dto.Metric{
Label: []*dto.LabelPair{
{
Name: &labelName,
Value: &labelValue,
},
},
Gauge: &dto.Gauge{
Value: &value,
},
TimestampMs: &ts,
}
}
metricName := fmt.Sprintf("metric%d", rand.Int63())
metricFamilies[i] = &dto.MetricFamily{
Name: &metricName,
Help: &helpMsg,
Type: &metricType,
Metric: metrics,
}
}
buf := &bytes.Buffer{}
enc := expfmt.NewEncoder(buf, expfmt.FmtText)
for _, mf := range metricFamilies {
enc.Encode(mf)
}
return buf
}