One of the reasons for slow load times is inefficient code, which uses a lot of CPU making everything else wait. This is especially painful in Node.js due to its single threaded model. First signes of the problem are high CPU times of requests, which is easily observable in Nodetime, where CPU times are provided for each request sample. Although, machine load can also indicate a problem. The logical next step is to locate what exactly blocks CPU and where is that function in the code.
V8 engine, on which Node.js is built, has a built-in sampling CPU profiler. It records where the instruction pointer was every millisecond and creates a call tree based on this. Although not exact, but the information is statistically sufficient to identify which functions spent the most time in CPU.
To run the profiler manually a few things must be done. d8 should be compiled separately and Node application should be started with
--prof option. After starting, v8.log file will be created, which should be analyzed with special OS-specific tools packaged with Node. A couple of libraries are available, which allow starting and stopping V8 profiler via API.
Nodetime makes it increadably easy to use V8's CPU profiler. Just
require('nodetime').profile() on the first line of your application and you're done. Normally you would start sending subsequent or parallel requrests to your server using
ab, other tool of choice or just the browser, and while it's being done start the profiler from Nodetime's web console. Here is what it looks like in the web console (a screencast is available here):
We see that functions test1 and test2 were found pretty often on CPU. The basic rule is to focus only on calls with high percentage to locate call traces, which were found to be more often on CPU.
Checkout how to install and use Nodetime here.