Resources:
NodeJS includes the Tick Profiler, a native tool, which allows you to profile your NodeJS code. It is activated using the --prof
flag, eg:
$ node --prof index.js
This will capture data on the performance but, in a development environment this will need to be simulated using the Apache Bench tool, eg:
$ ab -k -c 20 -n 250 "http://localhost:8080"
You will note that there are three flags used:
-k
- KeepAlive - perform multiple requests using the same session.-c 20
- Concurrency: the number of concurrent requests.-n 250
- Requests: the number of requests to perform in the session.
Apache Bench generates a range of useful data however we need more information to highlight where the issues are in the code. This is done using a tick file.
by using the --prof
flag, NodeJS generates a tick file in the current directory with the name isolate-0xnnnnnnnnnnnn-v8.log
, where nnn... is a number. This file needs to be interpreted using a tick interpreter and the analysis piped to a text file which can then be opened and read.
$ node -prof-process isolate-0xnnnnnnnnnnnn-v8.log > processes.txt
https://nodejs.org/en/docs/guides/simple-profiling/
Using the profiling tool together with the Apache Bench server benchmarking tool.
Node.js includes an out-of-process debugging utility accessible via a V8 Inspector and built-in debugging client. To use it, start Node.js with the inspect argument followed by the path to the script to debug; a prompt will be displayed indicating successful launch of the debugger:
$ node inspect index.js
< Debugger listening on ws://127.0.0.1:9229/ea2bbf59-874e-45c0-9535-1553980dfec2
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
By default the debugger will stop on the first statement. It supports several commands:
cmd | action |
---|---|
n | Step next (run the highlighted line and move to the next line but don't execute it) |
s | Step in |
o | Step out |
c | Continue execution until next breakpoint |
It is also possible to attach watchers to track values:
watch(expr)
- Add expression to watch listunwatch(expr)
- Remove expression from watch listwatchers
- List all watchers and their values (automatically listed on each breakpoint)
The debugger can also connect to the Chrome Devtools.
Open the about:inspect
url in the chrome browser, this will open the devtools window as shown. Click on the
This will start monitoring websockets on two ports, we will be using port 9229
.
Start your script as before using the node inspect index.js
command.