Author: https://medium.com/@vincentvallet
Description: How to run a CPU profiling with Node.js on your production in real-time and without interruption of service.
Date: 2019-10-18T17:23:34.816Z

# Node.js and CPU profiling on production (in real-time without downtime)

Vincent Vallet
Oct 18, 2019 · 8 min read

# Why CPU monitoring is important?

I work at Voodoo, a French company that creates mobile video games. We have a lot of challenges with performance, availability, and scalability because of the insane amount of traffic our infrastructure supports (billions of events/requests per day). In this setting, every metric is important and gives us a lot of information about the state of our system.

When working with Node.js one of the most critical resources to monitor is the CPU. Most of the time, when working on a low traffic API or project we don't realize how many simple lines of code can have a huge impact on CPU. On the other hand, when traffic increases, a simple mistake can cost dearly.

For memory, constant monitoring is the best practice to track the worst developer nightmare a.k.a memory leak.

Memory leak in action

> Stay focused on the CPU!

Basic CPU monitoring

# CPU profiling: what's the difference with CPU monitoring?

Basically, for Node.js, CPU profiling is nothing more than collecting data about functions which are CPU consuming.

## Add arguments to Node.js

Node.js provides a way to collect data about CPU with two command lines.

    node --prof app.js

Output of — prof

- It needs to restart the application to launch a CPU profiling

- It is NOT suited for a production environment

CPU profiling before optimization

How can we identify an issue?

A CPU profiling should be read like this: The wider is the block the more it consumes CPU. So we are looking for the widest blocks.

CPU profiling after optimization

What do we observe? Blocks are a lot smaller! It means that functions consume less CPU individually.

# Conclusion

CPU profiling is a must-have for every Node.js application running on production.