Skip to content

Commit

Permalink
Allow specifying custom timestamps in performance logger spans
Browse files Browse the repository at this point in the history
Summary: Changelog: [internal] Allow specifying custom timestamps in {start/stop}Timestamp in performance loggers

Reviewed By: lunaleaps

Differential Revision: D24474631

fbshipit-source-id: 48d8e69af8ba79ef1638cd5d03bac33af84f7881
  • Loading branch information
rubennorte authored and facebook-github-bot committed Oct 23, 2020
1 parent 5c498e3 commit 74fc285
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
18 changes: 16 additions & 2 deletions Libraries/Utilities/__tests__/PerformanceLogger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ describe('PerformanceLogger', () => {
});
});

it('starts & stops a timespan with custom timestamps', () => {
let perfLogger = createPerformanceLogger();
const startTime = 25;
const endTime = 35;
perfLogger.startTimespan(TIMESPAN_1, startTime);
perfLogger.stopTimespan(TIMESPAN_1, endTime);
expect(perfLogger.hasTimespan(TIMESPAN_1)).toBe(true);
expect(perfLogger.getTimespans()[TIMESPAN_1]).toEqual({
startTime,
endTime,
totalTime: expect.any(Number),
});
});

it('does not override a timespan', () => {
let perfLogger = createPerformanceLogger();
perfLogger.startTimespan(TIMESPAN_1);
Expand Down Expand Up @@ -206,8 +220,8 @@ describe('PerformanceLogger', () => {

it('records extras for a timespan', () => {
let perfLogger = createPerformanceLogger();
perfLogger.startTimespan(TIMESPAN_1, POINT_ANNOTATION_1);
perfLogger.stopTimespan(TIMESPAN_1, POINT_ANNOTATION_2);
perfLogger.startTimespan(TIMESPAN_1, undefined, POINT_ANNOTATION_1);
perfLogger.stopTimespan(TIMESPAN_1, undefined, POINT_ANNOTATION_2);
expect(perfLogger.getTimespans()[TIMESPAN_1]?.startExtras).toEqual(
POINT_ANNOTATION_1,
);
Expand Down
28 changes: 20 additions & 8 deletions Libraries/Utilities/createPerformanceLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export interface IPerformanceLogger {
markPoint(key: string, timestamp?: number, extras?: Extras): void;
removeExtra(key: string): ?ExtraValue;
setExtra(key: string, value: ExtraValue): void;
startTimespan(key: string, extras?: Extras): void;
stopTimespan(key: string, extras?: Extras): void;
startTimespan(key: string, timestamp?: number, extras?: Extras): void;
stopTimespan(key: string, timestamp?: number, extras?: Extras): void;
}

const _cookies: {[key: string]: number, ...} = {};
Expand Down Expand Up @@ -188,7 +188,11 @@ class PerformanceLogger implements IPerformanceLogger {
}
}

markPoint(key: string, timestamp?: number, extras?: Extras) {
markPoint(
key: string,
timestamp?: number = getCurrentTimestamp(),
extras?: Extras,
) {
if (this._closed) {
if (PRINT_TO_CONSOLE && __DEV__) {
infoLog('PerformanceLogger: markPoint - has closed ignoring: ', key);
Expand All @@ -204,7 +208,7 @@ class PerformanceLogger implements IPerformanceLogger {
}
return;
}
this._points[key] = timestamp ?? getCurrentTimestamp();
this._points[key] = timestamp;
if (extras) {
this._pointExtras[key] = extras;
}
Expand Down Expand Up @@ -236,7 +240,11 @@ class PerformanceLogger implements IPerformanceLogger {
this._extras[key] = value;
}

startTimespan(key: string, extras?: Extras) {
startTimespan(
key: string,
timestamp?: number = getCurrentTimestamp(),
extras?: Extras,
) {
if (this._closed) {
if (PRINT_TO_CONSOLE && __DEV__) {
infoLog(
Expand All @@ -258,7 +266,7 @@ class PerformanceLogger implements IPerformanceLogger {
}

this._timespans[key] = {
startTime: getCurrentTimestamp(),
startTime: timestamp,
startExtras: extras,
};
_cookies[key] = Systrace.beginAsyncEvent(key);
Expand All @@ -267,7 +275,11 @@ class PerformanceLogger implements IPerformanceLogger {
}
}

stopTimespan(key: string, extras?: Extras) {
stopTimespan(
key: string,
timestamp?: number = getCurrentTimestamp(),
extras?: Extras,
) {
if (this._closed) {
if (PRINT_TO_CONSOLE && __DEV__) {
infoLog('PerformanceLogger: stopTimespan - has closed ignoring: ', key);
Expand Down Expand Up @@ -296,7 +308,7 @@ class PerformanceLogger implements IPerformanceLogger {
}

timespan.endExtras = extras;
timespan.endTime = getCurrentTimestamp();
timespan.endTime = timestamp;
timespan.totalTime = timespan.endTime - (timespan.startTime || 0);
if (PRINT_TO_CONSOLE) {
infoLog('PerformanceLogger.js', 'end: ' + key);
Expand Down

0 comments on commit 74fc285

Please sign in to comment.