|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const vm = require('vm'); |
| 4 | +const common = require('../common.js'); |
| 5 | +const assert = require('assert'); |
| 6 | + |
| 7 | +const bench = common.createBenchmark(main, { |
| 8 | + stage: ['all', 'link', 'instantiate', 'evaluate'], |
| 9 | + n: [1000], |
| 10 | +}, { |
| 11 | + flags: ['--experimental-vm-modules'], |
| 12 | +}); |
| 13 | + |
| 14 | +function main({ stage, n }) { |
| 15 | + const arr = []; |
| 16 | + let importSource = ''; |
| 17 | + let useSource = 'export const result = 0 '; |
| 18 | + for (let i = 0; i < n; i++) { |
| 19 | + importSource += `import { value${i} } from 'mod${i}';\n`; |
| 20 | + useSource += ` + value${i}\n`; |
| 21 | + } |
| 22 | + |
| 23 | + if (stage === 'all') { |
| 24 | + bench.start(); |
| 25 | + } |
| 26 | + for (let i = 0; i < n; i++) { |
| 27 | + const m = new vm.SourceTextModule(` |
| 28 | + export const value${i} = 1; |
| 29 | + `); |
| 30 | + arr.push(m); |
| 31 | + } |
| 32 | + |
| 33 | + const root = new vm.SourceTextModule(` |
| 34 | + ${importSource} |
| 35 | + ${useSource}; |
| 36 | + `); |
| 37 | + |
| 38 | + if (stage === 'link') { |
| 39 | + bench.start(); |
| 40 | + } |
| 41 | + |
| 42 | + root.linkRequests(arr); |
| 43 | + for (let i = 0; i < n; i++) { |
| 44 | + arr[i].linkRequests([]); |
| 45 | + } |
| 46 | + |
| 47 | + if (stage === 'link') { |
| 48 | + bench.end(n); |
| 49 | + } |
| 50 | + |
| 51 | + if (stage === 'instantiate') { |
| 52 | + bench.start(); |
| 53 | + } |
| 54 | + root.instantiate(); |
| 55 | + if (stage === 'instantiate') { |
| 56 | + bench.end(n); |
| 57 | + } |
| 58 | + |
| 59 | + if (stage === 'evaluate') { |
| 60 | + bench.start(); |
| 61 | + } |
| 62 | + root.evaluate(); |
| 63 | + if (stage === 'evaluate' || stage === 'all') { |
| 64 | + bench.end(n); |
| 65 | + } |
| 66 | + |
| 67 | + assert.strictEqual(root.namespace.result, n); |
| 68 | +} |
0 commit comments