Skip to content

Commit 754ea20

Browse files
mikenomitchhyperlint-ai[bot]kodster28
authored
Adds DevTools docs (#16264)
* DevTools docs up * Apply suggestions from code review Fixes typos Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Code block tweaks * Proofreading tweaks * Adding links from limits page * Wording changes * fixing missed rebase * Slight cleanup --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> Co-authored-by: kodster28 <[email protected]>
1 parent 96f4c7e commit 754ea20

File tree

11 files changed

+232
-29
lines changed

11 files changed

+232
-29
lines changed

public/_redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,7 @@
12341234
/workers/about/routes/ /workers/configuration/routing/routes/ 301
12351235
/workers/platform/routes/ /workers/configuration/routing/ 301
12361236
/workers/about/tips/debugging/ /workers/observability/ 301
1237+
/workers/testing/debugging-tools/ /workers/observability/dev-tools/ 301
12371238
/workers/about/tips/signing-requests/ /workers/examples/signing-requests/ 301
12381239
/workers/about/using-cache/ /workers/reference/how-the-cache-works/ 301
12391240
/workers/learning/how-the-cache-works/ /workers/reference/how-the-cache-works/ 301
254 KB
Loading
106 KB
Loading
420 KB
Loading
203 KB
Loading
Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
---
2-
title: Debugging tools
32
pcx_content_type: concept
3+
title: Breakpoints
4+
description: Debug your local and deployed Workers using breakpoints.
45
sidebar:
56
order: 4
67
head: []
7-
description: Debug your local and deployed Workers using a variety of tools.
8-
98
---
109

11-
Being able to assess how your Workers are functioning at various points in the development cycle is vital to identifying the root causes of bugs or issues.
12-
13-
Cloudflare provides a variety of tools to help you debug your Workers.
14-
15-
## Chrome DevTools
16-
17-
Wrangler supports using [Chrome DevTools](https://developer.chrome.com/docs/devtools/) to view logs/sources, set breakpoints, and profile CPU/memory usage. To open a DevTools session connected to your Worker from any Chromium-based browser, run [`wrangler dev`](/workers/wrangler/commands/#dev) and press the <kbd>d</kbd> key in your terminal.
18-
1910
## Debug via breakpoints
2011

2112
As of Wrangler 3.9.0, you can debug via breakpoints in your Worker. Breakpoints provide the ability to review what is happening at a given point in the execution of your Worker. Breakpoint functionality exists in both DevTools and VS Code.
@@ -32,17 +23,17 @@ To setup VS Code for breakpoint debugging in your Worker project:
3223
```json
3324
{
3425
"configurations": [
35-
{
36-
"name": "Wrangler",
37-
"type": "node",
38-
"request": "attach",
39-
"port": 9229,
40-
"cwd": "/",
41-
"resolveSourceMapLocations": null,
42-
"attachExistingChildren": false,
43-
"autoAttachChildProcesses": false,
44-
"sourceMaps": true // works with or without this line
45-
}
26+
{
27+
"name": "Wrangler",
28+
"type": "node",
29+
"request": "attach",
30+
"port": 9229,
31+
"cwd": "/",
32+
"resolveSourceMapLocations": null,
33+
"attachExistingChildren": false,
34+
"autoAttachChildProcesses": false,
35+
"sourceMaps": true // works with or without this line
36+
}
4637
]
4738
}
4839
```
@@ -57,20 +48,16 @@ To setup VS Code for breakpoint debugging in your Worker project:
5748

5849
:::caution
5950

60-
6151
Breakpoint debugging in `wrangler dev` using `--remote` could extend Worker CPU time and incur additional costs since you are testing against actual resources that count against usage limits. It is recommended to use `wrangler dev` without the `--remote` option. This ensures you are developing locally.
6252

63-
6453
:::
6554

6655
:::note
6756

68-
6957
The `.vscode/launch.json` file only applies to a single workspace. If you prefer, you can add the above launch configuration to your User Settings (per the [official VS Code documentation](https://code.visualstudio.com/docs/editor/debugging#_global-launch-configuration)) to have it available for all your workspaces.
7058

71-
7259
:::
7360

7461
## Related resources
7562

76-
* [Local Development](/workers/testing/local-development/) - Develop your Workers and connected resources locally via Wrangler and [`workerd`](https://github.com/cloudflare/workerd), for a fast, accurate feedback loop.
63+
- [Local Development](/workers/testing/local-development/) - Develop your Workers and connected resources locally via Wrangler and [`workerd`](https://github.com/cloudflare/workerd), for a fast, accurate feedback loop.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
pcx_content_type: concept
3+
title: Profiling CPU usage
4+
weight: 4
5+
description: Learn how to profile CPU usage and ensure CPU-time per request stays under Workers limits
6+
---
7+
8+
If a Worker spends too much time performing CPU-intensive tasks, responses may be slow or the Worker might
9+
fail to startup due to [time limits](/workers/platform/limits/#worker-startup-time).
10+
11+
Profiling in DevTools can help you identify and fix code that uses too much CPU.
12+
13+
Measuring execution time of specific functions in production can be difficult because Workers
14+
[only increment timers on I/O](/workers/reference/security-model/#step-1-disallow-timers-and-multi-threading)
15+
for security purposes. However, measuring CPU execution times is possible in local development with DevTools.
16+
17+
## Taking a profile
18+
19+
To generate a CPU profile:
20+
21+
- Run `wrangler dev` to start your Worker
22+
- Hit <kbd>d</kbd> from your terminal to open DevTools
23+
- Click on the "Profiler" tab
24+
- Click `Start` to begin recording CPU usage
25+
- Send requests to your Worker from a new tab
26+
- Click `Stop`
27+
28+
You now have a CPU profile.
29+
30+
## An Example Profile
31+
32+
Let's look at an example to learn how to read a CPU profile. Imagine you have the following Worker:
33+
34+
```js title="index.js"
35+
const addNumbers = (body) => {
36+
for (let i = 0; i < 5000; ++i) {
37+
body = body + " " + i;
38+
}
39+
return body;
40+
};
41+
42+
const moreAddition = (body) => {
43+
for (let i = 5001; i < 15000; ++i) {
44+
body = body + " " + i;
45+
}
46+
return body;
47+
};
48+
49+
export default {
50+
async fetch(request, env, ctx) {
51+
let body = "Hello Profiler! - ";
52+
body = addNumbers(body);
53+
body = moreAddition(body);
54+
return new Response(body);
55+
},
56+
};
57+
```
58+
59+
You want to find which part of the code causes slow response times. How do you use DevTool profiling to identify the
60+
CPU-heavy code and fix the issue?
61+
62+
First, as mentioned above, you open DevTools by hitting <kbd>d</kbd> after running `wrangler dev`. Then, you
63+
navigate to the "Profiler" tab and take a profile by pressing `Start` and sending a request.
64+
65+
![CPU Profile](~/assets/images/workers/observability/profile.png)
66+
67+
The top chart in this image shows a timeline of the profile, and you can use it to zoom in on a specific request.
68+
69+
The chart below shows the CPU time used for operations run during the request. In this screenshot, you can see
70+
"fetch" time at the top and the subscomponents of fetch beneath, including the two functions `addNumbers` and
71+
`moreAdditions`. By hovering over each box, you get more information, and by clicking the box, you navigate
72+
to the function's source code.
73+
74+
Using this graph, you can answer the question of "what is taking CPU time?". The `addNumbers` function has
75+
a very small box, representing 0.3ms of CPU time. The `moreAdditions` box is larger, representing 2.2ms of CPU time.
76+
77+
Therefore, if you want to make response times faster, you need to optimize `moreAdditions`.
78+
79+
You can also change the visualization from ‘Chart’ to ‘Heavy (Bottom Up)’ for an alternative view.
80+
81+
![CPU Profile](~/assets/images/workers/observability/heavy.png)
82+
83+
This shows the relative times allocated to each function. At the top of the list, `moreAdditions` is clearly the
84+
slowest portion of your Worker. You can see that garbage collection also represents a large percentage of time, so
85+
memory optimization could be useful.
86+
87+
## Additional Resources
88+
89+
To learn more about how to use the CPU profiler, see [Google's documentation on Profiling the CPU in DevTools](https://developer.chrome.com/docs/devtools/performance/nodejs#profile).
90+
91+
To learn how to use DevTools to gain insight into memory, see the [Memory Usage Documentation](/workers/observability/dev-tools/memory-usage/).
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: DevTools
3+
weight: 1
4+
pcx_content_type: concept
5+
meta:
6+
description: Overview of interactive DevTools used to debug a variety of situations
7+
---
8+
9+
## Using DevTools
10+
11+
When running your Worker locally using `wrangler dev`, you automatically have access to [Cloudflare's implementation](https://github.com/cloudflare/workers-sdk/tree/main/packages/wrangler-devtools?cf_target_id=115890352C73E75FD7D837D0B8720E96) of [Chrome's DevTools](https://developer.chrome.com/docs/devtools/overview).
12+
DevTools help you debug and optimize your Workers.
13+
14+
:::note
15+
You may have experience using Chrome's DevTools for frontend development. Notably, Worker DevTools are used for backend code and _not_ client-side JavaScript.
16+
:::
17+
18+
## Opening DevTools
19+
20+
To get started, run your Worker in development mode with `wrangler dev`, then open the DevTools in the browser by hitting <kbd>d</kbd> from your terminal. Now when you access this worker locally, it can be debugged and profiled with this DevTools instance.
21+
22+
Alternatively, both the [Cloudflare Dashboard](https://dash.cloudflare.com/) and the [Worker's Playground](https://workers.cloudflare.com/playground) include DevTools in their UI.
23+
24+
## DevTool use cases
25+
26+
DevTools can be used in a variety of situations. For more information, see the documentation:
27+
28+
- [Debugging code via breakpoints and logging](/workers/observability/dev-tools/breakpoints/)
29+
- [Profiling CPU usage](/workers/observability/dev-tools/cpu-usage/)
30+
- [Addressing out of memory (OOM) errors](/workers/observability/dev-tools/memory-usage/)
31+
32+
## Related resources
33+
34+
- [Local development](/workers/testing/local-development/) - Develop your Workers and connected resources locally via Wrangler and workerd, for a fast, accurate feedback loop.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: Profiling Memory
3+
weight: 4
4+
pcx_content_type: concept
5+
meta:
6+
description: Learn how to profile Memory usage to avoid out-of-memory errors and optimize your Worker
7+
---
8+
9+
Understanding Worker memory usage can help you optimize performance, avoid Out of Memory (OOM) errors
10+
when hitting [Worker memory limits](/workers/platform/limits/#memory), and fix memory leaks.
11+
12+
You can profile memory usage with snapshots in DevTools. Memory snapshots let you view a summary of
13+
memory usage, see how much memory is allocated to different data types, and get details on specific
14+
objects in memory.
15+
16+
## Taking a snapshot
17+
18+
To generate a memory snapshot:
19+
20+
- Run `wrangler dev` to start your Worker
21+
- Hit <kbd>d</kbd> from your terminal to open DevTools
22+
- Click on the "Memory" tab
23+
- Send requests to your Worker to start allocating memory
24+
- Optionally include a debugger to make sure you can pause execution at the proper time
25+
- Click `Take snapshot`
26+
27+
You can now inspect Worker memory.
28+
29+
## An Example Snapshot
30+
31+
Let's look at an example to learn how to read a memory snapshot. Imagine you have the following Worker:
32+
33+
```js title="index.js"
34+
let responseText = "Hello world!";
35+
36+
export default {
37+
async fetch(request, env, ctx) {
38+
let now = new Date().toISOString();
39+
responseText = responseText + ` (Requested at: ${now})`;
40+
return new Response(responseText.slice(0, 53));
41+
},
42+
};
43+
```
44+
45+
While this code worked well initially, over time you notice slower responses and
46+
Out of Memory errors. Using DevTools, you can find out if this is a memory leak.
47+
48+
First, as mentioned above, you open DevTools by hitting <kbd>d</kbd> after running `wrangler dev`.
49+
Then, you navigate to the "Memory" tab.
50+
51+
Next, generate a large volume of traffic to the Worker by sending requests. You can do this with `curl` or by
52+
repeatedly reloading the browser. Note that other Workers may require more specific requests to reproduce
53+
a memory leak.
54+
55+
Then, click the "Take Snapshot" button and view the results.
56+
57+
First, navigate to "Statistics" in the dropdown to get a general sense of what takes up memory.
58+
59+
![Memory Statistics](~/assets/images/workers/observability/memory-stats.png)
60+
61+
Looking at these statistics, you can see that a lot of memory is dedicated to strings at 67 kB. This is
62+
likely the source of the memory leak. If you make more requests and take another snapshot, you would see
63+
this number grow.
64+
65+
![Memory Summary](~/assets/images/workers/observability/memory-summary.png)
66+
67+
The memory summary lists data types by the amount of memory they take up. When you click into "(string)", you can see
68+
a string that is far larger than the rest. The text shows that you are appending "Requested at" and a date repeatedly,
69+
inadvertently overwriting the global variable with an increasingly large string:
70+
71+
```js
72+
responseText = responseText + ` (Requested at: ${now})`;
73+
```
74+
75+
Using Memory Snapshotting in DevTools, you've identified the object and line of code causing the memory leak.
76+
You can now fix it with a small code change.
77+
78+
## Additional Resources
79+
80+
To learn more about how to use Memory Snapshotting, see [Google's documentation on Memory Heap Snapshots](https://developer.chrome.com/docs/devtools/memory-problems/heap-snapshots).
81+
82+
To learn how to use DevTools to gain insight into CPU usage, see the [CPU Profiling Documentation](/workers/observability/dev-tools/cpu-usage/).

src/content/docs/workers/platform/limits.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ Cloudflare updates the Workers runtime a few times per week. When this happens,
7676

7777
CPU time is the amount of time the CPU actually spends doing work, during a given request. Most Workers requests consume less than a millisecond of CPU time. It is rare to find normally operating Workers that exceed the CPU time limit.
7878

79-
<Render file="isolate-cpu-flexibility" /> <br />
79+
<Render file="isolate-cpu-flexibility" />
80+
81+
Using DevTools locally can help identify CPU intensive portions of your code. See the [CPU profiling with DevTools documentation](/workers/observability/dev-tools/cpu-usage/) to learn more.
8082

8183
You can also set a custom limit on the amount of CPU time that can be used during each invocation of your Worker. To do so, navigate to the Workers section in the Cloudflare dashboard. Select the specific Worker you wish to modify, then click on the "Settings" tab where you can adjust the CPU time limit.
8284

@@ -156,6 +158,8 @@ If a Worker processes a request that pushes the Worker over the 128 MB limit, th
156158

157159
Use the [TransformStream API](/workers/runtime-apis/streams/transformstream/) to stream responses if you are concerned about memory usage. This avoids loading an entire response into memory.
158160

161+
Using DevTools locally can help identify memory leaks in your code. See the [memory profiling with DevTools documentation](/workers/observability/dev-tools/memory-usage/) to learn more.
162+
159163
---
160164

161165
## Subrequests
@@ -264,6 +268,10 @@ A Worker must be able to be parsed and execute its global scope (top-level code
264268

265269
You can measure your Worker's startup time by deploying it to Cloudflare using [Wrangler](/workers/wrangler/). When you run `npx wrangler@latest deploy` or `npx wrangler@latest versions upload --experimental-versions`, Wrangler will output the startup time of your Worker in the command-line output, using the `startup_time_ms` field in the [Workers Script API](/api/operations/worker-script-upload-worker-module) or [Workers Versions API](/api/operations/worker-versions-upload-version#request-body).
266270

271+
If you are having trouble staying under this limit, consider [profiling using DevTools](/workers/observability/dev-tools/) locally to learn how to optimize your code.
272+
273+
<Render file="limits_increase" />
274+
267275
---
268276

269277
## Number of Workers

0 commit comments

Comments
 (0)