Cloudflare workers: how do I measure execution time of my method?

performance
#1

I’m trying to measure how long does it take for worker to handle user request and couldn’t find API that could do it correctly.

Things I’ve tried so far:
console.time/console.timeEnd
performance.now
date.getTime()

Result either 0 or undefined. Please share recipe

#2

Since it is too quick it could be 0.

const requestStartTime = Date.now();
// your code
const executionTime = Date.now() - requestStartTime
1 Like
#3

Hi @natasignal,

We lock Date.now() and console.time() to the time of last I/O, and we don’t implement performance.now, so it is not directly possible to measure CPU timing from within a script. This is necessary in order to mitigate timing side channel attacks (e.g., Spectre).

This means that you can measure I/O times – how long it takes to fetch() a resource, for example – but not things like how long a CPU-bound function takes to execute.

Harris

3 Likes
#4

Is it possible that the server-timing header could be added? See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing

Currently I’m calculating this myself using the difference between Date.now() before and after, but this is not reliable like you say.

It’s hard to know if you‘re hitting the limit in your worker. Does the worker have a hard limit on execution time?

1 Like
#5

I feel like CPU time is the elephant in the room, if this cannot be provided it’s going to repeat and repeat in this forum forever. It’s ok if we can’t get it within the worker, but can we at least get an estimated CPU time in the response headers?

This way we can at least trial-and-error functions to find out what is consuming CPU-time.

closed #6

This topic was automatically closed after 30 days. New replies are no longer allowed.