Profiling Your Code On The Server
There are a large number of third-party code profiling tools for NodeJS. You should consider looking into some of those if you are having a hard time identifying slowdowns. Especially if you have real-world users who are having server performance issues, but you can't replicate them in your development environment.
For many of your profiling needs, you can use the same V8 profiler that is part of the V8 engine used in Chrome. As with profiling your process performance on the browser you only run this for a limited time, and you shouldn't run this on your production server.
To run node with the profiler enabled then add the following tag to the command line.
So your command to run the server will look something like
node --prof index.js
After you run the command, you will want to repeatedly access each of the endpoints you are trying to profile. Accessing the URL of the endpoints can be done in several ways.
You could use the application and manually go to the parts that will call the URL. Utilizing the application front-end can be slow and unfocused, especially if you have to refresh the page where it will almost always make several additional requests to other endpoints.
Another option is to use a tool like Postman(https://www.getpostman.com/) or curl(built-in or easy to install on most *nix based OS's) that lets you access an endpoint directly. Postman can be especially helpful if you have to be authenticated to access the endpoint.
Then there are tools specifically designed for load testing a server such as ApacheBench(https://httpd.apache.org/docs/2.4/programs/ab.html) or Seige(https://www.joedog.org/siege-home/). These have the advantage of pushing the server with lots of traffic and less overall work for you in most cases. With ApacheBench you can include request headers, which allows you to pass authentication information.
After accessing the endpoint, you are testing stop your server.
You will now have a file labeled something like:
This file isn't useful to us as is. To make it readable, we will need to process it. Processing can be done using NodeJS with the following flag:
This flag requires you pass in the filename and prints a human-readable version of the file. So you may want to pipe the results to another file using a command such as:
node --prof-process isolate-0xXXXXXXXXXXXX-v8.log > endpoint-name.profile.txt
In there we can see what it is that is using up your CPU processing time.