-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathstats_context_test.go
108 lines (86 loc) · 3.35 KB
/
stats_context_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
/*
Copyright 2018-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
package rest
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"slices"
"testing"
"time"
"github.com/couchbase/sync_gateway/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNetworkInterfaceStatsForHostnamePort(t *testing.T) {
_, err := networkInterfaceStatsForHostnamePort("127.0.0.1:4984")
assert.NoError(t, err, "Unexpected Error")
_, err = networkInterfaceStatsForHostnamePort("localhost:4984")
assert.NoError(t, err, "Unexpected Error")
_, err = networkInterfaceStatsForHostnamePort("0.0.0.0:4984")
assert.NoError(t, err, "Unexpected Error")
_, err = networkInterfaceStatsForHostnamePort(":4984")
assert.NoError(t, err, "Unexpected Error")
}
func TestDescriptionPopulation(t *testing.T) {
base.SkipPrometheusStatsRegistration = false
defer func() {
base.SkipPrometheusStatsRegistration = true
}()
rt := NewRestTester(t, nil)
defer rt.Close()
srv := httptest.NewServer(rt.TestMetricsHandler())
defer srv.Close()
// Ensure metrics endpoint is accessible and that db database has entries
resp, err := http.Get(srv.URL + "/_metrics")
require.NoError(t, err)
defer func() {
assert.NoError(t, resp.Body.Close())
}()
assert.Equal(t, http.StatusOK, resp.StatusCode)
bodyString, err := io.ReadAll(resp.Body)
require.NoError(t, err)
// assert on a HELP description
assert.Contains(t, string(bodyString), `HELP sgw_cache_high_seq_stable The highest contiguous sequence number that has been cached.`)
}
func TestMemoryProfile(t *testing.T) {
stats := statsContext{heapProfileCollectionThreshold: 1, heapProfileEnabled: true} // set to a very low value to ensure collection
outputDir := t.TempDir()
ctx := base.TestCtx(t)
// make sure go memory stats are set once
AddGoRuntimeStats()
// collect single profile
startTime := "01"
require.NoError(t, stats.collectMemoryProfile(ctx, outputDir, startTime))
require.Equal(t, []string{"pprof_heap_high_01.pb.gz"}, getFilenames(t, outputDir))
// collect enough profiles to trigger rotation
expectedFilenames := make([]string, 0, 10)
for i := 2; i < 12; i++ {
// reset heap profile time time to ensure we create new heap profiles
stats.lastHeapProfile = time.Time{}
expectedFilenames = append(expectedFilenames, fmt.Sprintf("pprof_heap_high_%02d.pb.gz", i))
require.NoError(t, stats.collectMemoryProfile(ctx, outputDir, fmt.Sprintf("%02d", i)))
}
require.ElementsMatch(t, expectedFilenames, getFilenames(t, outputDir))
// ask for another profile, this should not be collected. Since the last profile collection (11) set lastHeapProfile, we do not collect another profile for 5 minutes.
require.NoError(t, stats.collectMemoryProfile(ctx, outputDir, "12"))
require.Equal(t, expectedFilenames, getFilenames(t, outputDir))
}
func getFilenames(t *testing.T, dir string) []string {
files, err := os.ReadDir(dir)
require.NoError(t, err)
filenames := make([]string, 0, len(files))
for _, file := range files {
filenames = append(filenames, file.Name())
}
slices.Sort(filenames)
return filenames
}