Skip to content

Commit 59af8c2

Browse files
authored
[browserstack benchmark tool] Use stream to write to outfile (#6750)
FEATURE * write * Update app.js * Update app.js
1 parent 167fd6f commit 59af8c2

File tree

2 files changed

+80
-47
lines changed

2 files changed

+80
-47
lines changed

e2e/benchmarks/browserstack-benchmark/app.js

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const {
3030
endFirebaseInstance
3131
} = require('./firestore.js');
3232
const {PromiseQueue} = require('./promise_queue');
33+
const JSONStream = require('JSONStream');
3334

35+
const jsonwriter = JSONStream.stringify();
3436
const port = process.env.PORT || 8001;
3537
let io;
3638
let parser;
@@ -195,18 +197,16 @@ async function benchmark(config, runOneBenchmark = getOneBenchmarkResult) {
195197
// Optionally written to an outfile or pushed to a database once all
196198
// benchmarks return results
197199
const fulfilled = await Promise.allSettled(results);
198-
if (cliArgs?.outfile === 'html') {
199-
await write(
200-
'./benchmark_results.js',
201-
`let benchmarkResults = ${JSON.stringify(fulfilled)};`);
202-
} else if (cliArgs?.outfile === 'json') {
203-
await write('./benchmark_results.json', JSON.stringify(fulfilled));
204-
} else {
205-
console.log('\Benchmarks complete.\n');
200+
if (cliArgs?.outfile === 'html' || cliArgs?.outfile === 'json') {
201+
for (const benchmarkResult of fulfilled) {
202+
jsonwriter.write(benchmarkResult);
203+
}
206204
}
207205
if (cliArgs?.firestore) {
208206
await pushToFirestore(fulfilled);
209207
}
208+
console.log(
209+
`\n${config.benchmark?.model} model benchmark over ${config.benchmark?.backend} backend complete.\n`);
210210
return fulfilled;
211211
}
212212

@@ -301,26 +301,6 @@ function runBrowserStackBenchmark(tabId) {
301301
});
302302
}
303303

304-
/**
305-
* Writes a passed message to a passed JSON file.
306-
*
307-
* @param filePath Relative filepath of target file
308-
* @param msg Message to be written
309-
*/
310-
function write(filePath, msg) {
311-
return new Promise((resolve, reject) => {
312-
fs.writeFile(filePath, msg, 'utf8', err => {
313-
if (err) {
314-
console.log(`Error: ${err}.`);
315-
return reject(err);
316-
} else {
317-
console.log(`\nOutput written to ${filePath}.`);
318-
return resolve();
319-
}
320-
});
321-
})
322-
}
323-
324304
/**
325305
* Pushes all benchmark results to Firestore.
326306
*
@@ -405,34 +385,85 @@ function setupHelpMessage() {
405385
* @param file Relative filepath to preset benchmark configuration
406386
* @param runBenchmark Function to run a benchmark configuration
407387
*/
408-
function runBenchmarkFromFile(file, runBenchmark = benchmarkAll) {
388+
async function runBenchmarkFromFile(file, runBenchmark = benchmarkAll) {
409389
console.log('Running a preconfigured benchmark...');
410-
runBenchmark(file);
390+
await runBenchmark(file);
411391
}
412392

413-
/** Sets up the local or remote environment for benchmarking. */
414-
async function prebenchmarkSetup() {
415-
checkBrowserStackAccount();
393+
async function initializeWriting() {
416394
if (cliArgs.firestore) {
417395
db = await runFirestore(firebaseConfig)
418396
};
397+
398+
let file;
399+
if (cliArgs?.outfile === 'html') {
400+
await fs.writeFile(
401+
'./benchmark_results.js', 'const benchmarkResults = ', 'utf8', err => {
402+
if (err) {
403+
console.log(`Error: ${err}.`);
404+
return reject(err);
405+
} else {
406+
return resolve();
407+
}
408+
});
409+
file = fs.createWriteStream('benchmark_results.js', {'flags': 'a'});
410+
} else if (cliArgs?.outfile === 'json') {
411+
file = fs.createWriteStream('./benchmark_results.json');
412+
} else {
413+
return;
414+
}
415+
416+
// Pipe the JSON data to the file.
417+
jsonwriter.pipe(file);
418+
console.log(`\nStart writing.`);
419+
420+
// If having outfile, add a listener to Ctrl+C to finalize writing.
421+
process.on('SIGINT', async () => {
422+
await finalizeWriting();
423+
process.exit();
424+
});
425+
}
426+
427+
428+
async function finalizeWriting() {
429+
if (cliArgs.firestore) {
430+
await endFirebaseInstance();
431+
}
432+
433+
if (cliArgs?.outfile === 'html') {
434+
jsonwriter.end();
435+
console.log('\nOutput written to ./benchmark_results.js.');
436+
} else if (cliArgs?.outfile === 'json') {
437+
jsonwriter.end();
438+
console.log('\nOutput written to ./benchmark_results.json.');
439+
}
440+
}
441+
442+
/** Sets up the local or remote environment for benchmarking. */
443+
async function prebenchmarkSetup() {
444+
checkBrowserStackAccount();
445+
await initializeWriting();
446+
419447
if (!cliArgs.cloud) {
420448
runServer()
421449
};
422-
if (cliArgs.benchmarks) {
423-
const filePath = resolve(cliArgs.benchmarks);
424-
if (fs.existsSync(filePath)) {
425-
console.log(`\nFound file at ${filePath}`);
426-
const config = require(filePath);
427-
runBenchmarkFromFile(config);
428-
} else {
429-
throw new Error(
430-
`File could not be found at ${filePath}. ` +
431-
`Please provide a valid path.`);
450+
451+
try {
452+
if (cliArgs.benchmarks) {
453+
const filePath = resolve(cliArgs.benchmarks);
454+
if (fs.existsSync(filePath)) {
455+
console.log(`\nFound file at ${filePath}`);
456+
const config = require(filePath);
457+
await runBenchmarkFromFile(config);
458+
console.log('finish')
459+
} else {
460+
throw new Error(
461+
`File could not be found at ${filePath}. ` +
462+
`Please provide a valid path.`);
463+
}
432464
}
433-
}
434-
if (cliArgs.firestore) {
435-
endFirebaseInstance();
465+
} finally {
466+
finalizeWriting();
436467
}
437468
}
438469

@@ -445,4 +476,3 @@ if (require.main === module) {
445476
exports.runBenchmarkFromFile = runBenchmarkFromFile;
446477
exports.getOneBenchmarkResult = getOneBenchmarkResult;
447478
exports.benchmark = benchmark;
448-
exports.write = write;

e2e/benchmarks/browserstack-benchmark/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@
4242
"resolutions": {
4343
"node-fetch": "2.6.7",
4444
"minimist": "1.2.6"
45+
},
46+
"dependencies": {
47+
"JSONStream": "^1.3.5"
4548
}
4649
}

0 commit comments

Comments
 (0)