Skip to content

Commit cf8b21d

Browse files
ShelbyZdkundel
authored andcommitted
feat: add estimated execution time to function run (#81)
* feat: add estimated execution time to function run - use process.hrtime.bigint() to track start/stop - debug log estimated execution time in milliseconds Not sure if requiring debug() is the correct way to report this information. We could also consider displaying a warning/error message if the estimated duration execeeds the defined limit. Another approach to capturing timing would be to use perf_hooks to setup an observer and add measurements. The observer could process the measurements (logging) and disconnect. * feat: add estimated execution time to function run - fix trying to use process.hrtime.bigint() on nodeJS 6.x * feat: add estimated execution time to function run - moving debug statement before error handling/return
1 parent ce63952 commit cf8b21d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/runtime/route.ts

+15
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,26 @@ export function functionToRoute(
131131
debug('Event for %s: %o', req.path, event);
132132
const context = constructContext(config);
133133
debug('Context for %s: %p', req.path, context);
134+
let run_timings: {
135+
start: [number, number];
136+
end: [number, number];
137+
} = {
138+
start: [0, 0],
139+
end: [0, 0],
140+
};
134141

135142
const callback: ServerlessCallback = function callback(
136143
err,
137144
responseObject?
138145
) {
146+
run_timings.end = process.hrtime();
139147
debug('Function execution %s finished', req.path);
148+
debug(
149+
`(Estimated) Total Execution Time: ${(run_timings.end[0] * 1e9 +
150+
run_timings.end[1] -
151+
(run_timings.start[0] * 1e9 + run_timings.start[1])) /
152+
1e6}ms`
153+
);
140154
if (err) {
141155
handleError(err, req, res, functionFilePath);
142156
return;
@@ -146,6 +160,7 @@ export function functionToRoute(
146160

147161
debug('Calling function for %s', req.path);
148162
try {
163+
run_timings.start = process.hrtime();
149164
fn(context, event, callback);
150165
} catch (err) {
151166
callback(err);

0 commit comments

Comments
 (0)