Skip to content

Commit e4ddf8a

Browse files
authored
perf: only time the fetch event when debug logging is enabled (#873)
1 parent 6af7626 commit e4ddf8a

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

runtime/fastly/builtins/fetch-event.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,8 @@ bool FetchEvent::server_get(JSContext *cx, unsigned argc, JS::Value *vp) {
425425
return true;
426426
}
427427

428-
void dispatch_fetch_event(HandleObject event, double *total_compute) {
428+
void dispatch_fetch_event(HandleObject event) {
429429
MOZ_ASSERT(FetchEvent::is_instance(event));
430-
auto pre_handler = system_clock::now();
431-
432430
RootedValue result(ENGINE->cx());
433431
RootedValue event_val(ENGINE->cx(), JS::ObjectValue(*event));
434432
HandleValueArray argsv = HandleValueArray(event_val);
@@ -449,11 +447,14 @@ void dispatch_fetch_event(HandleObject event, double *total_compute) {
449447
}
450448

451449
FetchEvent::stop_dispatching(event);
450+
}
452451

452+
void dispatch_fetch_event(HandleObject event, double *total_compute) {
453+
auto pre_handler = system_clock::now();
454+
dispatch_fetch_event(event);
453455
double diff = duration_cast<microseconds>(system_clock::now() - pre_handler).count();
454456
*total_compute += diff;
455-
if (ENGINE->debug_logging_enabled())
456-
printf("Request handler took %fms\n", diff / 1000);
457+
printf("Request handler took %fms\n", diff / 1000);
457458
}
458459

459460
JSObject *FetchEvent::prepare_downstream_request(JSContext *cx) {

runtime/fastly/builtins/fetch-event.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class ServerInfo final : public builtins::BuiltinNoConstructor<ServerInfo> {
5656
static JSObject *create(JSContext *cx);
5757
};
5858

59+
void dispatch_fetch_event(HandleObject event);
5960
void dispatch_fetch_event(HandleObject event, double *total_compute);
6061

6162
class FetchEvent final : public builtins::BuiltinNoConstructor<FetchEvent> {

runtime/fastly/handler.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ void handle_incoming(host_api::Request req) {
2727
std::chrono::high_resolution_clock::now());
2828

2929
double total_compute = 0;
30-
auto start = system_clock::now();
30+
std::chrono::system_clock::time_point start;
31+
if (ENGINE->debug_logging_enabled()) {
32+
start = system_clock::now();
33+
}
3134

3235
__wasilibc_initialize_environ();
3336

@@ -43,7 +46,11 @@ void handle_incoming(host_api::Request req) {
4346
return;
4447
}
4548

46-
fastly::fetch_event::dispatch_fetch_event(fetch_event, &total_compute);
49+
if (ENGINE->debug_logging_enabled()) {
50+
fastly::fetch_event::dispatch_fetch_event(fetch_event, &total_compute);
51+
} else {
52+
fastly::fetch_event::dispatch_fetch_event(fetch_event);
53+
}
4754

4855
// Loop until no more resolved promises or backend requests are pending.
4956
if (ENGINE->debug_logging_enabled()) {
@@ -77,9 +84,9 @@ void handle_incoming(host_api::Request req) {
7784
return;
7885
}
7986

80-
auto end = system_clock::now();
81-
double diff = duration_cast<microseconds>(end - start).count();
8287
if (ENGINE->debug_logging_enabled()) {
88+
auto end = system_clock::now();
89+
double diff = duration_cast<microseconds>(end - start).count();
8390
printf("Done. Total request processing time: %fms. Total compute time: %fms\n", diff / 1000,
8491
total_compute / 1000);
8592
}

0 commit comments

Comments
 (0)