]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/pprof: eliminate arbitrary deadline in testCPUProfile
authorBryan C. Mills <bcmills@google.com>
Mon, 16 May 2022 15:41:12 +0000 (11:41 -0400)
committerGopher Robot <gobot@golang.org>
Mon, 16 May 2022 16:15:58 +0000 (16:15 +0000)
The testCPUProfile helper function iterates until the profile contains
enough samples. However, in general very slow builders may need longer
to complete tests, and may have less-responsive schedulers (leading to
longer durations required to collect profiles with enough samples).
To compensate, slower builders generally run tests with longer timeouts.

Since this test helper already dynamically scales the profile duration
based on the collected samples, allow it to continue to retry and
rescale until it would exceed the test's deadline.

Fixes #52656 (hopefully).

Change-Id: I4561e721927503f33a6d23336efa979bb9d3221f
Reviewed-on: https://go-review.googlesource.com/c/go/+/406614
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>

src/runtime/pprof/pprof_test.go

index eeb7b2758bd7598061824e068a9dcb10d917dcfc..faefd857f0f605627956cabd4d5997c07a5de377 100644 (file)
@@ -437,10 +437,14 @@ func testCPUProfile(t *testing.T, matches profileMatchFunc, f func(dur time.Dura
 
        broken := cpuProfilingBroken()
 
-       maxDuration := 5 * time.Second
-       if testing.Short() && broken {
-               // If it's expected to be broken, no point waiting around.
-               maxDuration /= 10
+       deadline, ok := t.Deadline()
+       if broken || !ok {
+               if broken && testing.Short() {
+                       // If it's expected to be broken, no point waiting around.
+                       deadline = time.Now().Add(1 * time.Second)
+               } else {
+                       deadline = time.Now().Add(10 * time.Second)
+               }
        }
 
        // If we're running a long test, start with a long duration
@@ -455,7 +459,7 @@ func testCPUProfile(t *testing.T, matches profileMatchFunc, f func(dur time.Dura
        // several others under go test std. If a test fails in a way
        // that could mean it just didn't run long enough, try with a
        // longer duration.
-       for duration <= maxDuration {
+       for {
                var prof bytes.Buffer
                if err := StartCPUProfile(&prof); err != nil {
                        t.Fatal(err)
@@ -468,9 +472,10 @@ func testCPUProfile(t *testing.T, matches profileMatchFunc, f func(dur time.Dura
                }
 
                duration *= 2
-               if duration <= maxDuration {
-                       t.Logf("retrying with %s duration", duration)
+               if time.Until(deadline) < duration {
+                       break
                }
+               t.Logf("retrying with %s duration", duration)
        }
 
        if broken {