Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
feat: measure handler performance with perf_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Jan 2, 2020
1 parent 38585cf commit 0d372e8
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/runtime-engine/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { EnvPropertyParser } from './lib/parser';
import { DebugLogger } from './lib/debug';
import { BaseLoggerFactory } from './lib/loggerFactory';
import { fileExists, getHandlerMeta, getHandlerMethod } from './util';
import { performance } from 'perf_hooks';

export class ServerlessBaseRuntime extends EventEmitter implements Runtime {
propertyParser: PropertyParser<string>;
Expand Down Expand Up @@ -126,6 +127,7 @@ export class ServerlessBaseRuntime extends EventEmitter implements Runtime {
async ready() {
await this.handlerInvokerWrapper('beforeReadyHandler', [this]);
// TODO 增加健康检查
this.measureMarksOnReady();
}

async close() {
Expand Down Expand Up @@ -230,12 +232,26 @@ export class ServerlessBaseRuntime extends EventEmitter implements Runtime {
}

protected async handlerInvokerWrapper(handlerKey: string, args?) {
performance.mark(`${handlerKey}:start`);
if (this.handlerStore.has(handlerKey)) {
const handlers = this.handlerStore.get(handlerKey);
this.debugLogger.log(`${handlerKey} exec, task = ${handlers.length}`);
for (const handler of handlers) {
await handler.apply(this, args);
}
}
performance.mark(`${handlerKey}:end`);
}

private measureMarksOnReady() {
[
'beforeRuntimeStartHandler',
'afterRuntimeStartHandler',
'beforeFunctionStartHandler',
'afterFunctionStartHandler',
'beforeReadyHandler',
].forEach(it => {
performance.measure(`${it}:measure`, `${it}:start`, `${it}:end`);
});
}
}

0 comments on commit 0d372e8

Please sign in to comment.