fix(benchpress): Update types for TypeScript nullability support

This commit is contained in:
Miško Hevery
2017-03-24 09:56:50 -07:00
committed by Hans
parent 075f3f8c9f
commit 14669f20bf
17 changed files with 70 additions and 69 deletions

View File

@ -16,7 +16,7 @@ export function convertPerfProfileToEvents(perfProfile: any): any[] {
const finishedEvents: {[key: string]: any}[] = []; // Event[] finished events
const addFinishedEvent = function(eventName: string, startTime: number, endTime: number) {
const categorizedEventName = categorizeEvent(eventName);
let args: {[key: string]: any} = undefined;
let args: {[key: string]: any}|undefined = undefined;
if (categorizedEventName == 'gc') {
// TODO: We cannot measure heap size at the moment
args = {usedHeapSize: 0};

View File

@ -140,11 +140,11 @@ export class PerflogMetric extends Metric {
const markName = this._markName(this._measureCount - 1);
const nextMarkName = restart ? this._markName(this._measureCount++) : null;
return this._driverExtension.timeEnd(markName, nextMarkName)
.then((_) => this._readUntilEndMark(markName));
.then((_: any) => this._readUntilEndMark(markName));
}
private _readUntilEndMark(
markName: string, loopCount: number = 0, startEvent: PerfLogEvent = null) {
markName: string, loopCount: number = 0, startEvent: PerfLogEvent|null = null) {
if (loopCount > _MAX_RETRY_COUNT) {
throw new Error(`Tried too often to get the ending mark: ${loopCount}`);
}
@ -175,7 +175,7 @@ export class PerflogMetric extends Metric {
}
startEvent['ph'] = 'B';
endEvent['ph'] = 'E';
endEvent['ts'] = startEvent['ts'] + startEvent['dur'];
endEvent['ts'] = startEvent['ts'] ! + startEvent['dur'] !;
this._remainingEvents.push(startEvent);
this._remainingEvents.push(endEvent);
} else {
@ -185,13 +185,13 @@ export class PerflogMetric extends Metric {
if (needSort) {
// Need to sort because of the ph==='X' events
this._remainingEvents.sort((a, b) => {
const diff = a['ts'] - b['ts'];
const diff = a['ts'] ! - b['ts'] !;
return diff > 0 ? 1 : diff < 0 ? -1 : 0;
});
}
}
private _aggregateEvents(events: PerfLogEvent[], markName: string): {[key: string]: number} {
private _aggregateEvents(events: PerfLogEvent[], markName: string): {[key: string]: number}|null {
const result: {[key: string]: number} = {'scriptTime': 0, 'pureScriptTime': 0};
if (this._perfLogFeatures.gc) {
result['gcTime'] = 0;
@ -217,8 +217,8 @@ export class PerflogMetric extends Metric {
result['requestCount'] = 0;
}
let markStartEvent: PerfLogEvent = null;
let markEndEvent: PerfLogEvent = null;
let markStartEvent: PerfLogEvent = null !;
let markEndEvent: PerfLogEvent = null !;
events.forEach((event) => {
const ph = event['ph'];
const name = event['name'];
@ -242,8 +242,8 @@ export class PerflogMetric extends Metric {
const frameTimestamps: number[] = [];
const frameTimes: number[] = [];
let frameCaptureStartEvent: PerfLogEvent = null;
let frameCaptureEndEvent: PerfLogEvent = null;
let frameCaptureStartEvent: PerfLogEvent|null = null;
let frameCaptureEndEvent: PerfLogEvent|null = null;
const intervalStarts: {[key: string]: PerfLogEvent} = {};
const intervalStartCount: {[key: string]: number} = {};
@ -251,7 +251,7 @@ export class PerflogMetric extends Metric {
let inMeasureRange = false;
events.forEach((event) => {
const ph = event['ph'];
let name = event['name'];
let name = event['name'] !;
let microIterations = 1;
const microIterationsMatch = name.match(_MICRO_ITERATIONS_REGEX);
if (microIterationsMatch) {
@ -270,7 +270,7 @@ export class PerflogMetric extends Metric {
if (this._requestCount && name === 'sendRequest') {
result['requestCount'] += 1;
} else if (this._receivedData && name === 'receivedData' && ph === 'I') {
result['receivedData'] += event['args']['encodedDataLength'];
result['receivedData'] += event['args'] !['encodedDataLength'] !;
}
if (ph === 'B' && name === _MARK_NAME_FRAME_CAPTURE) {
if (frameCaptureStartEvent) {
@ -289,7 +289,7 @@ export class PerflogMetric extends Metric {
}
if (ph === 'I' && frameCaptureStartEvent && !frameCaptureEndEvent && name === 'frame') {
frameTimestamps.push(event['ts']);
frameTimestamps.push(event['ts'] !);
if (frameTimestamps.length >= 2) {
frameTimes.push(
frameTimestamps[frameTimestamps.length - 1] -
@ -308,14 +308,14 @@ export class PerflogMetric extends Metric {
intervalStartCount[name]--;
if (intervalStartCount[name] === 0) {
const startEvent = intervalStarts[name];
const duration = (event['ts'] - startEvent['ts']);
intervalStarts[name] = null;
const duration = (event['ts'] ! - startEvent['ts'] !);
intervalStarts[name] = null !;
if (name === 'gc') {
result['gcTime'] += duration;
const amount =
(startEvent['args']['usedHeapSize'] - event['args']['usedHeapSize']) / 1000;
(startEvent['args'] !['usedHeapSize'] ! - event['args'] !['usedHeapSize'] !) / 1000;
result['gcAmount'] += amount;
const majorGc = event['args']['majorGc'];
const majorGc = event['args'] !['majorGc'];
if (majorGc && majorGc) {
result['majorGcTime'] += duration;
}

View File

@ -48,7 +48,7 @@ export class Sampler {
}
private _iterate(lastState: SampleState): Promise<SampleState> {
let resultPromise: Promise<SampleState>;
let resultPromise: Promise<SampleState|null>;
if (this._prepare !== Options.NO_PREPARE) {
resultPromise = this._driver.waitFor(this._prepare);
} else {
@ -76,5 +76,5 @@ export class Sampler {
}
export class SampleState {
constructor(public completeSample: MeasureValues[], public validSample: MeasureValues[]) {}
constructor(public completeSample: MeasureValues[], public validSample: MeasureValues[]|null) {}
}

View File

@ -17,7 +17,7 @@ export abstract class Validator {
/**
* Calculates a valid sample out of the complete sample
*/
validate(completeSample: MeasureValues[]): MeasureValues[] { throw new Error('NYI'); }
validate(completeSample: MeasureValues[]): MeasureValues[]|null { throw new Error('NYI'); }
/**
* Returns a Map that describes the properties of the validator

View File

@ -35,7 +35,7 @@ export class RegressionSlopeValidator extends Validator {
return {'sampleSize': this._sampleSize, 'regressionSlopeMetric': this._metric};
}
validate(completeSample: MeasureValues[]): MeasureValues[] {
validate(completeSample: MeasureValues[]): MeasureValues[]|null {
if (completeSample.length >= this._sampleSize) {
const latestSample =
completeSample.slice(completeSample.length - this._sampleSize, completeSample.length);

View File

@ -23,7 +23,7 @@ export class SizeValidator extends Validator {
describe(): {[key: string]: any} { return {'sampleSize': this._sampleSize}; }
validate(completeSample: MeasureValues[]): MeasureValues[] {
validate(completeSample: MeasureValues[]): MeasureValues[]|null {
if (completeSample.length >= this._sampleSize) {
return completeSample.slice(completeSample.length - this._sampleSize, completeSample.length);
} else {

View File

@ -43,7 +43,7 @@ export abstract class WebDriverExtension {
{
provide: WebDriverExtension,
useFactory: (children: WebDriverExtension[], capabilities: {[key: string]: any}) => {
let delegate: WebDriverExtension;
let delegate: WebDriverExtension = undefined !;
children.forEach(extension => {
if (extension.supports(capabilities)) {
delegate = extension;
@ -64,7 +64,7 @@ export abstract class WebDriverExtension {
timeBegin(name: string): Promise<any> { throw new Error('NYI'); }
timeEnd(name: string, restartName: string): Promise<any> { throw new Error('NYI'); }
timeEnd(name: string, restartName: string|null): Promise<any> { throw new Error('NYI'); }
/**
* Format:

View File

@ -51,7 +51,7 @@ export class ChromeDriverExtension extends WebDriverExtension {
return this._driver.executeScript(`console.time('${name}');`);
}
timeEnd(name: string, restartName: string = null): Promise<any> {
timeEnd(name: string, restartName: string|null = null): Promise<any> {
let script = `console.timeEnd('${name}');`;
if (restartName) {
script += `console.time('${restartName}');`;
@ -82,14 +82,14 @@ export class ChromeDriverExtension extends WebDriverExtension {
}
private _convertPerfRecordsToEvents(
chromeEvents: Array<{[key: string]: any}>, normalizedEvents: PerfLogEvent[] = null) {
chromeEvents: Array<{[key: string]: any}>, normalizedEvents: PerfLogEvent[]|null = null) {
if (!normalizedEvents) {
normalizedEvents = [];
}
chromeEvents.forEach((event) => {
const categories = this._parseCategories(event['cat']);
const normalizedEvent = this._convertEvent(event, categories);
if (normalizedEvent != null) normalizedEvents.push(normalizedEvent);
if (normalizedEvent != null) normalizedEvents !.push(normalizedEvent);
});
return normalizedEvents;
}
@ -167,7 +167,7 @@ export class ChromeDriverExtension extends WebDriverExtension {
private _isEvent(
eventCategories: string[], eventName: string, expectedCategories: string[],
expectedName: string = null): boolean {
expectedName: string|null = null): boolean {
const hasCategories = expectedCategories.reduce(
(value, cat) => value && eventCategories.indexOf(cat) !== -1, true);
return !expectedName ? hasCategories : hasCategories && eventName === expectedName;

View File

@ -32,7 +32,7 @@ export class FirefoxDriverExtension extends WebDriverExtension {
return this._driver.executeScript('window.markStart("' + name + '");');
}
timeEnd(name: string, restartName: string = null): Promise<any> {
timeEnd(name: string, restartName: string|null = null): Promise<any> {
let script = 'window.markEnd("' + name + '");';
if (restartName != null) {
script += 'window.markStart("' + restartName + '");';

View File

@ -23,7 +23,7 @@ export class IOsDriverExtension extends WebDriverExtension {
return this._driver.executeScript(`console.time('${name}');`);
}
timeEnd(name: string, restartName: string = null): Promise<any> {
timeEnd(name: string, restartName: string|null = null): Promise<any> {
let script = `console.timeEnd('${name}');`;
if (restartName != null) {
script += `console.time('${restartName}');`;
@ -50,28 +50,28 @@ export class IOsDriverExtension extends WebDriverExtension {
}
/** @internal */
private _convertPerfRecordsToEvents(records: any[], events: PerfLogEvent[] = null) {
private _convertPerfRecordsToEvents(records: any[], events: PerfLogEvent[]|null = null) {
if (!events) {
events = [];
}
records.forEach((record) => {
let endEvent: PerfLogEvent = null;
let endEvent: PerfLogEvent|null = null;
const type = record['type'];
const data = record['data'];
const startTime = record['startTime'];
const endTime = record['endTime'];
if (type === 'FunctionCall' && (data == null || data['scriptName'] !== 'InjectedScript')) {
events.push(createStartEvent('script', startTime));
events !.push(createStartEvent('script', startTime));
endEvent = createEndEvent('script', endTime);
} else if (type === 'Time') {
events.push(createMarkStartEvent(data['message'], startTime));
events !.push(createMarkStartEvent(data['message'], startTime));
} else if (type === 'TimeEnd') {
events.push(createMarkEndEvent(data['message'], startTime));
events !.push(createMarkEndEvent(data['message'], startTime));
} else if (
type === 'RecalculateStyles' || type === 'Layout' || type === 'UpdateLayerTree' ||
type === 'Paint' || type === 'Rasterize' || type === 'CompositeLayers') {
events.push(createStartEvent('render', startTime));
events !.push(createStartEvent('render', startTime));
endEvent = createEndEvent('render', endTime);
}
// Note: ios used to support GCEvent up until iOS 6 :-(
@ -79,7 +79,7 @@ export class IOsDriverExtension extends WebDriverExtension {
this._convertPerfRecordsToEvents(record['children'], events);
}
if (endEvent != null) {
events.push(endEvent);
events !.push(endEvent);
}
});
return events;