runtime: disable automatic GC for STW metric tests

A follow-up to https://go.dev/cl/534161 -- calls to runtime/trace.Start
and Stop synchronize with the GC, waiting for any in-progress mark phase
to complete. Disable automatic GCs to quiet the system, so we can
observe only the test's intentional pauses.

Change-Id: I6f8106c42528f9bda9afec1c151119783bbc78dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/543075
Run-TryBot: Rhys Hiltner <rhys@justin.tv>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Auto-Submit: Rhys Hiltner <rhys@justin.tv>
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/src/runtime/metrics_test.go b/src/runtime/metrics_test.go
index d7f21f1..1e82897 100644
--- a/src/runtime/metrics_test.go
+++ b/src/runtime/metrics_test.go
@@ -780,8 +780,6 @@
 // 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"},
@@ -848,7 +846,7 @@
 }
 
 func TestSchedPauseMetrics(t *testing.T) {
-	tests := []struct{
+	tests := []struct {
 		name string
 		isGC bool
 		fn   func(t *testing.T)
@@ -856,13 +854,13 @@
 		{
 			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")
 				}
@@ -870,33 +868,33 @@
 				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")
 				}
@@ -912,7 +910,7 @@
 		},
 		{
 			name: "runtime/trace.Start",
-			fn:   func(t *testing.T) {
+			fn: func(t *testing.T) {
 				if trace.IsEnabled() {
 					t.Skip("tracing already enabled")
 				}
@@ -926,6 +924,15 @@
 		},
 	}
 
+	// 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)