Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: distinguish idle from program time #248

Merged
merged 6 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion ts/src/profilers/time-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ export class TimeProfiler {
* @param durationMillis - time in milliseconds for which to collect profile.
*/
async profile(durationMillis: number): Promise<perftools.profiles.IProfile> {
// Node.js contains an undocumented API for reporting idle status to V8.
// This lets the profiler distinguish idle time from time spent in native
// code. Ideally this should be default behavior. Until then, use the
// undocumented API.
// See https://github.com/nodejs/node/issues/19009#issuecomment-403161559.
// tslint:disable-next-line no-any
(process as any)._startProfilerIdleNotifier();

This comment was marked as spam.

This comment was marked as spam.

const runName = 'stackdriver-profiler-' + Date.now() + '-' + Math.random();
profiler.startProfiling(runName);
await delay(durationMillis);
const result = profiler.stopProfiling(runName);
return serializeTimeProfile(result, this.intervalMicros);
// tslint:disable-next-line no-any
(process as any)._stopProfilerIdleNotifier();
const profile = serializeTimeProfile(result, this.intervalMicros);
return profile;
}
}
11 changes: 11 additions & 0 deletions ts/test/test-time-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ const v8TimeProfiler = require('bindings')('time_profiler');

describe('TimeProfiler', () => {
describe('profile', () => {
it('should detect idle time', async () => {
const durationMillis = 500;
const intervalMicros = 1000;
const profiler = new TimeProfiler(intervalMicros);
const profile = await profiler.profile(durationMillis);
assert.ok(profile.stringTable);
assert.notStrictEqual(profile.stringTable!.indexOf('(idle)'), -1);
});
});

describe('profile (w/ stubs)', () => {
const sinonStubs: sinon.SinonStub[] = new Array();
before(() => {
sinonStubs.push(sinon.stub(v8TimeProfiler, 'startProfiling'));
Expand Down