Monday 5 October 2020

Tracing Node.js program execution

I'd like to trace the execution path of an arbitrary Node.js program.

Specifically, I'd like to run a program (server or script), and have some sort of block-level (function call, loop, if statement) trace of the execution.

Constraints

  • Output must contain files / lines / hit count for all lines during execution
  • No code minification. Istanbul is great but I want to keep the code that is executed in the end as readable as possible.
  • For long-running processes (servers, for example), I want to be able to see "current" line coverage (or as up-to-date as possible)
  • I don't want to lose any coverage data, so while Profiling would give me some hints as to lines hit, it's not really code coverage.

Things I don't care about

  • Exactly how the coverage is read. For example, it could be output to a file, it could be read via the code, etc.
  • Coverage format

Thing's I've investigated so far:

Using NODE_V8_COVERAGE:

I found that if I set the NODE_V8_COVERAGE environment variable to a directory, coverage data will be output to that directory when the program exits (here's a blog post on the creation of this feature).

The problem that I'm facing here is that I'm not sure there's a way to trigger the generation of these reports before the program terminates.

Using inspector

I have also been experimenting with Node.js inspector. I found a useful CPU profiler here. This could end up being helpful, but this profiler works by sampling, not as a hook into the language. As a result, I only get line numbers / counts for parts of the code that were slow.

I also tried using Profiler.startPreciseCoverage, thinking that somehow this might give me every line that was executed (didn't find the documentation to be clear on what this does really). It didn't seem to be any more useful

Using Istanbul

I would like to avoid instrumenting code if possible.

Question

It seems like my options are limited, but at the same time this is only a result of my Googling for an hour or two.

Is there a better way to capture line coverage with the constraints listed above?



from Tracing Node.js program execution

No comments:

Post a Comment