-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: playing around with initial implementation
- Loading branch information
Benjamin Coe
committed
Oct 26, 2017
0 parents
commit 18f5471
Showing
10 changed files
with
695 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Copyright (c) 2017, Contributors | ||
|
||
Permission to use, copy, modify, and/or distribute this software | ||
for any purpose with or without fee is hereby granted, provided | ||
that the above copyright notice and this permission notice | ||
appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE | ||
LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES | ||
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | ||
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | ||
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# c8 - native code-coverage | ||
|
||
Code-coverage using [v8's Profiler](https://nodejs.org/dist/latest-v8.x/docs/api/inspector.html) | ||
compatible with [Istanbul's reporters](https://istanbul.js.org/docs/advanced/alternative-reporters/). | ||
|
||
Like [nyc](https://github.com/istanbuljs/nyc), c8 just magically works, simply: | ||
|
||
```bash | ||
npm i c8 -g | ||
c8 node foo.js | ||
``` | ||
|
||
The above example will collect coverage for `foo.js` using v8's profiler. | ||
|
||
TODO: | ||
|
||
- [ ] write logic for converting v8 coverage output to [Istanbul Coverage.json format](https://github.com/gotwarlost/istanbul/blob/master/coverage.json.md). | ||
- [ ] talk to Node.js project about silencing messages: | ||
|
||
> `Debugger listening on ws://127.0.0.1:56399/e850110a-c5df-41d8-8ef2-400f6829617f`. | ||
- [ ] figure out why `detailed` mode does not appear to be working. | ||
- [ ] figure out a better way to determine that all processes in event loop | ||
have terminated (except the inspector session). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
const argv = require('yargs').parse() | ||
const foreground = require('foreground-child') | ||
const sw = require('spawn-wrap') | ||
|
||
if (argv._.length) { | ||
sw([require.resolve('./wrap')]) | ||
foreground(process.argv.slice(2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const fs = require('fs') | ||
const sw = require('spawn-wrap') | ||
const CRI = require('chrome-remote-interface'); | ||
const getPort = require('get-port'); | ||
const inspector = require('inspector') | ||
|
||
// if there are N or less active handles | ||
// in the event loop, dump coverage and exit. | ||
const EXIT_HANDLE_COUNT = 4 | ||
|
||
getPort().then(async port => { | ||
// start an inspector session on an unused port. | ||
inspector.open(port, true) | ||
const client = await CRI({port: port}) | ||
const {Profiler} = client | ||
await Profiler.enable() | ||
await Profiler.startPreciseCoverage({callCount: true, detailed: true}) | ||
|
||
// run the original "main" now that we've started the inspector. | ||
sw.runMain() | ||
|
||
// wait for everything in event loop to terminate | ||
// except for inspector session. | ||
setInterval(() => { | ||
const handleCount = process._getActiveHandles().length | ||
+ process._getActiveRequests().length | ||
if (handleCount <= EXIT_HANDLE_COUNT) { | ||
outputCoverageAndExit(client, Profiler) | ||
} | ||
}, 100) | ||
}).catch(err => { | ||
throw err | ||
}) | ||
|
||
async function outputCoverageAndExit (client, Profiler) { | ||
const IGNORED_PATHS = [ | ||
/\/bin\/wrap.js/, | ||
/\/node_modules\//, | ||
/node-spawn-wrap/ | ||
] | ||
let {result} = await Profiler.takePreciseCoverage() | ||
result = result.filter(coverage => { | ||
for (var ignored, i = 0; (ignored = IGNORED_PATHS[i]) !== undefined; i++) { | ||
if (ignored.test(coverage.url)) return false | ||
} | ||
if (!/^\//.test(coverage.url)) return false | ||
else return true | ||
}) | ||
console.log(JSON.stringify(result, null, 2)) | ||
client.close() | ||
process.exit(0) | ||
} |
Empty file.
Oops, something went wrong.