@@ -4,10 +4,12 @@ import {BenchmarkType, Benchmark, benchmarks, fileName, LighthouseData} from './
4
4
import { setUseShadowRoot } from './webdriverAccess'
5
5
6
6
const lighthouse = require ( 'lighthouse' ) ;
7
+ const chromeLauncher = require ( 'chrome-launcher' ) ;
7
8
8
9
import { lhConfig } from './lighthouseConfig' ;
9
10
import * as fs from 'fs' ;
10
- import { JSONResult , config , FrameworkData , BenchmarkError , ErrorsAndWarning , BenchmarkOptions } from './common'
11
+ import * as path from 'path' ;
12
+ import { JSONResult , config , FrameworkData , BenchmarkError , ErrorsAndWarning , BenchmarkOptions , BenchmarkDriverOptions } from './common'
11
13
import * as R from 'ramda' ;
12
14
13
15
var chromedriver :any = require ( 'chromedriver' ) ;
@@ -94,32 +96,118 @@ function asString(res: Timingresult[]): string {
94
96
return res . reduce ( ( old , cur ) => old + "\n" + JSON . stringify ( cur ) , "" ) ;
95
97
}
96
98
97
- async function runLighthouse ( protocolResults : any [ ] ) : Promise < LighthouseData > {
98
- const traceEvents = protocolResults . filter ( e => e . method === 'Tracing.dataCollected' ) . map ( e => e . params ) ;
99
- const devtoolsLogs = protocolResults . filter ( e => e . method . startsWith ( 'Page' ) || e . method . startsWith ( 'Network' ) ) ;
100
-
101
- const filenamePrefix = `${ process . cwd ( ) } /${ Date . now ( ) } ` ;
102
- const traceFilename = `${ filenamePrefix } .trace.json` ;
103
- const devtoolslogsFilename = `${ filenamePrefix } .devtoolslogs.json` ;
104
-
105
- fs . writeFileSync ( traceFilename , JSON . stringify ( { traceEvents} ) ) ;
106
- fs . writeFileSync ( devtoolslogsFilename , JSON . stringify ( devtoolsLogs ) ) ;
107
- lhConfig . artifacts . traces = { defaultPass : traceFilename } ;
108
- lhConfig . artifacts . devtoolsLogs = { defaultPass : devtoolslogsFilename } ;
109
-
110
- const lhResults = await lighthouse ( 'http://example.com/thispage' , { } , lhConfig ) ;
111
- fs . unlinkSync ( traceFilename ) ;
112
- fs . unlinkSync ( devtoolslogsFilename ) ;
113
-
114
- const audits = lhResults . audits ;
115
- let LighthouseData : LighthouseData = {
116
- TimeToConsistentlyInteractive : 0 , // temporarily disabled due to #458 audits['consistently-interactive'].extendedInfo.value.timeInMs,
117
- ScriptBootUpTtime : audits [ 'bootup-time' ] . rawValue ,
118
- MainThreadWorkCost : audits [ 'mainthread-work-breakdown' ] . rawValue ,
119
- TotalByteWeight : audits [ 'total-byte-weight' ] . rawValue ,
99
+ // async function runLighthouse(protocolResults: any[]): Promise<LighthouseData> {
100
+ // const traceEvents = protocolResults.filter(e => e.method === 'Tracing.dataCollected').map(e => e.params);
101
+ // const devtoolsLogs = protocolResults.filter(e => e.method.startsWith('Page') || e.method.startsWith('Network'));
102
+
103
+ // const filenamePrefix = `${process.cwd()}/${Date.now()}`;
104
+ // const traceFilename = `${filenamePrefix}.trace.json`;
105
+ // const devtoolslogsFilename = `${filenamePrefix}.devtoolslogs.json`;
106
+
107
+ // fs.writeFileSync(traceFilename, JSON.stringify({traceEvents}));
108
+ // fs.writeFileSync(devtoolslogsFilename, JSON.stringify(devtoolsLogs));
109
+ // lhConfig.artifacts.traces = {defaultPass: traceFilename};
110
+ // lhConfig.artifacts.devtoolsLogs = {defaultPass: devtoolslogsFilename};
111
+
112
+ // const lhResults = await lighthouse('http://example.com/thispage', {}, lhConfig);
113
+ // fs.unlinkSync(traceFilename);
114
+ // fs.unlinkSync(devtoolslogsFilename);
115
+
116
+ // const audits = lhResults.audits;
117
+ // let LighthouseData: LighthouseData = {
118
+ // TimeToConsistentlyInteractive: 0, // temporarily disabled due to #458 audits['consistently-interactive'].extendedInfo.value.timeInMs,
119
+ // ScriptBootUpTtime: audits['bootup-time'].rawValue,
120
+ // MainThreadWorkCost: audits['mainthread-work-breakdown'].rawValue,
121
+ // TotalByteWeight: audits['total-byte-weight'].rawValue,
122
+ // };
123
+
124
+ // return LighthouseData;
125
+ // }
126
+
127
+ // async function computeResultsStartup(driver: WebDriver, framework: FrameworkData, benchmark: Benchmark, warnings: String[]): Promise<LighthouseData> {
128
+ // let durationJSArr : number[] = await driver.executeScript("return [window.performance.timing.loadEventEnd, window.performance.timing.navigationStart]") as number[];
129
+ // let durationJS = (durationJSArr[0] as number) - (durationJSArr[1] as number);
130
+ // let reportedDuration = durationJS;
131
+
132
+ // let entriesBrowser = await driver.manage().logs().get(logging.Type.BROWSER);
133
+ // if (config.LOG_DEBUG) console.log("browser entries", entriesBrowser);
134
+
135
+ // const perfLogEvents = (await fetchEventsFromPerformanceLog(driver));
136
+ // let filteredEvents = perfLogEvents.timingResults;
137
+ // let protocolResults = perfLogEvents.protocolResults;
138
+ // return runLighthouse(protocolResults);
139
+ // }
140
+
141
+
142
+ function extractRawValue ( results : any , id : string ) {
143
+ let audits = results . audits ;
144
+ if ( ! audits ) return null ;
145
+ let audit_with_id = audits [ id ] ;
146
+ if ( typeof audit_with_id === 'undefined' ) return null ;
147
+ if ( typeof audit_with_id . rawValue === 'undefined' ) return null ;
148
+ return audit_with_id . rawValue ;
149
+ }
150
+
151
+ function rmDir ( dirPath : string ) {
152
+ try { var files = fs . readdirSync ( dirPath ) ; }
153
+ catch ( e ) { return ; }
154
+ if ( files . length > 0 )
155
+ for ( var i = 0 ; i < files . length ; i ++ ) {
156
+ var filePath = path . join ( dirPath , files [ i ] ) ;
157
+ if ( fs . statSync ( filePath ) . isFile ( ) )
158
+ fs . unlinkSync ( filePath ) ;
159
+ else
160
+ rmDir ( filePath ) ;
161
+ }
162
+ fs . rmdirSync ( dirPath ) ;
163
+ } ;
164
+
165
+ async function runLighthouse ( framework : FrameworkData , benchmarkOptions : BenchmarkOptions ) : Promise < LighthouseData > {
166
+ const opts = {
167
+ chromeFlags :
168
+ [
169
+ "--headless" ,
170
+ "--no-sandbox" ,
171
+ "--no-first-run" ,
172
+ "--enable-automation" ,
173
+ "--disable-infobars" ,
174
+ "--disable-background-networking" ,
175
+ "--disable-background-timer-throttling" ,
176
+ "--disable-cache" ,
177
+ "--disable-translate" ,
178
+ "--disable-sync" ,
179
+ "--disable-extensions" ,
180
+ "--disable-default-apps" ,
181
+ "--window-size=1200,800"
182
+ ] ,
183
+ onlyCategories : [ 'performance' ] ,
184
+ port : ''
120
185
} ;
121
186
122
- return LighthouseData ;
187
+ try {
188
+ if ( fs . existsSync ( 'prefs' ) ) rmDir ( 'prefs' ) ;
189
+ fs . mkdirSync ( 'prefs' ) ;
190
+ fs . mkdirSync ( 'prefs/Default' ) ;
191
+ fs . copyFileSync ( 'chromePreferences.json' , 'prefs/Default/Preferences' ) ;
192
+
193
+ let options : any = { chromeFlags : opts . chromeFlags , logLevel : "verbose" , userDataDir : "prefs" } ;
194
+ if ( benchmarkOptions . chromeBinaryPath ) options . chromePath = benchmarkOptions . chromeBinaryPath ;
195
+ let chrome = await chromeLauncher . launch ( options ) ;
196
+ opts . port = chrome . port ;
197
+ let results = await lighthouse ( `http://localhost:${ benchmarkOptions . port } /${ framework . uri } /` , opts , null ) ;
198
+ await chrome . kill ( ) ;
199
+
200
+ let LighthouseData : LighthouseData = {
201
+ TimeToConsistentlyInteractive : extractRawValue ( results . lhr , 'interactive' ) ,
202
+ ScriptBootUpTtime : extractRawValue ( results . lhr , 'bootup-time' ) ,
203
+ MainThreadWorkCost : extractRawValue ( results . lhr , 'mainthread-work-breakdown' ) ,
204
+ TotalByteWeight : extractRawValue ( results . lhr , 'total-byte-weight' )
205
+ } ;
206
+ return LighthouseData ;
207
+ } catch ( error ) {
208
+ console . log ( "error running lighthouse" , error ) ;
209
+ throw error ;
210
+ }
123
211
}
124
212
125
213
async function computeResultsCPU ( driver : WebDriver , benchmarkOptions : BenchmarkOptions , framework : FrameworkData , benchmark : Benchmark , warnings : String [ ] ) : Promise < number [ ] > {
@@ -222,21 +310,7 @@ async function computeResultsMEM(driver: WebDriver, benchmarkOptions: BenchmarkO
222
310
return results ;
223
311
}
224
312
225
- async function computeResultsStartup ( driver : WebDriver , framework : FrameworkData , benchmark : Benchmark , warnings : String [ ] ) : Promise < LighthouseData > {
226
- let durationJSArr : number [ ] = await driver . executeScript ( "return [window.performance.timing.loadEventEnd, window.performance.timing.navigationStart]" ) as number [ ] ;
227
- let durationJS = ( durationJSArr [ 0 ] as number ) - ( durationJSArr [ 1 ] as number ) ;
228
- let reportedDuration = durationJS ;
229
-
230
- let entriesBrowser = await driver . manage ( ) . logs ( ) . get ( logging . Type . BROWSER ) ;
231
- if ( config . LOG_DEBUG ) console . log ( "browser entries" , entriesBrowser ) ;
232
-
233
- const perfLogEvents = ( await fetchEventsFromPerformanceLog ( driver ) ) ;
234
- let filteredEvents = perfLogEvents . timingResults ;
235
- let protocolResults = perfLogEvents . protocolResults ;
236
- return runLighthouse ( protocolResults ) ;
237
- }
238
-
239
- function buildDriver ( benchmarkOptions : BenchmarkOptions ) {
313
+ function buildDriver ( benchmarkOptions : BenchmarkDriverOptions ) {
240
314
let logPref = new logging . Preferences ( ) ;
241
315
logPref . setLevel ( logging . Type . PERFORMANCE , logging . Level . ALL ) ;
242
316
logPref . setLevel ( logging . Type . BROWSER , logging . Level . ALL ) ;
@@ -418,39 +492,53 @@ async function runMemOrCPUBenchmark(framework: FrameworkData, benchmark: Benchma
418
492
async function runStartupBenchmark ( framework : FrameworkData , benchmark : Benchmark , benchmarkOptions : BenchmarkOptions ) : Promise < ErrorsAndWarning >
419
493
{
420
494
console . log ( "benchmarking startup" , framework , benchmark . id ) ;
421
- let results : LighthouseData [ ] = [ ] ;
422
- let errors : BenchmarkError [ ] = [ ] ;
423
- let warnings : String [ ] = [ ] ;
424
495
425
- let chromeDuration = 0 ;
426
- try {
427
- for ( let i = 0 ; i < benchmarkOptions . numIterationsForAllBenchmarks ; i ++ ) {
428
- let driver = buildDriver ( benchmarkOptions ) ;
429
- try {
430
- setUseShadowRoot ( framework . useShadowRoot ) ;
431
- await driver . executeScript ( "console.timeStamp('initBenchmark')" ) ;
432
- await initBenchmark ( driver , benchmark , framework ) ;
433
- await driver . executeScript ( "console.timeStamp('runBenchmark')" ) ;
434
- await runBenchmark ( driver , benchmark , framework ) ;
435
- await driver . executeScript ( "console.timeStamp('finishedBenchmark')" ) ;
436
- await afterBenchmark ( driver , benchmark , framework ) ;
437
- await driver . executeScript ( "console.timeStamp('afterBenchmark')" ) ;
438
- await wait ( 5000 ) ;
439
- results . push ( await computeResultsStartup ( driver , framework , benchmark , warnings ) ) ;
440
- } catch ( e ) {
441
- errors . push ( await registerError ( driver , framework , benchmark , e ) ) ;
442
- throw e ;
443
- } finally {
444
- await driver . close ( ) ;
445
- await driver . quit ( ) ;
446
- }
496
+ let errors : BenchmarkError [ ] = [ ] ;
497
+ let results : LighthouseData [ ] = [ ] ;
498
+ for ( let i = 0 ; i < benchmarkOptions . numIterationsForAllBenchmarks ; i ++ ) {
499
+ try {
500
+ results . push ( await runLighthouse ( framework , benchmarkOptions ) ) ;
501
+ } catch ( error ) {
502
+ errors . push ( { imageFile : null , exception : error } ) ;
503
+ throw error ;
447
504
}
448
- await writeResult ( { framework : framework , results : results , benchmark : benchmark } , benchmarkOptions . outputDirectory ) ;
449
- } catch ( e ) {
450
- console . log ( "ERROR:" , e ) ;
451
- if ( config . EXIT_ON_ERROR ) { throw "Benchmarking failed" }
452
505
}
453
- return { errors, warnings} ;
506
+ await writeResult ( { framework : framework , results : results , benchmark : benchmark } , benchmarkOptions . outputDirectory ) ;
507
+ return { errors, warnings : [ ] } ;
508
+
509
+ // let results : LighthouseData[] = [];
510
+ // let errors: BenchmarkError[] = [];
511
+ // let warnings: String[] = [];
512
+
513
+ // let chromeDuration = 0;
514
+ // try {
515
+ // for (let i = 0; i<benchmarkOptions.numIterationsForAllBenchmarks; i++) {
516
+ // let driver = buildDriver(benchmarkOptions);
517
+ // try {
518
+ // setUseShadowRoot(framework.useShadowRoot);
519
+ // await driver.executeScript("console.timeStamp('initBenchmark')");
520
+ // await initBenchmark(driver, benchmark, framework);
521
+ // await driver.executeScript("console.timeStamp('runBenchmark')");
522
+ // await runBenchmark(driver, benchmark, framework);
523
+ // await driver.executeScript("console.timeStamp('finishedBenchmark')");
524
+ // await afterBenchmark(driver, benchmark, framework);
525
+ // await driver.executeScript("console.timeStamp('afterBenchmark')");
526
+ // await wait(5000);
527
+ // results.push(await computeResultsStartup(driver, framework, benchmark, warnings));
528
+ // } catch (e) {
529
+ // errors.push(await registerError(driver, framework, benchmark, e));
530
+ // throw e;
531
+ // } finally {
532
+ // await driver.close();
533
+ // await driver.quit();
534
+ // }
535
+ // }
536
+ // await writeResult({framework: framework, results: results, benchmark: benchmark}, benchmarkOptions.outputDirectory);
537
+ // } catch (e) {
538
+ // console.log("ERROR:", e);
539
+ // if (config.EXIT_ON_ERROR) { throw "Benchmarking failed"}
540
+ // }
541
+ // return {errors, warnings};
454
542
}
455
543
456
544
export async function executeBenchmark ( frameworks : FrameworkData [ ] , keyed : boolean , frameworkName : string , benchmarkName : string , benchmarkOptions : BenchmarkOptions ) : Promise < ErrorsAndWarning > {
0 commit comments