1'use strict'; 2 3const common = require('../common.js'); 4const cp = require('child_process'); 5 6const command = 'echo'; 7const args = ['hello']; 8const options = {}; 9const cb = () => {}; 10 11const configs = { 12 n: [1e3], 13 methodName: [ 14 'exec', 'execSync', 15 'execFile', 'execFileSync', 16 'spawn', 'spawnSync', 17 ], 18 params: [1, 2, 3, 4], 19}; 20 21const bench = common.createBenchmark(main, configs); 22 23function main({ n, methodName, params }) { 24 const method = cp[methodName]; 25 26 switch (methodName) { 27 case 'exec': 28 switch (params) { 29 case 1: 30 bench.start(); 31 for (let i = 0; i < n; i++) method(command).kill(); 32 bench.end(n); 33 break; 34 case 2: 35 bench.start(); 36 for (let i = 0; i < n; i++) method(command, options).kill(); 37 bench.end(n); 38 break; 39 case 3: 40 bench.start(); 41 for (let i = 0; i < n; i++) method(command, options, cb).kill(); 42 bench.end(n); 43 break; 44 } 45 break; 46 case 'execSync': 47 switch (params) { 48 case 1: 49 bench.start(); 50 for (let i = 0; i < n; i++) method(command); 51 bench.end(n); 52 break; 53 case 2: 54 bench.start(); 55 for (let i = 0; i < n; i++) method(command, options); 56 bench.end(n); 57 break; 58 } 59 break; 60 case 'execFile': 61 switch (params) { 62 case 1: 63 bench.start(); 64 for (let i = 0; i < n; i++) method(command).kill(); 65 bench.end(n); 66 break; 67 case 2: 68 bench.start(); 69 for (let i = 0; i < n; i++) method(command, args).kill(); 70 bench.end(n); 71 break; 72 case 3: 73 bench.start(); 74 for (let i = 0; i < n; i++) method(command, args, options).kill(); 75 bench.end(n); 76 break; 77 case 4: 78 bench.start(); 79 for (let i = 0; i < n; i++) method(command, args, options, cb).kill(); 80 bench.end(n); 81 break; 82 } 83 break; 84 case 'execFileSync': 85 case 'spawnSync': 86 switch (params) { 87 case 1: 88 bench.start(); 89 for (let i = 0; i < n; i++) method(command); 90 bench.end(n); 91 break; 92 case 2: 93 bench.start(); 94 for (let i = 0; i < n; i++) method(command, args); 95 bench.end(n); 96 break; 97 case 3: 98 bench.start(); 99 for (let i = 0; i < n; i++) method(command, args, options); 100 bench.end(n); 101 break; 102 } 103 break; 104 case 'spawn': 105 switch (params) { 106 case 1: 107 bench.start(); 108 for (let i = 0; i < n; i++) method(command).kill(); 109 bench.end(n); 110 break; 111 case 2: 112 bench.start(); 113 for (let i = 0; i < n; i++) method(command, args).kill(); 114 bench.end(n); 115 break; 116 case 3: 117 bench.start(); 118 for (let i = 0; i < n; i++) method(command, args, options).kill(); 119 bench.end(n); 120 break; 121 } 122 break; 123 } 124} 125