Skip to content

Commit e600711

Browse files
joyeecheungaduh95
authored andcommitted
benchmark: add benchmark for leaf source text modules
PR-URL: #60205 Refs: #59656 Refs: #37648 Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 47fa765 commit e600711

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
3+
constvm=require('vm');
4+
constcommon=require('../common.js');
5+
constassert=require('assert');
6+
7+
constbench=common.createBenchmark(main,{
8+
stage: ['all','compile','link','instantiate','evaluate'],
9+
type: ['sync','async'],
10+
n: [1000],
11+
},{
12+
flags: ['--experimental-vm-modules'],
13+
});
14+
15+
functionmain({ stage, type, n }){
16+
constarr=[];
17+
if(stage==='all'||stage==='compile'){
18+
bench.start();
19+
}
20+
21+
for(leti=0;i<n;i++){
22+
letsource=`export const value${i} = 1;`;
23+
if(type==='async'){
24+
source+=`\nawait Promise.resolve();\n`;
25+
}
26+
constm=newvm.SourceTextModule(source);
27+
arr.push(m);
28+
}
29+
30+
if(stage==='compile'){
31+
bench.end(n);
32+
return;
33+
}
34+
35+
if(stage==='link'){
36+
bench.start();
37+
}
38+
39+
for(leti=0;i<n;i++){
40+
arr[i].linkRequests([]);
41+
}
42+
43+
if(stage==='link'){
44+
bench.end(n);
45+
return;
46+
}
47+
48+
if(stage==='instantiate'){
49+
bench.start();
50+
}
51+
52+
for(leti=0;i<n;i++){
53+
arr[i].instantiate();
54+
}
55+
56+
if(stage==='instantiate'){
57+
bench.end(n);
58+
return;
59+
}
60+
61+
if(stage==='evaluate'){
62+
bench.start();
63+
}
64+
65+
functionfinalize(){
66+
bench.end(n);
67+
for(leti=0;i<n;i++){
68+
assert.strictEqual(arr[i].namespace[`value${i}`],1);
69+
}
70+
}
71+
72+
if(type==='sync'){
73+
for(leti=0;i<n;i++){
74+
arr[i].evaluate();
75+
}
76+
finalize();
77+
}else{
78+
constpromises=[];
79+
for(leti=0;i<n;i++){
80+
promises.push(arr[i].evaluate());
81+
}
82+
Promise.all(promises).then(finalize);
83+
}
84+
}

0 commit comments

Comments
(0)