Normal profiling of node.js applications

Foreword


One of the stumbling blocks when developing on node.js is more complicated debugging compared to other modern languages. Due to the asynchronous structure of the code in a large application, it becomes difficult to find a memory leak or a place for intensive processor use without specialized utilities. At different times, profiling tools were already created for node.js, but most of them were either simply not convenient enough or were no longer supported by developers.

Search


For a long time, I managed with conservative debugging methods in the form of periodically outputting the amount of used memory and the execution time of critical sections of code to the console, but the time came when the need for a high-quality tool got very urgent.

The first thing I decided to see was the node-inspector recovering , which after switching to node.js 0.6.x ceased to support CPU and Heap profiling. It turned out that in the new version of node-inspector idle profiling was completely excluded and now it's just a debugger. Rummaging a bit in the code of the old version, I still managed to get the profiling of the CPU and Heap on node 0.8.x, but this solution was not ideal. To get it out of the “crafts” state, it would be necessary to replace the outdated interface of the WebKit console with the modern one, rewriting a decent amount of code and fixing some performance problems. In general, the solution based on the WebKit console seems very flexible to me, so I quit this venture and continued the search.

Here it is


After trying a few more different options, I came across the https://nodetime.com service . This is a tool of a completely different level, which is much more suitable for the role of the “same” ideal tool. The service consists of two parts:
  • backend - collects detailed statistics about the application;
  • frontend is the service itself, which displays these statistics in a human-readable, beautiful way.

In addition to the most necessary CPU and Heap profiling tools, nodetime contains a set of metrics that greatly simplify development. Very pleased with the simplicity and accessibility of using the service. To get started, simply connect the nodetime module to the project.

In addition to collecting and displaying real-time statistics, nodetime provides the ability to save and view statistics for specific periods of time, as well as configure notifications for certain events (for example: memory leak).

I want more


Of course, the nodetime service is very convenient, but far from always there is the possibility of remote debugging of the application. For many, the issue of confidentiality of data collected by the profiler is very acute, and the possibility of debugging without connecting to a network is not unnecessary.

Because backend nodetime is licensed under MIT, for which many thanks to its author , I decided to create an interface based on it that could be used locally. Having spent a week and a half of free evenings on the analysis of the protocol and implementation of the interface, I got a stable version, which is not a shame to show people. Meet ...

Look


To start the demo:
Here we have launched an example of a chat implementation .
And here you can see the profiler.

Note: the demo is running on a weak VPS, a big request is not to arrange hardcore so that everyone can watch.

The source code for the project is located on GitHub . You can add a module to your application using npm .

npm install look

For the module to work, just connect it to the application and, if desired, specify the port and interface on which the profiler will work. By default, the profiler starts on port 5959 and listens on the '0.0.0.0' interface.

A simple example:

require('look').start(3131);

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(8080, '0.0.0.0');
console.log('Server running at http://0.0.0.0:8080/');

After starting the application, the profiler will be available on port 3131 of your server.

The interface is responsible for a small application based on connect , which runs in a separate thread so as not to affect the operation of the main application.

Conclusion


This is the first stable version of look that implements the most necessary functionality. As needed, look will improve, there is room to grow. Hope this tool helps you write your node.js applications better.