Skip to content

Commit

Permalink
feat: can be used in worker threads (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 authored May 14, 2022
1 parent de25633 commit 8eea608
Show file tree
Hide file tree
Showing 44 changed files with 1,619 additions and 731 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
BasedOnStyle: Google
DerivePointerAlignment: false
MaxEmptyLinesToKeep: 1
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 ]
node-version: [ 4, 6, 8, 10, 12, 14, 16, 18 ]
steps:
- name: Checkout Git Source
uses: actions/checkout@master
Expand All @@ -32,7 +32,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 ]
node-version: [ 8, 10, 12, 14, 16, 18 ]
steps:
- name: Checkout Git Source
uses: actions/checkout@master
Expand Down
111 changes: 110 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ v8-profiler-next provides [node](https://github.com/nodejs/node) bindings for the
## I. Quick Start

* **Compatibility**
* **node version:** v4.x ~ v17.x
* **node version:** v4.x ~ v18.x
* **platform:** mac, linux, windows

This module can also be used in `worker_threads`.

### take cpu profile

```js
Expand Down Expand Up @@ -47,6 +49,42 @@ setTimeout(() => {
}, 5 * 60 * 1000);
```

Get `.cpuprofile` in `worker_threads`:

```js
'use strict';

const fs = require('fs');
const path = require('path');
const v8Profiler = require('./');
const workerThreads = require('worker_threads');

v8Profiler.setGenerateType(1);

if (workerThreads.isMainThread) {
const w = new workerThreads.Worker(__filename, {
env: process.env,
});
v8Profiler.startProfiling('main', true);
w.once('exit', code => {
// create cpu profile in main thread
const profile = v8Profiler.stopProfiling('main');
const mainProfile = path.join(__dirname, 'main.cpuprofile');
fs.existsSync(mainProfile) && fs.unlinkSync(mainProfile);
fs.writeFileSync(mainProfile, JSON.stringify(profile));
});
} else {
v8Profiler.startProfiling('worker_threads', true);
// create cpu profile in worker_threads
const start = Date.now();
while (Date.now() - start < 2000) { }
const profile = v8Profiler.stopProfiling('worker_threads');
const workerProfile = path.join(__dirname, 'worker_threads.cpuprofile');
fs.existsSync(workerProfile) && fs.unlinkSync(workerProfile);
fs.writeFileSync(workerProfile, JSON.stringify(profile));
}
```

### take heapsnapshot

```js
Expand All @@ -68,6 +106,42 @@ transform.pipe(process.stdout);
transform.on('finish', snapshot.delete.bind(snapshot));
```

Get `.heapsnapshot` in `worker_threads`:

```js
'use strict';

const fs = require('fs');
const path = require('path');
const v8Profiler = require('./');
const workerThreads = require('worker_threads');

function createSnapshot(filename) {
const snapshot = v8Profiler.takeSnapshot();
const file = path.join(__dirname, filename);
const transform = snapshot.export();
transform.pipe(fs.createWriteStream(file));
transform.on('finish', snapshot.delete.bind(snapshot));
}

if (workerThreads.isMainThread) {
const w = new workerThreads.Worker(__filename, {
env: process.env,
});

// create heapsnapshot in main thread
createSnapshot('main.heapsnapshot');

} else {
const start = Date.now();
const array = [];
while (Date.now() - start < 2000) { array.push(new Array(1e3).fill('*')); }

// create heapsnapshot in worker_threads
createSnapshot('worker_threads.heapsnapshot');
}
```

### take allocation profile

**Attention:** If node version < v12.x, please use sampling heap profiling alone without cpu profiling or taking snapshot.
Expand All @@ -91,6 +165,41 @@ setTimeout(() => {
}, 60 * 1000);
```

Get `.heapprofile` in `worker_threads`:

```js
'use strict';

const fs = require('fs');
const path = require('path');
const v8Profiler = require('./');
const workerThreads = require('worker_threads');

if (workerThreads.isMainThread) {
const w = new workerThreads.Worker(__filename, {
env: process.env,
});
v8Profiler.startSamplingHeapProfiling();
w.once('exit', code => {
// create heap profile in main thread
const profile = v8Profiler.stopSamplingHeapProfiling();
const mainProfile = path.join(__dirname, 'main.heapprofile');
fs.existsSync(mainProfile) && fs.unlinkSync(mainProfile);
fs.writeFileSync(mainProfile, JSON.stringify(profile));
});
} else {
v8Profiler.startSamplingHeapProfiling();
// create heap profile in worker_threads
const start = Date.now();
const array = [];
while (Date.now() - start < 2000) { array.push(new Array(1e3).fill('*')); }
const profile = v8Profiler.stopSamplingHeapProfiling();
const workerProfile = path.join(__dirname, 'worker_threads.heapprofile');
fs.existsSync(workerProfile) && fs.unlinkSync(workerProfile);
fs.writeFileSync(workerProfile, JSON.stringify(profile));
}
```

## II. License

[MIT License](LICENSE)
Expand Down
8 changes: 1 addition & 7 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ environment:
matrix:
# node.js
- nodejs_version: "4"
- nodejs_version: "5"
- nodejs_version: "6"
- nodejs_version: "7"
- nodejs_version: "8"
- nodejs_version: "9"
- nodejs_version: "10"
- nodejs_version: "11"
- nodejs_version: "12"
- nodejs_version: "13"
- nodejs_version: "14"
- nodejs_version: "15"
- nodejs_version: "16"
- nodejs_version: "17"
- nodejs_version: "18"

# Install scripts. (runs after repo cloning)
install:
Expand Down
20 changes: 11 additions & 9 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
'target_name': 'profiler',
'win_delay_load_hook': 'false',
'sources': [
'src/cpu_profiler/cpu_profiler.cc',
'src/cpu_profiler/cpu_profile.cc',
'src/cpu_profiler/cpu_profile_node.cc',
'src/heap_profiler/sampling_heap_profiler.cc',
'src/heapsnapshot/heap_profiler.cc',
'src/heapsnapshot/heap_snapshot.cc',
'src/heapsnapshot/heap_output_stream.cc',
'src/heapsnapshot/heap_graph_node.cc',
'src/heapsnapshot/heap_graph_edge.cc',
'src/profiler.cc',
'src/cpu_profiler.cc',
'src/cpu_profile.cc',
'src/cpu_profile_node.cc',
'src/heap_profiler.cc',
'src/heap_snapshot.cc',
'src/heap_output_stream.cc',
'src/heap_graph_node.cc',
'src/heap_graph_edge.cc',
'src/sampling_heap_profile.cc'
'src/environment_data.cc'
],
'include_dirs' : [
"src",
"<!(node -e \"require('nan')\")"
],
'conditions':[
Expand Down
20 changes: 20 additions & 0 deletions lib/worker_threads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

let canIUseWorkerThreads = false;
let isMainThread = true;
let threadId = 0;

try {
const workerThreads = require('worker_threads');
isMainThread = workerThreads.isMainThread;
threadId = workerThreads.threadId;
canIUseWorkerThreads = true;
} catch (err) {
err;
}

module.exports = {
isMainThread,
threadId,
canIUseWorkerThreads,
};
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
"description": "node bindings for the v8 profiler",
"main": "dispatch.js",
"scripts": {
"test": "mocha test/*.test.js --timeout 10000",
"build": "node-gyp rebuild"
"test-old": "mocha test/*.test.js --exclude test/worker.test.js --timeout 10000",
"test-new": "mocha test/*.test.js --timeout 10000",
"test": "node scripts/test.js",
"test-single": "mocha --timeout 10000",
"build": "node-gyp rebuild",
"format": "clang-format -i --glob=\"src/**/*[.h|.cc]\""
},
"repository": {
"type": "git",
Expand All @@ -23,6 +27,7 @@
"homepage": "https://github.com/hyj1991/v8-profiler-next#readme",
"files": [
"src",
"lib",
"binding.gyp",
"dispatch.js",
"index.d.ts",
Expand All @@ -34,6 +39,7 @@
},
"devDependencies": {
"chai": "^4.2.0",
"clang-format": "^1.8.0",
"mocha": "^5.2.0"
}
}
19 changes: 19 additions & 0 deletions scripts/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const cp = require('child_process');
const path = require('path');
const nodeVersion = process.versions.node;

function exec(cmd) {
cp.execSync(cmd, {
cwd: path.join(__dirname, '../'),
stdio: 'inherit', env: process.env
});
}

const tags = nodeVersion.split('.');
if (tags[0] < 8) {
exec('npm run test-old');
} else {
exec('npm run test-new');
}
21 changes: 0 additions & 21 deletions src/cpu_profile.h

This file was deleted.

25 changes: 0 additions & 25 deletions src/cpu_profile_node.h

This file was deleted.

Loading

0 comments on commit 8eea608

Please sign in to comment.