From 18c2cd0e27fec4f6da0dfe06650e1e5151622112 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 9 Jun 2020 20:46:52 +0800 Subject: [PATCH 1/2] v8: implement v8.takeCoverage() Add an v8.takeCoverage() API that allows the user to write the coverage started by NODE_V8_COVERAGE to disk on demand. The coverage can be written multiple times during the lifetime of the process, each time the execution counter will be reset. When the process is about to exit, one last coverage will still be written to disk. Also refactors the internal profiler connection code so that we use the inspector response id to identify the profile response instead of using an ad-hoc flag in C++. --- doc/api/v8.md | 16 ++ lib/v8.js | 7 + src/inspector_profiler.cc | 187 ++++++++++++-------- src/inspector_profiler.h | 21 ++- test/fixtures/v8-coverage/interval.js | 14 ++ test/fixtures/v8-coverage/take-coverage.js | 10 ++ test/parallel/test-v8-take-coverage-noop.js | 32 ++++ test/parallel/test-v8-take-coverage.js | 85 +++++++++ 8 files changed, 292 insertions(+), 80 deletions(-) create mode 100644 test/fixtures/v8-coverage/interval.js create mode 100644 test/fixtures/v8-coverage/take-coverage.js create mode 100644 test/parallel/test-v8-take-coverage-noop.js create mode 100644 test/parallel/test-v8-take-coverage.js diff --git a/doc/api/v8.md b/doc/api/v8.md index 891060606f6388..acdc122ed6a563 100644 --- a/doc/api/v8.md +++ b/doc/api/v8.md @@ -214,6 +214,21 @@ v8.setFlagsFromString('--trace_gc'); setTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 60e3); ``` +## `v8.takeCoverage()` + + + +The `v8.takeCoverage()` method allows the user to write the coverage started by +[`NODE_V8_COVERAGE`][] to disk on demand. This method can be invoked multiple +times during the lifetime of the process, each time the execution counter will +be reset and a new coverage report will be written to the directory specified +by [`NODE_V8_COVERAGE`][]. + +When the process is about to exit, one last coverage will still be written to +disk. + ## `v8.writeHeapSnapshot([filename])` + +The `v8.stopCoverage()` method allows the user to stop the coverage collection +started by [`NODE_V8_COVERAGE`][], so that V8 can release the execution count +records and optimize code. This can be used in conjunction with +`v8.takeCoverage()` if the user wants to collect the coverage on demand. ## `v8.writeHeapSnapshot([filename])`