diff --git a/Libraries/Utilities/__tests__/PerformanceLogger-test.js b/Libraries/Utilities/__tests__/PerformanceLogger-test.js index 4d83a9b7941547..9af931852397ab 100644 --- a/Libraries/Utilities/__tests__/PerformanceLogger-test.js +++ b/Libraries/Utilities/__tests__/PerformanceLogger-test.js @@ -32,7 +32,7 @@ describe('PerformanceLogger', () => { let perfLogger = createPerformanceLogger(); perfLogger.startTimespan(TIMESPAN_1); perfLogger.stopTimespan(TIMESPAN_1); - perfLogger.addTimespan(TIMESPAN_2, TIMESPAN_2_DURATION); + perfLogger.addTimeAnnotation(TIMESPAN_2, TIMESPAN_2_DURATION); expect(perfLogger.hasTimespan(TIMESPAN_1)).toBe(true); expect(perfLogger.hasTimespan(TIMESPAN_2)).toBe(true); expect(perfLogger.getTimespans()[TIMESPAN_2].totalTime).toBe( @@ -46,10 +46,33 @@ describe('PerformanceLogger', () => { let old = perfLogger.getTimespans()[TIMESPAN_1]; perfLogger.startTimespan(TIMESPAN_1); expect(perfLogger.getTimespans()[TIMESPAN_1]).toBe(old); - perfLogger.addTimespan(TIMESPAN_1, 1); + perfLogger.addTimeAnnotation(TIMESPAN_1, 1); expect(perfLogger.getTimespans()[TIMESPAN_1]).toBe(old); }); + it('adds a timespan with start and end timestamps', () => { + let perfLogger = createPerformanceLogger(); + const startTime = 0; + const endTime = 100; + const description = 'description'; + perfLogger.addTimespan(TIMESPAN_1, startTime, endTime, description); + expect(perfLogger.getTimespans()[TIMESPAN_1]).toEqual({ + description, + startTime, + endTime, + totalTime: endTime - startTime, + }); + }); + + it('adds a timespan with same key will not override existing', () => { + let perfLogger = createPerformanceLogger(); + perfLogger.startTimespan(TIMESPAN_1); + perfLogger.stopTimespan(TIMESPAN_1); + const existing = perfLogger.getTimespans()[TIMESPAN_1]; + perfLogger.addTimespan(TIMESPAN_1, 0, 100, 'overriding'); + expect(perfLogger.getTimespans()[TIMESPAN_1]).toEqual(existing); + }); + it('logs an extra', () => { let perfLogger = createPerformanceLogger(); perfLogger.setExtra(EXTRA_KEY, EXTRA_VALUE); diff --git a/Libraries/Utilities/createPerformanceLogger.js b/Libraries/Utilities/createPerformanceLogger.js index 927cb54e8b3994..e30b42165bf0da 100644 --- a/Libraries/Utilities/createPerformanceLogger.js +++ b/Libraries/Utilities/createPerformanceLogger.js @@ -24,7 +24,8 @@ type Timespan = { }; export type IPerformanceLogger = { - addTimespan(string, number, string | void): void, + addTimeAnnotation(string, number, string | void): void, + addTimespan(string, number, number, string | void): void, startTimespan(string, string | void): void, stopTimespan(string, options?: {update?: boolean}): void, clear(): void, @@ -34,7 +35,7 @@ export type IPerformanceLogger = { getTimespans(): {[key: string]: Timespan, ...}, hasTimespan(string): boolean, logTimespans(): void, - addTimespans(Array, Array): void, + addTimeAnnotations(Array, Array): void, setExtra(string, mixed): void, getExtras(): {[key: string]: mixed, ...}, removeExtra(string): ?mixed, @@ -66,7 +67,7 @@ function createPerformanceLogger(): IPerformanceLogger { _extras: {}, _points: {}, - addTimespan(key: string, lengthInMs: number, description?: string) { + addTimeAnnotation(key: string, durationInMs: number, description?: string) { if (this._timespans[key]) { if (PRINT_TO_CONSOLE && __DEV__) { infoLog( @@ -79,7 +80,31 @@ function createPerformanceLogger(): IPerformanceLogger { this._timespans[key] = { description: description, - totalTime: lengthInMs, + totalTime: durationInMs, + }; + }, + + addTimespan( + key: string, + startTime: number, + endTime: number, + description?: string, + ) { + if (this._timespans[key]) { + if (PRINT_TO_CONSOLE && __DEV__) { + infoLog( + 'PerformanceLogger: Attempting to add a timespan that already exists ', + key, + ); + } + return; + } + + this._timespans[key] = { + description, + startTime, + endTime, + totalTime: endTime - (startTime || 0), }; }, @@ -199,10 +224,14 @@ function createPerformanceLogger(): IPerformanceLogger { } }, - addTimespans(newTimespans: Array, labels: Array) { - for (let ii = 0, l = newTimespans.length; ii < l; ii += 2) { + addTimeAnnotations(durationsInMs: Array, labels: Array) { + for (let ii = 0, l = durationsInMs.length; ii < l; ii += 2) { const label = labels[ii / 2]; - this.addTimespan(label, newTimespans[ii + 1] - newTimespans[ii], label); + this.addTimespan( + label, + durationsInMs[ii + 1] - durationsInMs[ii], + label, + ); } },