// Call f() and verify that the correct STW metrics increment. If isGC is true,
// fn triggers a GC STW. Otherwise, fn triggers an other STW.
func testSchedPauseMetrics(t *testing.T, fn func(t *testing.T), isGC bool) {
- t.Helper()
-
m := []metrics.Sample{
{Name: "/sched/pauses/stopping/gc:seconds"},
{Name: "/sched/pauses/stopping/other:seconds"},
}
func TestSchedPauseMetrics(t *testing.T) {
- tests := []struct{
+ tests := []struct {
name string
isGC bool
fn func(t *testing.T)
{
name: "runtime.GC",
isGC: true,
- fn: func(t *testing.T) {
+ fn: func(t *testing.T) {
runtime.GC()
},
},
{
name: "runtime.GOMAXPROCS",
- fn: func(t *testing.T) {
+ fn: func(t *testing.T) {
if runtime.GOARCH == "wasm" {
t.Skip("GOMAXPROCS >1 not supported on wasm")
}
n := runtime.GOMAXPROCS(0)
defer runtime.GOMAXPROCS(n)
- runtime.GOMAXPROCS(n+1)
+ runtime.GOMAXPROCS(n + 1)
},
},
{
name: "runtime.GoroutineProfile",
- fn: func(t *testing.T) {
+ fn: func(t *testing.T) {
var s [1]runtime.StackRecord
runtime.GoroutineProfile(s[:])
},
},
{
name: "runtime.ReadMemStats",
- fn: func(t *testing.T) {
+ fn: func(t *testing.T) {
var mstats runtime.MemStats
runtime.ReadMemStats(&mstats)
},
},
{
name: "runtime.Stack",
- fn: func(t *testing.T) {
+ fn: func(t *testing.T) {
var b [64]byte
runtime.Stack(b[:], true)
},
},
{
name: "runtime/debug.WriteHeapDump",
- fn: func(t *testing.T) {
+ fn: func(t *testing.T) {
if runtime.GOOS == "js" {
t.Skip("WriteHeapDump not supported on js")
}
},
{
name: "runtime/trace.Start",
- fn: func(t *testing.T) {
+ fn: func(t *testing.T) {
if trace.IsEnabled() {
t.Skip("tracing already enabled")
}
},
}
+ // These tests count STW pauses, classified based on whether they're related
+ // to the GC or not. Disable automatic GC cycles during the test so we don't
+ // have an incidental GC pause when we're trying to observe only
+ // non-GC-related pauses. This is especially important for the
+ // runtime/trace.Start test, since (as of this writing) that will block
+ // until any active GC mark phase completes.
+ defer debug.SetGCPercent(debug.SetGCPercent(-1))
+ runtime.GC()
+
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
testSchedPauseMetrics(t, tc.fn, tc.isGC)