Skip to content

Commit a31f9bf

Browse files
lukealbaoUlisesGascon
authored andcommitted
test: v8: Add test-linux-perf-logger test suite
PR-URL: #50352 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent cca55b8 commit a31f9bf

2 files changed

Lines changed: 165 additions & 0 deletions

File tree

‎test/fixtures/linux-perf-logger.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
process.stdout.write(`${process.pid}`);
4+
5+
consttestRegex=/test-regex/gi;
6+
7+
functionfunctionOne(){
8+
for(leti=0;i<100;i++){
9+
constmatch=testRegex.exec(Math.random().toString());
10+
}
11+
}
12+
13+
functionfunctionTwo(){
14+
functionOne();
15+
}
16+
17+
functionTwo();
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
'use strict';
2+
3+
// --- About this test suite
4+
//
5+
// JIT support for perf(1) was added in 2009 (see https://lkml.org/lkml/2009/6/8/499).
6+
// It works by looking for a perf map file in /tmp/perf-<pid>.map, where <pid> is the
7+
// PID of the target process.
8+
//
9+
// The structure of this file is stable. Perf expects each line to specify a symbol
10+
// in the form:
11+
//
12+
// <start> <length> <name>
13+
//
14+
// where <start> is the hex representation of the instruction pointer for the beginning
15+
// of the function, <length> is the byte length of the function, and <name> is the
16+
// readable JIT name used for reporting.
17+
//
18+
// This file asserts that a node script run with the appropriate flags will produce
19+
// a compliant perf map.
20+
//
21+
// NOTE: This test runs only on linux, as that is the only platform supported by perf, and
22+
// accordingly the only platform where `perf-basic-prof*` v8 flags are available.
23+
24+
25+
constcommon=require('../common');
26+
if(!common.isLinux){
27+
common.skip('--perf-basic-prof* is statically defined as linux-only');
28+
}
29+
30+
constassert=require('assert');
31+
const{ spawnSync }=require('child_process');
32+
const{ readFileSync }=require('fs');
33+
34+
constfixtures=require('../common/fixtures');
35+
consttmpdir=require('../common/tmpdir');
36+
tmpdir.refresh();
37+
38+
consttestCases=[
39+
{
40+
title: '--perf-basic-prof interpreted',
41+
nodeFlags: ['--perf-basic-prof','--no-turbo-inlining','--no-opt'],
42+
matches: [
43+
'~functionOne .+/linux-perf-logger.js',
44+
'~functionTwo .+/linux-perf-logger.js',
45+
'test-regex',
46+
],
47+
noMatches: ['\\*functionOne','\\*functionTwo'],
48+
},
49+
{
50+
title: '--perf-basic-prof compiled',
51+
nodeFlags: ['--perf-basic-prof','--no-turbo-inlining','--always-opt'],
52+
matches: [
53+
'test-regex',
54+
'~functionOne .+/linux-perf-logger.js',
55+
'~functionTwo .+/linux-perf-logger.js',
56+
'\\*functionOne .+/linux-perf-logger.js',
57+
'\\*functionTwo .+/linux-perf-logger.js',
58+
],
59+
noMatches: [],
60+
},
61+
{
62+
title: '--perf-basic-prof-only-functions interpreted',
63+
nodeFlags: ['--perf-basic-prof-only-functions','--no-turbo-inlining','--no-opt'],
64+
matches: ['~functionOne .+/linux-perf-logger.js','~functionTwo .+/linux-perf-logger.js'],
65+
noMatches: ['\\*functionOne','\\*functionTwo','test-regex'],
66+
},
67+
{
68+
title: '--perf-basic-prof-only-functions compiled',
69+
nodeFlags: ['--perf-basic-prof-only-functions','--no-turbo-inlining','--always-opt'],
70+
matches: [
71+
'~functionOne .+/linux-perf-logger.js',
72+
'~functionTwo .+/linux-perf-logger.js',
73+
'\\*functionOne .+/linux-perf-logger.js',
74+
'\\*functionTwo .+/linux-perf-logger.js',
75+
],
76+
noMatches: ['test-regex'],
77+
},
78+
];
79+
80+
functionrunTest(test){
81+
constreport={
82+
title: test.title,
83+
perfMap: '[uninitialized]',
84+
errors: [],
85+
};
86+
87+
constargs=test.nodeFlags.concat(fixtures.path('linux-perf-logger.js'));
88+
construn=spawnSync(process.execPath,args,{cwd: tmpdir.path,encoding: 'utf8'});
89+
if(run.error){
90+
report.errors.push(run.error.stack);
91+
returnreport;
92+
}
93+
if(run.status!==0){
94+
report.errors.push(`running script:\n${run.stderr}`);
95+
returnreport;
96+
}
97+
98+
try{
99+
report.perfMap=readFileSync(`/tmp/perf-${run.pid}.map`,'utf8');
100+
}catch(err){
101+
report.errors.push(`reading perf map: ${err.stack}`);
102+
returnreport;
103+
}
104+
105+
consthexRegex='[a-fA-F0-9]+';
106+
for(consttestRegexoftest.matches){
107+
constlineRegex=newRegExp(`${hexRegex}${hexRegex}.*:${testRegex}`);
108+
if(!lineRegex.test(report.perfMap)){
109+
report.errors.push(`Expected to match ${lineRegex}`);
110+
}
111+
}
112+
113+
for(constregexoftest.noMatches){
114+
constnoMatch=newRegExp(regex);
115+
if(noMatch.test(report.perfMap)){
116+
report.errors.push(`Expected not to match ${noMatch}`);
117+
}
118+
}
119+
120+
returnreport;
121+
}
122+
123+
functionserializeError(report,index){
124+
return`[ERROR ${index+1}] ${report.title}
125+
Errors:
126+
${report.errors.map((err,i)=>`${i+1}. ${err}`).join('\n')}
127+
Perf map content:
128+
${report.perfMap}
129+
</end perf map content>
130+
`;
131+
}
132+
133+
functionrunSuite(){
134+
constfailures=[];
135+
136+
for(consttcoftestCases){
137+
constreport=runTest(tc);
138+
if(report.errors.length>0){
139+
failures.push(report);
140+
}
141+
}
142+
143+
consterrorsToReport=failures.map(serializeError).join('\n--------\n');
144+
145+
assert.strictEqual(failures.length,0,`${failures.length} tests failed\n\n${errorsToReport}`);
146+
}
147+
148+
runSuite();

0 commit comments

Comments
 (0)