Skip to content

Commit

Permalink
fix: distinguish idle from program time (#248)
Browse files Browse the repository at this point in the history
fix: distinguish idle from program time

Fixes: #38
  • Loading branch information
ofrobots authored and aalexand committed Jul 17, 2018
1 parent a4b7e6e commit 25f8a26
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ts/src/profilers/time-profiler.ts
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();
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
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

0 comments on commit 25f8a26

Please sign in to comment.