website/docs/guides/profile.mdx
import Image from '@site/src/components/Docs/Image';
Troubleshooting slow or unperformant tasks? Profile and diagnose them with ease!
:::caution
Profiling is only supported by node based tasks, and is not supported by tasks that are created
through package.json inference, or for packages that ship non-JavaScript code (like Rust or Go).
:::
CPU profiling helps you get a better understanding of which parts of your code require the most CPU time, and how your code is executed and optimized by Node.js. The profiler will measure code execution and activities performed by the engine itself, such as compilation, calls of system libraries, optimization, and garbage collection.
To record a CPU profile, pass --profile cpu to the moon run command. When
successful, the profile will be written to
.moon/cache/states/<project>/<task>/snapshot.cpuprofile.
$ moon run --profile cpu app:lint
CPU profiles can be reviewed and analyzed with Chrome developer tools using the following steps.
chrome://inspect.<Image src={require('./profile/cpu.png')} alt="DevTools Profiler - CPU" />
snapshot.cpuprofile that was
previously recorded. If successful, the snapshot will appear in the left
column.On macOS, press
command+shift+.to display hidden files and folders, to locate the.moonfolder.
<Image src={require('./profile/cpu-loaded.png')} alt="DevTools Profiler - CPU snapshot loaded" />
<Image src={require('./profile/cpu-selected.png')} alt="DevTools Profiler - CPU snapshot being analyzed through charts" />
Heap profiling lets you detect memory leaks, dynamic memory problems, and locate the fragments of code that caused them.
To record a heap profile, pass --profile heap to the moon run command. When
successful, the profile will be written to
.moon/cache/states/<project>/<task>/snapshot.heapprofile.
$ moon run --profile heap app:lint
Heap profiles can be reviewed and analyzed with Chrome developer tools using the following steps.
chrome://inspect.<Image src={require('./profile/heap.png')} alt="DevTools Profiler - Heap" />
snapshot.heapprofile that was
previously recorded. If successful, the snapshot will appear in the left
column.On macOS, press
command+shift+.to display hidden files and folders, to locate the.moonfolder.
<Image src={require('./profile/heap-loaded.png')} alt="DevTools Profiler - Heap snapshot loaded" />
<Image src={require('./profile/heap-selected.png')} alt="DevTools Profiler - Heap snapshot being analyzed through charts" />
Chrome DevTools provide 3 views for analyzing activities within a snapshot. Each view gives you a different perspective on these activities.
The Bottom up view is helpful if you encounter a heavy function and want to find out where it was called from.
<Image src={require('./profile/bottom-up.png')} alt="Bottom up profiler view" />
The Top down view works in a similar fashion to Bottom up, but displays functions starting from the top-level entry points. These are also known as root activities.
<Image src={require('./profile/top-down.png')} alt="Top down profiler view" />
DevTools represents main thread activity with a flame chart. The x-axis represents the recording over time. The y-axis represents the call stack. The events on top cause the events below it.
<Image src={require('./profile/flame-chart.png')} alt="Flame chart profiler view" />