Skip to content

Commit 424937f

Browse files
authored
fix(profiling) sample timestamps need to be in seconds (#12563)
1 parent 847c05a commit 424937f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

packages/profiling-node/bindings/cpu_profiler.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ CreateFrameNode(const napi_env &env, const v8::CpuProfileNode &node,
546546
};
547547

548548
napi_value CreateSample(const napi_env &env, const enum ProfileFormat format,
549-
const uint32_t stack_id, const int64_t sample_timestamp,
549+
const uint32_t stack_id,
550+
const int64_t sample_timestamp_ns,
550551
const double chunk_timestamp,
551552
const uint32_t thread_id) {
552553
napi_value js_node;
@@ -564,7 +565,7 @@ napi_value CreateSample(const napi_env &env, const enum ProfileFormat format,
564565
switch (format) {
565566
case ProfileFormat::kFormatThread: {
566567
napi_value timestamp;
567-
napi_create_int64(env, sample_timestamp, &timestamp);
568+
napi_create_int64(env, sample_timestamp_ns, &timestamp);
568569
napi_set_named_property(env, js_node, "elapsed_since_start_ns", timestamp);
569570
} break;
570571
case ProfileFormat::kFormatChunk: {
@@ -643,7 +644,8 @@ static void GetSamples(const napi_env &env, const v8::CpuProfile *profile,
643644
uint64_t sample_offset_from_profile_start_ms =
644645
(sample_timestamp_us - profile_start_time_us) * 1e-3;
645646
double seconds_since_start =
646-
profile_start_timestamp_ms + sample_offset_from_profile_start_ms;
647+
(profile_start_timestamp_ms + sample_offset_from_profile_start_ms) *
648+
1e-3;
647649

648650
napi_value sample = nullptr;
649651
sample = CreateSample(env, format, stack_index, sample_timestamp_ns,

packages/profiling-node/test/cpu_profiler.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ describe('Profiler bindings', () => {
233233
throw new Error(`Sample ${JSON.stringify(sample)} has no timestamp`);
234234
}
235235
expect(sample.timestamp).toBeDefined();
236+
// No older than a minute and not in the future. Timestamp is in seconds so convert to ms
237+
// as the constructor expectes ms.
238+
expect(new Date((sample.timestamp as number) * 1e3).getTime()).toBeGreaterThan(Date.now() - 60 * 1e3);
239+
expect(new Date((sample.timestamp as number) * 1e3).getTime()).toBeLessThanOrEqual(Date.now());
236240
}
237241
});
238242

0 commit comments

Comments
 (0)