EdgeWorkers Code Profiler

The EdgeWorkers Code Profiler helps you gain insight about the CPU performance of your EdgeWorkers code. You can view the profiling information in a V8 compatible profiling tool to see which section of your code is consuming the most CPU timing resources.

👍

For more information about how to use the profiling information to improve your code, read the Code profiler tutorial.

Prerequisites

Before you can use the EdgeWorkers Code Profiler you need to:

Use the VS Code extension to profile your code

Once you've completed the prerequisites the EdgeWorkers Code Profiler will appear in the bottom panel of the EdgeWorkers VS Code extension.

📘

Profiling is currently limited to the Akamai staging network.

  1. To profile your code, enter the URL where your EdgeWorkers function is configured to execute.

Code ProfilerCode Profiler

  1. Select an event handler to profile.

    The EdgeWorkers Code Profiler operates on a per event handler basis. Event handler results are limited to one event handler per request.

  2. You can optionally specify a location and a name for the profiler results file.

  3. You can also add request headers.

  4. Click the Run Profiler button to get a profile of the JavaScript code.

Code Profiler outputCode Profiler output

  1. Click on the “flame” icon in the top right corner of the profiler tool to view the EdgeWorkers execution details in a flamegraph. Flamegraphs illustrate the stack trace and the amount of time spent in each stack frame.

Code Profiler flame graphCode Profiler flame graph

Use the CLI to profile your code

If you can’t use of the VSCode extension or if you need more flexibility you can use the CLI to profile your code. To do this, you need to generate a secret key that is used in the profiling request.

📘

Profiling is currently limited to the Akamai staging network.

  1. Use this EdgeWorkers CLI command to generate a secret key:
$ akamai edgeworkers secret
  1. Here’s an example of a secret key (this key is an example and cannot be used in your request).

    Fef77a483a6dd85b45a6c5092f1c178a6eaf21e56a3b69195e33f53070eec669

  2. Add a user-defined variable to your property. See User-defined variables for more information.

  3. Enter the secret key you created in step 1 into the Initial Value column of the user-defined variable.

  4. Select the Sensitive Security Setting to prevent exposing the key.

    Secret keySecret key

  5. Save and activate your property.

Request code profiling information

Follow these steps to generate an authentication token and add code profiling details to your requests. You can also use these steps to re-generate an expired token.

👍

You can also use the EdgeWorkers API to generate a secret key.

  1. Use this EdgeWorkers CLI command to generate an authentication token. In this example we set the token expiry to 60 minutes for the www.example.com hostname.
akamai edgeworkers auth --expiry 60 www.example.com
  1. Here’s an example of a response.
Creating auth token ... /
------------------------------------------------------------------------------------------------------------------------------------------------
Add the following request header to your requests to get additional trace information.
Akamai-EW-Trace: st=1601393394~exp=1601395194~acl=/*~hmac=89ec982c9098536d82ad7c24232578a4fdee88739e7c75c9c371be66b6fe614b
------------------------------------------------------------------------------------------------------------------------------------------------

📘

You need to provide the EdgeWorkers CLI with API access to the hostname in the request. Without this access the command will fail to provide valid code profiling information. For more information refer to the Get Started with APIs documentation.

  1. Active your EdgeWorkers code bundle.

  2. Choose an event handler to profile:

    • x-ew-code-profile-onclientrequest: on
    • x-ew-code-profile-onoriginrequest: on
    • x-ew-code-profile-onoriginresponse: on
    • x-ew-code-profile-onclientresponse: on
    • x-ew-code-profile-responseprovider: on

    If multiple headers are included in the request, the one that executes first is used. For example, if onClientRequest and onOriginResponse are included in the request only onClientRequest is included in the profiling request.

  3. Issue an HTTP request with the secure token and an event handler.

'https://www.example.com/' --resolve www.example.com:443:23.193.6.69 -H 'akamai-ew-trace: Fef77a483a6dd85b45a6c5092f1c178a6eaf21e56a3b69195e33f53070eec669'-H 'x-ew-code-profile-onclientresponse: on' > newprofile.cpuprofile
  1. The JSON profile data is returned as the HTTP response body.
{"nodes":[{"id":1,"callFrame":{"functionName":"(root)","scriptId":"0","url":"","lineNumber":-1,"columnNumber":-1},"hitCount":0,"children":[2,3]},{"id":2,"callFrame":{"functionName":"(program)","scriptId":"0","url":"","lineNumber":-1,"columnNumber":-1},"hitCount":5},{"id":3,"callFrame":{"functionName":"onClientRequest","scriptId":"3","url":"main.js","lineNumber":5,"columnNumber":31},"hitCount":1,"children":[4,5,6,7],"positionTicks":[{"line":25,"ticks":1}]},{"id":4,"callFrame":{"functionName":"Cookies","scriptId":"4","url":"cookies","lineNumber":43,"columnNumber":20},"hitCount":1,"positionTicks":[{"line":46,"ticks":1}]},{"id":5,"callFrame":{"functionName":"checkLanguage","scriptId":"3","url":"main.js","lineNumber":45,"columnNumber":22},"hitCount":1,"positionTicks":[{"line":54,"ticks":1}]},{"id":6,"callFrame":{"functionName":"checkRedirect","scriptId":"3","url":"main.js","lineNumber":68,"columnNumber":22},"hitCount":2,"positionTicks":[{"line":86,"ticks":1},{"line":72,"ticks":1}]},{"id":7,"callFrame":{"functionName":"SetCookie","scriptId":"4","url":"cookies","lineNumber":265,"columnNumber":22},"hitCount":1,"positionTicks":[{"line":280,"ticks":1}]}],"startTime":6399655041506,"endTime":6399655043547,"samples":[2,2,2,4,5,6,6,7,3,2,2,2],"timeDeltas":[1373,89,54,65,57,5,62,70,50,58,18,59]}
  1. You can import the response into a compatible JavaScript profiling tool. For example, you can view the profiling details in the Performance section of the Chrome DevTools.

FAQ

Review these commonly-asked questions about the EdgeWorkers Code Profiler.

How do I use the EdgeWorkers Code Profiler?

The easiest way to use the EdgeWorkers Code Profiler is to install the Akamai EdgeWorkers Toolkit. When you set up this Visual Studio Code extension, you also need to set up the EdgeWorkers CLI and create an .edgerc file with Akamai API Client credentials.

What profiling capabilities are available?

This release of the EdgeWorkers Code Profiler supports CPU profiling. The next release will cover additional functionality.

Can I profile code on the Akamai staging and production networks?

Profiling is only supported on the Akamai staging network.

Can I load the profiling data in Chrome DevTools?

Yes. We recommend that you use the Chrome DevTools to help analyze your profiling data.

Can I use Sandbox to run the EdgeWorkers Code Profiler?

Yes. You can use Akamai Sandbox to run the code profiler through a sandbox URL.

Which IDEs support the EdgeWorkers Code Profiler?

Currently the EdgeWorkers Code Profiler supports the VS Code extension. Support for IntelliJ is planned for a future release.

Why do some very short running functions not appear in the output CPU profile?

The EdgeWorkers Code Profiler leverages the V8 profiler that takes samples from a given interval. Because of this, some functions may not appear in the profile results. A future release will include the ability to control the sample interval. This update will allow you to tune the sample down to see short running functions at the expense of a larger output file.