Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling Node.JS (code) execution time in Linux

Tags:

I am looking for a method to do precise Node.JS profiling of script execution times on Linux.

There are interesting projects like the NodeTime.com Performance Profiler, but this profiles the timing of I/O httprequests and such, not execution time of lines of code.

I am looking for a way to figure out exactly where I can optimize my Javascript, where most of the time is spent, etc.


One interesting method I've seen is trying to create a FlameGraph using DTrace to profile Node.JS.

However, dtrace is very Solaris-specific.

  • For Linux (Debian/Ubuntu), dtrace can be found in the sytemtap-sdt-dev package. However, stap dtrace is not the same, and lacks all relevant hooks/probes.
  • Paul Fox made a port from the Unix version. It's more feature complete but somehow the hooks/probes don't work in userspace as the Solaris one, and also cannot be used to profile node.
    ftp://crisp.dyndns-server.com/pub/release/website/dtrace/ (It's pretty easy to build, see README.)
  • There's also an Oracle port, but noone would recommend it. Apparently, it only has about 0,1 percent of the probes the Paul Fox port. (Which is ironic, because Oracle was formerly Sun, original authors of dtrace for Solaris)

How, in Linux, using the terminal or using Eclipse, can I profile the code of my Node.JS scripts? I'm looking for something specific like the Zend Profiler shows execution times of each command in code of PHP scripts.

like image 227
Redsandro Avatar asked Oct 30 '12 16:10

Redsandro


People also ask

How does node JS calculate execution time?

In order to measure code execution time you have to provide time returned by the first process. hrtime() call, as a parameter to the second process. hrtime() call.

How do I profile a node script?

The easiest way in Node. js to profile applications is by using the inbuilt profiler, which collects all the data from functions and logs it into a file. Node. js implements this by introducing the --prof flag, which communicates with the V8 profiler and then logs the data.

How do I know if node JS is running on Linux?

To see if Node. js is installed, type node -v in Terminal. This should print the version number so you'll see something like this v0. 10.35 .

Which node js calls are used to implement delays in code execution?

One way to delay execution of a function in NodeJS is to use the seTimeout() function. Just put the code you want to delay in the callback. For example, below is how you can wait 1 second before executing some code.


2 Answers

"look" is a very good tool made by Vadim for profiling NodeJS app.

Take a look here:

https://github.com/baryshev/look

like image 135
Bogdan Le Avatar answered Nov 02 '22 16:11

Bogdan Le


If you're not against using nodetime, it actually does have CPU profiling:

See: http://nodetime.com/blog/cpu-profiling-with-nodetime

enter image description here

like image 43
brianreavis Avatar answered Nov 02 '22 15:11

brianreavis