-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
Expand file tree
/
Copy pathtest-v8-cpu-profile.js
More file actions
49 lines (41 loc) · 1.17 KB
/
test-v8-cpu-profile.js
File metadata and controls
49 lines (41 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
const common = require('../common');
const assert = require('assert');
const v8 = require('v8');
function busyLoop(iterations = 1000) {
let total = 0;
for (let i = 0; i < iterations; i++) {
total += Math.sqrt(i + total);
}
return total;
}
function burnBlocks(blocks = 200, iterations = 1000) {
let total = 0;
for (let i = 0; i < blocks; i++) {
total += busyLoop(iterations);
}
return total;
}
function collectProfile(options, workload) {
const handle = v8.startCpuProfile(options);
workload();
return JSON.parse(handle.stop());
}
{
const profile = collectProfile({ sampleInterval: 0.001 }, () => burnBlocks(400, 2000));
assert.ok(profile.samples.length >= 1);
}
{
const profile = collectProfile({ sampleInterval: 0.5, maxBufferSize: 3 }, () => burnBlocks(400, 2000));
assert.ok(profile.samples.length <= 3);
}
{
const profile = collectProfile({ sampleInterval: 0.1, maxBufferSize: 50 }, () => burnBlocks(600, 3000));
assert.ok(profile.samples.length >= 1);
assert.ok(profile.samples.length <= 50);
}
{
assert.throws(
() => v8.startCpuProfile({ sampleInterval: -1 }),
common.expectsError({ code: 'ERR_OUT_OF_RANGE' }));
}