| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 95ee3b5 commit b6bdf75
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,26 +1,17 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - const crypto = require('crypto'); | ||
| 3 | + const { spawnSync } = require("child_process"); | ||
| 4 | + const sleepTime = new Number(process.argv[2] || "0.1"); | ||
| 5 | + const repeat = new Number(process.argv[3]) || 5; | ||
| 4 | 6 | ||
| 5 | - // Functions should be complex enough for V8 to run them a few times before | ||
| 6 | - // compiling, but not complex enough to always stay in interpreted mode. They | ||
| 7 | - // should also take some time to run, otherwise Linux perf might miss them | ||
| 8 | - // entirely even when sampling at a high frequency. | ||
| 9 | - function functionOne(i) { | ||
| 10 | - for (let j=i; j > 0; j--) { | ||
| 11 | - crypto.createHash('md5').update(functionTwo(i, j)).digest("hex"); | ||
| 12 | - } | ||
| 7 | + function functionOne() { | ||
| 8 | + functionTwo(); | ||
| 13 | 9 | } | |
| 14 | 10 | ||
| 15 | - function functionTwo(x, y) { | ||
| 16 | - let data = ((((x * y) + (x / y)) * y) ** (x + 1)).toString(); | ||
| 17 | - if (x % 2 == 0) { | ||
| 18 | - return crypto.createHash('md5').update(data.repeat((x % 100) + 1)).digest("hex"); | ||
| 19 | - } else { | ||
| 20 | - return crypto.createHash('md5').update(data.repeat((y % 100) + 1)).digest("hex"); | ||
| 21 | - } | ||
| 11 | + function functionTwo() { | ||
| 12 | + spawnSync('sleep', [`${sleepTime}`]); | ||
| 22 | 13 | } | |
| 23 | 14 | ||
| 24 | - for (let i = 0; i < 1000; i++) { | ||
| 25 | - functionOne(i); | ||
| 15 | + for (let i = 0; i < repeat; i++) { | ||
| 16 | + functionOne(); | ||
| 26 | 17 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,16 +22,53 @@ tmpdir.refresh(); | |||
| 22 | 22 | if (process.config.variables.node_shared) | |
| 23 | 23 | common.skip("can't test Linux perf with shared libraries yet"); | |
| 24 | 24 | ||
| 25 | - const perfArgs = [ | ||
| 25 | + if (!common.isLinux) | ||
| 26 | + common.skip('only testing Linux for now'); | ||
| 27 | + | ||
| 28 | + const frequency = 99; | ||
| 29 | + | ||
| 30 | + const repeat = 5; | ||
| 31 | + | ||
| 32 | + // Expected number of samples we'll capture per repeat | ||
| 33 | + const sampleCount = 10; | ||
| 34 | + const sleepTime = sampleCount * (1.0 / frequency); | ||
| 35 | + | ||
| 36 | + const perfFlags = [ | ||
| 26 | 37 | 'record', | |
| 27 | - '-F999', | ||
| 38 | + `-F${frequency}`, | ||
| 28 | 39 | '-g', | |
| 29 | - '--', | ||
| 30 | - process.execPath, | ||
| 40 | + ]; | ||
| 41 | + | ||
| 42 | + const nodeCommonFlags = [ | ||
| 31 | 43 | '--perf-basic-prof', | |
| 32 | 44 | '--interpreted-frames-native-stack', | |
| 33 | 45 | '--no-turbo-inlining', // Otherwise simple functions might get inlined. | |
| 46 | + ]; | ||
| 47 | + | ||
| 48 | + const perfInterpretedFramesArgs = [ | ||
| 49 | + ...perfFlags, | ||
| 50 | + '--', | ||
| 51 | + process.execPath, | ||
| 52 | + ...nodeCommonFlags, | ||
| 53 | + '--no-opt', | ||
| 34 | 54 | fixtures.path('linux-perf.js'), | |
| 55 | + `${sleepTime}`, | ||
| 56 | + `${repeat}`, | ||
| 57 | + ]; | ||
| 58 | + | ||
| 59 | + const perfCompiledFramesArgs = [ | ||
| 60 | + ...perfFlags, | ||
| 61 | + '--', | ||
| 62 | + process.execPath, | ||
| 63 | + ...nodeCommonFlags, | ||
| 64 | + '--always-opt', | ||
| 65 | + fixtures.path('linux-perf.js'), | ||
| 66 | + `${sleepTime}`, | ||
| 67 | + `${repeat}`, | ||
| 68 | + ]; | ||
| 69 | + | ||
| 70 | + const perfArgsList = [ | ||
| 71 | + perfInterpretedFramesArgs, perfCompiledFramesArgs | ||
| 35 | 72 | ]; | |
| 36 | 73 | ||
| 37 | 74 | const perfScriptArgs = [ | |
@@ -43,33 +80,27 @@ const options = { | |||
| 43 | 80 | encoding: 'utf-8', | |
| 44 | 81 | }; | |
| 45 | 82 | ||
| 46 | - if (!common.isLinux) | ||
| 47 | - common.skip('only testing Linux for now'); | ||
| 48 | - | ||
| 49 | - const perf = spawnSync('perf', perfArgs, options); | ||
| 50 | - | ||
| 51 | - if (perf.error && perf.error.errno === 'ENOENT') | ||
| 52 | - common.skip('perf not found on system'); | ||
| 53 | - | ||
| 54 | - if (perf.status !== 0) { | ||
| 55 | - common.skip(`Failed to execute perf: ${perf.stderr}`); | ||
| 56 | - } | ||
| 83 | + let output = ''; | ||
| 57 | 84 | ||
| 58 | - const perfScript = spawnSync('perf', perfScriptArgs, options); | ||
| 85 | + for (const perfArgs of perfArgsList) { | ||
| 86 | + const perf = spawnSync('perf', perfArgs, options); | ||
| 87 | + assert.ifError(perf.error); | ||
| 88 | + if (perf.status !== 0) | ||
| 89 | + throw new Error(`Failed to execute 'perf': ${perf.stderr}`); | ||
| 59 | 90 | ||
| 60 | - if (perf.error) | ||
| 61 | - common.skip(`perf script aborted: ${perf.error.errno}`); | ||
| 91 | + const perfScript = spawnSync('perf', perfScriptArgs, options); | ||
| 92 | + assert.ifError(perfScript.error); | ||
| 93 | + if (perfScript.status !== 0) | ||
| 94 | + throw new Error(`Failed to execute perf script: ${perfScript.stderr}`); | ||
| 62 | 95 | ||
| 63 | - if (perfScript.status !== 0) { | ||
| 64 | - common.skip(`Failed to execute perf script: ${perfScript.stderr}`); | ||
| 96 | + output += perfScript.stdout; | ||
| 65 | 97 | } | |
| 66 | 98 | ||
| 67 | 99 | const interpretedFunctionOneRe = /InterpretedFunction:functionOne/; | |
| 68 | 100 | const compiledFunctionOneRe = /LazyCompile:\*functionOne/; | |
| 69 | 101 | const interpretedFunctionTwoRe = /InterpretedFunction:functionTwo/; | |
| 70 | 102 | const compiledFunctionTwoRe = /LazyCompile:\*functionTwo/; | |
| 71 | 103 | ||
| 72 | - const output = perfScript.stdout; | ||
| 73 | 104 | ||
| 74 | 105 | assert.ok(output.match(interpretedFunctionOneRe), | |
| 75 | 106 | "Couldn't find interpreted functionOne()"); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments