-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarkRunner.js
127 lines (101 loc) · 3.09 KB
/
benchmarkRunner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs');
const { parseArgs, exec } = require('./helpers');
const {
filesInt,
testCasesInt,
filesBigInt,
testCasesBigInt,
} = require('./test-data');
function calculateAverageTime(executionTimes) {
return (
executionTimes.reduce((sum, time) => sum + time, 0) / executionTimes.length
);
}
function ensureDirectoryExists(directory) {
if (fs.existsSync(directory)) {
return;
}
fs.mkdirSync(directory);
}
function generateFileName(options) {
const { directory, fileName, rowCount, colCount } = options;
return `${directory}/${fileName}_${rowCount}x${colCount}.txt`;
}
function generateContent(averageTime, executionTimes) {
return [
`Average Execution Time: ${averageTime} ms`,
'---',
...executionTimes.map((time, i) => `${i + 1}. Execution Time: ${time} ms`),
].join('\n');
}
function saveExecutionResults(options) {
const { executionTimes, averageTime } = options;
const directory = 'benchmark';
ensureDirectoryExists(directory);
const resultFileName = generateFileName({ directory, ...options });
const content = generateContent(averageTime, executionTimes);
fs.writeFileSync(resultFileName, content);
console.log(`File saved: ${resultFileName}`);
}
function runBenchmarkTest(testCase) {
const { fileName, numberOfExecutions } = testCase;
const executionTimes = [];
for (let i = 0; i < numberOfExecutions; i++) {
const startTime = Date.now();
try {
exec(fileName, testCase);
} catch (error) {
console.error(
`Error running program in file ${fileName}: ${error.message}`,
);
return false;
}
executionTimes.push(Date.now() - startTime);
}
const averageTime = calculateAverageTime(executionTimes);
saveExecutionResults({ ...testCase, averageTime, executionTimes });
return true;
}
function executeBenchmark(options, files, testCases) {
const pairs = files.flatMap(fileName =>
testCases.map(testCase => ({ ...options, fileName, ...testCase })),
);
const allPassed = pairs.every((testCase, index) => {
process.stdout.write(
`Executing benchmark ${index + 1} of ${pairs.length} for ${
testCase.fileName
}... `,
);
return runBenchmarkTest(testCase);
});
return allPassed;
}
function __main__() {
try {
const argsSchema = { '-n': 'numberOfExecutions' };
const options = parseArgs(process.argv.slice(2), argsSchema);
if (!options.numberOfExecutions) {
throw new Error('Usage: node benchmarkRunner.js -n <numberOfExecutions>');
}
console.log(
`Starting benchmark execution with ${options.numberOfExecutions} executions each...`,
);
const allPassedInt = executeBenchmark(options, filesInt, testCasesInt);
const allPassedBigInt = executeBenchmark(
options,
filesBigInt,
testCasesBigInt,
);
if (allPassedInt && allPassedBigInt) {
console.log('All benchmarks completed successfully.');
process.exit(0);
} else {
console.error('Some benchmarks failed.');
process.exit(1);
}
} catch (error) {
console.error(error.message);
process.exit(1);
}
}
__main__();