/
render_handler_test.go
125 lines (114 loc) · 2.7 KB
/
render_handler_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
package http
import (
"net/http"
"testing"
"time"
"github.com/go-graphite/carbonapi/cmd/carbonapi/config"
"github.com/lomik/zapwriter"
)
func BenchmarkResponseCacheComputeKey(b *testing.B) {
var from int64 = 1628876560
var until int64 = 1628876620
var maxDataPoints int64 = 1024
noNullPoints := false
template := "test"
targets := []string{
"test.metric.*.cpu.load_avg",
"test.metric.*.memory.free",
}
format := "json"
for i := 0; i < b.N; i++ {
_ = responseCacheComputeKey(from, until, targets, format, maxDataPoints, noNullPoints, template)
}
}
func BenchmarkBackendCacheComputeKey(b *testing.B) {
from := "1628876560"
until := "1628876620"
targets := []string{
"test.metric.*.cpu.load_avg",
"test.metric.*.memory.free",
}
for i := 0; i < b.N; i++ {
_ = backendCacheComputeKey(from, until, targets, 0, true)
}
}
func BenchmarkBackendCacheComputeKeyAbs(b *testing.B) {
var from int64 = 1628876560
var until int64 = 1628876620
targets := []string{
"test.metric.*.cpu.load_avg",
"test.metric.*.memory.free",
}
for i := 0; i < b.N; i++ {
_ = backendCacheComputeKeyAbs(from, until, targets, 0, true)
}
}
func Test_getCacheTimeout(t *testing.T) {
cacheConfig := config.CacheConfig{
ShortTimeoutSec: 60,
DefaultTimeoutSec: 300,
ShortDuration: 3 * time.Hour,
ShortUntilOffsetSec: 120,
}
now := int64(1636985018)
tests := []struct {
name string
now time.Time
from int64
until int64
want int32
}{
{
name: "short: from = now - 600, until = now - 120",
now: time.Unix(now, 0),
from: now - 600,
until: now - 120,
want: 60,
},
{
name: "short: from = now - 10800",
now: time.Unix(now, 0),
from: now - 10800,
until: now,
want: 60,
},
{
name: "short: from = now - 10810, until = now - 120",
now: time.Unix(now, 0),
from: now - 10800,
until: now - 120,
want: 60,
},
{
name: "short: from = now - 10800, until now - 121",
now: time.Unix(now, 0),
from: now - 10800,
until: now - 121,
want: 300,
},
{
name: "default: from = now - 10801",
now: time.Unix(now, 0),
from: now - 10801,
until: now,
want: 300,
},
{
name: "short: from = now - 122, until = now - 121",
now: time.Unix(now, 0),
from: now - 122,
until: now - 121,
want: 300,
},
}
logger := zapwriter.Logger("test")
r, _ := http.NewRequest("GET", "http://127.0.0.1/render", nil)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
duration := time.Second * time.Duration(tt.until-tt.from)
if got := getCacheTimeout(logger, r, tt.now.Unix(), tt.until, duration, &cacheConfig); got != tt.want {
t.Errorf("getCacheTimeout() = %v, want %v", got, tt.want)
}
})
}
}