From 70d0456a16b01cc6f9d90bb7b7a6a00dfec32c15 Mon Sep 17 00:00:00 2001 From: Yumin Xia Date: Fri, 4 Sep 2026 12:52:27 +0000 Subject: [PATCH] Make CPU profile node test deterministic --- cpuprofilenode_test.go | 10 ++++++---- cpuprofiler_test.go | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/cpuprofilenode_test.go b/cpuprofilenode_test.go index d9a817d4e..71ad9b187 100644 --- a/cpuprofilenode_test.go +++ b/cpuprofilenode_test.go @@ -12,8 +12,8 @@ import ( func TestCPUProfileNode(t *testing.T) { // CPU profiles are sampling based. Running this test alongside the rest of - // the package can starve the profiler long enough for short-lived call - // paths to be absent from the profile. + // the package can starve the profiler, so keep it serial and give every + // expected call path one continuous sampling window. ctx := v8.NewContext(nil) iso := ctx.Isolate() @@ -32,9 +32,11 @@ func TestCPUProfileNode(t *testing.T) { fatalIf(t, err) fn, err := val.AsFunction() fatalIf(t, err) - timeout, err := v8.NewValue(iso, int32(1000)) + timeout, err := v8.NewValue(iso, int32(0)) fatalIf(t, err) - _, err = fn.Call(ctx.Global(), timeout) + pathDuration, err := v8.NewValue(iso, int32(250)) + fatalIf(t, err) + _, err = fn.Call(ctx.Global(), timeout, pathDuration) fatalIf(t, err) cpuProfile := cpuProfiler.StopProfiling(title) diff --git a/cpuprofiler_test.go b/cpuprofiler_test.go index 776fb2ad9..47c7c8d99 100644 --- a/cpuprofiler_test.go +++ b/cpuprofiler_test.go @@ -88,21 +88,21 @@ const profileScript = `function loop(timeout) { } } } -function delay() { try { loop(10); } catch(e) { } } -function bar() { delay(); } -function baz() { delay(); } -function foo() { +function delay(timeout = 10) { try { loop(timeout); } catch(e) { } } +function bar(timeout) { delay(timeout); } +function baz(timeout) { delay(timeout); } +function foo(delayTimeout = 10) { try { - delay(); - bar(); - delay(); - baz(); + delay(delayTimeout); + bar(delayTimeout); + delay(delayTimeout); + baz(delayTimeout); } catch (e) { } } -function start(timeout) { +function start(timeout, delayTimeout = 10) { var start = Date.now(); do { - foo(); + foo(delayTimeout); var duration = Date.now() - start; } while (duration < timeout); return duration;