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AG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;gBAC1B,KAAK,GAAG,IAAI,CAAC;aACd;iBAAM,IAAI,MAAM,GAAG,CAAC,CAAC,EAAE;gBACtB,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;gBACzB,KAAK,GAAG,IAAI,CAAC;aACd;iBAAM,IAAI,MAAM,GAAG,CAAC,CAAC,EAAE;gBACtB,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;gBACzB,KAAK,GAAG,IAAI,CAAC;aACd;iBAAM,IAAI,MAAM,GAAG,CAAC,EAAE;gBACrB,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;gBACzB,KAAK,GAAG,IAAI,CAAC;aACd;SACF;QAED,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;IAClC,CAAC","sourcesContent":["import { readFileSync, writeFileSync } from 'fs';\nimport * as Benchmark from 'benchmark';\nimport { Executor } from '../executors/Executor';\nimport Reporter, { eventHandler, ReporterProperties } from './Reporter';\nimport BenchmarkTest from '../BenchmarkTest';\nimport Test from '../Test';\nimport Suite from '../Suite';\n\n/**\n * Benchmark is a reporter that can generate a baseline report and do runtime\n * comparisons against an existing baseline.\n *\n * **Configuration**\n *\n * Along with the default reporter options, Benchmark also supports a `mode`\n * option. This can have two values:\n *\n * * `'baseline'`: Benchmark data will be written to a baseline file when\n * testing is finished\n * * `'test'`: Benchmark is compared to a baseline read from a file when testing\n * starts\n *\n * Baseline data is stored hierarchically by environment and then by test.\n *\n * **Notation**\n *\n * * **rme:** relative margin of error -- margin of error as a percentage of the\n * mean margin of error\n * * **mean:** mean execution time per function run\n * * **hz:** Hertz (number of executions of a function per second). 1/Hz is the\n * mean execution time of function.\n */\nexport default class BenchmarkReporter extends Reporter\n implements BenchmarkReporterProperties {\n baseline!: BenchmarkBaseline;\n\n filename: string;\n\n mode: BenchmarkMode;\n\n sessions: { [sessionId: string]: SessionInfo };\n\n thresholds: BenchmarkThresholds;\n\n constructor(executor: Executor, options: BenchmarkReporterOptions = {}) {\n super(executor, options);\n\n this.mode = options.mode || 'test';\n this.filename = options.filename || '';\n this.thresholds = options.thresholds || {};\n\n // In test mode, try to load benchmark data for comparison\n if (this.mode === 'test') {\n try {\n this.baseline = JSON.parse(\n readFileSync(this.filename, { encoding: 'utf8' })\n );\n } catch (error) {\n this.console.warn(\n 'Unable to load benchmark baseline data from ' + this.filename\n );\n this.console.warn('Switching to \"baseline\" mode');\n this.mode = 'baseline';\n }\n }\n\n if (!this.baseline) {\n this.baseline = {};\n }\n\n // Cache environments by session ID so we can look them up again when\n // serialized tests come back from remote browsers\n this.sessions = {};\n }\n\n _getSession(testOrSuite: Test | Suite) {\n const sessionId = testOrSuite.sessionId || 'local';\n let session = this.sessions[sessionId];\n\n if (!session) {\n let client: string;\n let version: string;\n let platform: string;\n\n if (testOrSuite.sessionId) {\n const environmentType = testOrSuite.remote.environmentType!;\n client = environmentType.browserName!;\n version = environmentType.version!;\n platform = environmentType.platform!;\n } else {\n client = process.title;\n version = process.version;\n platform = process.platform;\n }\n\n session = this.sessions[sessionId] = {\n suites: {},\n environment: {\n client,\n version,\n platform,\n id: client + ':' + version + ':' + platform\n }\n };\n }\n\n return session;\n }\n\n @eventHandler()\n runEnd() {\n if (this.mode === 'baseline') {\n let existingBaseline: BenchmarkBaseline;\n try {\n existingBaseline = JSON.parse(\n readFileSync(this.filename, { encoding: 'utf8' })\n );\n } catch (error) {\n existingBaseline = {};\n }\n\n // Merge the newly recorded baseline data into the existing baseline\n // data and write it back out to output file.\n const baseline = this.baseline;\n Object.keys(baseline).forEach(function(environmentId) {\n existingBaseline[environmentId] = baseline[environmentId];\n });\n writeFileSync(\n this.filename,\n JSON.stringify(existingBaseline, null, ' ')\n );\n }\n }\n\n @eventHandler()\n suiteEnd(suite: Suite) {\n const session = this._getSession(suite);\n\n if (!suite.hasParent) {\n const environment = session.environment;\n this.console.log(\n 'Finished benchmarking ' +\n environment.client +\n ' ' +\n environment.version +\n ' on ' +\n environment.platform\n );\n } else if (this.mode === 'test') {\n const suiteInfo = session.suites[suite.id];\n const numTests = suiteInfo.numBenchmarks;\n if (numTests > 0) {\n const numFailedTests = suiteInfo.numFailedBenchmarks;\n const message =\n numFailedTests + '/' + numTests + ' benchmarks failed in ' + suite.id;\n if (numFailedTests > 0) {\n this.console.warn(message);\n } else {\n this.console.log(message);\n }\n }\n }\n }\n\n @eventHandler()\n suiteStart(suite: Suite) {\n const session = this._getSession(suite);\n\n // This is a session root suite\n if (!suite.hasParent) {\n const environment = session.environment;\n const environmentName =\n environment.client +\n ' ' +\n environment.version +\n ' on ' +\n environment.platform;\n const baselineEnvironments = this.baseline;\n\n this.console.log(\n (this.mode === 'baseline' ? 'Baselining' : 'Benchmarking') +\n ' ' +\n environmentName\n );\n\n if (this.mode === 'baseline') {\n baselineEnvironments[environment.id] = {\n client: environment.client,\n version: environment.version,\n platform: environment.platform,\n tests: {}\n };\n } else if (!baselineEnvironments[environment.id]) {\n this.console.warn('No baseline data for ' + environmentName + '!');\n }\n } else {\n session.suites[suite.id] = {\n numBenchmarks: 0,\n numFailedBenchmarks: 0\n };\n }\n }\n\n @eventHandler()\n testEnd(test: Test) {\n const benchmarkTest = <BenchmarkTest>test;\n\n // Just check for the benchmark property because the test may be a\n // deserialized object rather than an actual BenchmarkTest instance.\n if (benchmarkTest.benchmark == null) {\n return;\n }\n\n if (benchmarkTest.error) {\n const session = this._getSession(benchmarkTest);\n const suiteInfo = session.suites[benchmarkTest.parentId];\n suiteInfo.numBenchmarks++;\n suiteInfo.numFailedBenchmarks++;\n\n this.console.error('FAIL: ' + benchmarkTest.id);\n this.console.error(\n this.executor.formatError(benchmarkTest.error, { space: ' ' })\n );\n } else {\n const checkTest = (baseline: BenchmarkData, benchmark: BenchmarkData) => {\n let warn: string[] = [];\n let fail: string[] = [];\n let list: string[];\n\n const baselineMean = baseline.stats.mean;\n const thresholds = this.thresholds || {};\n let percentDifference =\n (100 * (benchmark.stats.mean - baselineMean)) / baselineMean;\n\n if (\n thresholds.warn &&\n thresholds.warn.mean &&\n Math.abs(percentDifference) > thresholds.warn.mean\n ) {\n list = warn;\n if (\n thresholds.fail &&\n thresholds.fail.mean &&\n Math.abs(percentDifference) > thresholds.fail.mean\n ) {\n list = fail;\n }\n list.push(\n 'Execution time is ' + percentDifference.toFixed(1) + '% off'\n );\n }\n\n const baselineRme = baseline.stats.rme;\n // RME is already a percent\n percentDifference = benchmark.stats.rme - baselineRme;\n if (\n thresholds.warn &&\n thresholds.warn.rme &&\n Math.abs(percentDifference) > thresholds.warn.rme\n ) {\n list = warn;\n if (\n thresholds.fail &&\n thresholds.fail.rme &&\n Math.abs(percentDifference) > thresholds.fail.rme\n ) {\n list = fail;\n }\n list.push('RME is ' + percentDifference.toFixed(1) + '% off');\n }\n\n if (fail.length) {\n this.console.error(\n 'FAIL ' + benchmarkTest.id + ' (' + fail.join(', ') + ')'\n );\n return false;\n } else if (warn.length) {\n this.console.warn(\n 'WARN ' + benchmarkTest.id + ' (' + warn.join(', ') + ')'\n );\n } else {\n this.console.log('PASS ' + benchmarkTest.id);\n }\n\n return true;\n };\n\n const benchmark = benchmarkTest.benchmark;\n const session = this._getSession(benchmarkTest);\n const environment = session.environment;\n\n const suiteInfo = session.suites[benchmarkTest.parentId];\n suiteInfo.numBenchmarks++;\n\n const baseline = this.baseline[environment.id]!;\n\n if (this.mode === 'baseline') {\n baseline.tests[benchmarkTest.id] = {\n hz: benchmark.hz,\n times: benchmark.times,\n stats: {\n rme: benchmark.stats.rme,\n moe: benchmark.stats.moe,\n mean: benchmark.stats.mean\n }\n };\n this.console.log('Baselined ' + benchmarkTest.name);\n this.executor.log(\n 'Time per run:',\n formatSeconds(benchmark.stats.mean),\n '\\xb1',\n benchmark.stats.rme.toFixed(2),\n '%'\n );\n } else {\n if (baseline) {\n const testData = baseline.tests[benchmarkTest.id];\n const result = checkTest(testData, benchmark);\n const baselineStats = testData.stats;\n const benchmarkStats = benchmark.stats;\n this.executor.log(\n 'Expected time per run:',\n formatSeconds(baselineStats.mean),\n '\\xb1',\n baselineStats.rme.toFixed(2),\n '%'\n );\n this.executor.log(\n 'Actual time per run:',\n formatSeconds(benchmarkStats.mean),\n '\\xb1',\n benchmarkStats.rme.toFixed(2),\n '%'\n );\n\n if (!result) {\n suiteInfo.numFailedBenchmarks++;\n }\n }\n }\n }\n }\n}\n\nexport interface BenchmarkData {\n times: Benchmark.Times;\n hz: number;\n stats: {\n rme: number;\n moe: number;\n mean: number;\n };\n}\n\nexport interface BenchmarkThresholds {\n warn?: {\n rme?: number;\n hz?: number;\n mean?: number;\n };\n fail?: {\n rme?: number;\n hz?: number;\n mean?: number;\n };\n}\n\nexport interface BaselineEnvironment {\n client: string;\n version: string;\n platform: string;\n tests: { [testId: string]: BenchmarkData };\n}\n\nexport interface BenchmarkBaseline {\n [key: string]: BaselineEnvironment;\n}\n\nexport interface SesssionEnvironment {\n client: string;\n version: string;\n platform: string;\n id: string;\n}\n\nexport interface SessionInfo {\n suites: {\n [suiteId: string]: {\n numBenchmarks: number;\n numFailedBenchmarks: number;\n };\n };\n environment: SesssionEnvironment;\n}\n\nexport type BenchmarkMode = 'baseline' | 'test';\n\nexport interface BenchmarkReporterProperties extends ReporterProperties {\n filename: string;\n mode: BenchmarkMode;\n thresholds: BenchmarkThresholds;\n}\n\nexport type BenchmarkReporterOptions = Partial<BenchmarkReporterProperties>;\n\nfunction formatSeconds(value: number) {\n if (value == null) {\n return null;\n }\n\n let units = 's';\n if (value < 1) {\n const places = Math.ceil(Math.log(value) / Math.log(10)) - 1;\n if (places < -9) {\n value *= Math.pow(10, 12);\n units = 'ps';\n } else if (places < -6) {\n value *= Math.pow(10, 9);\n units = 'ns';\n } else if (places < -3) {\n value *= Math.pow(10, 6);\n units = 'µs';\n } else if (places < 0) {\n value *= Math.pow(10, 3);\n units = 'ms';\n }\n }\n\n return value.toFixed(3) + units;\n}\n"]} |