From 7ca2c8a9a79c16c0791ef1fd354a15d726f1cfb5 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 18 Dec 2021 11:44:01 -0500 Subject: [PATCH] Make loadBenchmarkDataset() compatible with more recent requests.json The format of requests.json used in latest Cliqz benchmark code has changed from the original one -- this commit makes the dataset load code also compatible with the new format. More recent dataset used in Cliqz benchmark code: - https://github.com/mjethani/scaling-palm-tree Cliqz benchmark code: - https://github.com/ghostery/adblocker/tree/master/packages/adblocker-benchmarks --- src/js/benchmarks.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/js/benchmarks.js b/src/js/benchmarks.js index 527f5755a..54a98fcb2 100644 --- a/src/js/benchmarks.js +++ b/src/js/benchmarks.js @@ -94,14 +94,30 @@ const loadBenchmarkDataset = (( ) => { console.info(`Loading benchmark dataset...`); datasetPromise = io.fetchText(datasetURL).then(details => { console.info(`Parsing benchmark dataset...`); - const requests = []; - const lineIter = new LineIterator(details.content); - while ( lineIter.eot() === false ) { - let request; + let requests = []; + if ( details.content.startsWith('[') ) { try { - request = JSON.parse(lineIter.next()); + requests = JSON.parse(details.content); } catch(ex) { } + } else { + const lineIter = new LineIterator(details.content); + const parsed = []; + while ( lineIter.eot() === false ) { + const line = lineIter.next().trim(); + if ( line === '' ) { continue; } + try { + parsed.push(JSON.parse(line)); + } catch(ex) { + parsed.length = 0; + break; + } + } + requests = parsed; + } + if ( requests.length === 0 ) { return; } + const out = []; + for ( const request of requests ) { if ( request instanceof Object === false ) { continue; } if ( !request.frameUrl || !request.url ) { continue; } if ( request.cpt === 'document' ) { @@ -109,9 +125,9 @@ const loadBenchmarkDataset = (( ) => { } else if ( request.cpt === 'xhr' ) { request.cpt = 'xmlhttprequest'; } - requests.push(request); + out.push(request); } - return requests; + return out; }).catch(details => { console.info(`Not found: ${details.url}`); datasetPromise = undefined;