Skip to content
Permalink
f2505d31f3
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
@aa7401
Latest commit 638681b Sep 9, 2019 History
1 contributor

Users who have contributed to this file

Profiling and Documentation

Resources:

  1. Lecture slides

1 Profiling

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:

  1. -k - KeepAlive - perform multiple requests using the same session.
  2. -c 20 - Concurrency: the number of concurrent requests.
  3. -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.

The 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

References

https://nodejs.org/en/docs/guides/simple-profiling/

Using the profiling tool together with the Apache Bench server benchmarking tool.

2 Debugging

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 list
  • unwatch(expr) - Remove expression from watch list
  • watchers - List all watchers and their values (automatically listed on each breakpoint)

Using the Chrome DevTools

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

opening the Chrome DevTools

This will start monitoring websockets on two ports, we will be using port 9229.

opening the Chrome DevTools

Start your script as before using the node inspect index.js command.

3 Documentation

3.1 JSDoc

3.2 APIDoc