MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestMemoryProfile

Function TestMemoryProfile

rest/stats_context_test.go:70–97  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

68}
69
70func TestMemoryProfile(t *testing.T) {
71 stats := statsContext{heapProfileCollectionThreshold: 1, heapProfileEnabled: true} // set to a very low value to ensure collection
72
73 outputDir := t.TempDir()
74 ctx := base.TestCtx(t)
75
76 // make sure go memory stats are set once
77 AddGoRuntimeStats()
78
79 // collect single profile
80 startTime := "01"
81 require.NoError(t, stats.collectMemoryProfile(ctx, outputDir, startTime))
82 require.Equal(t, []string{"pprof_heap_high_01.pb.gz"}, getFilenames(t, outputDir))
83
84 // collect enough profiles to trigger rotation
85 expectedFilenames := make([]string, 0, 10)
86 for i := 2; i < 12; i++ {
87 // reset heap profile time time to ensure we create new heap profiles
88 stats.lastHeapProfile = time.Time{}
89 expectedFilenames = append(expectedFilenames, fmt.Sprintf("pprof_heap_high_%02d.pb.gz", i))
90 require.NoError(t, stats.collectMemoryProfile(ctx, outputDir, fmt.Sprintf("%02d", i)))
91 }
92 require.ElementsMatch(t, expectedFilenames, getFilenames(t, outputDir))
93
94 // 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.
95 require.NoError(t, stats.collectMemoryProfile(ctx, outputDir, "12"))
96 require.Equal(t, expectedFilenames, getFilenames(t, outputDir))
97}
98
99func getFilenames(t *testing.T, dir string) []string {
100 files, err := os.ReadDir(dir)

Callers

nothing calls this directly

Calls 5

collectMemoryProfileMethod · 0.95
TestCtxFunction · 0.92
AddGoRuntimeStatsFunction · 0.85
getFilenamesFunction · 0.85
EqualMethod · 0.45

Tested by

no test coverage detected