mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Back out "Revert D102166537: [RN] Hoist PerformanceLogger primitives out of react-native" (#56621)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56621 Changelog: [General][Deprecated] Deprecated second argument for hook function passed to AppRegistry.setComponentProviderInstrumentationHook as it's not usable by apps. Passed stub that warns when used. Re-applying https://github.com/facebook/react-native/pull/56580 that was reverted. Reviewed By: javache Differential Revision: D102593753 fbshipit-source-id: 7714260a494a62d55880f10887cd440d217a4672
This commit is contained in:
1 parent
2c208f8c5c
commit
0f58e5e1db
16 files changed
+155
-818
No files matched your search
@@ -26,17 +26,4 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const start = Date.now();
|
||||
|
||||
require('../../src/private/setup/setUpDefaultReactNativeEnvironment').default();
|
||||
|
||||
const GlobalPerformanceLogger =
|
||||
require('../Utilities/GlobalPerformanceLogger').default;
|
||||
// We could just call GlobalPerformanceLogger.markPoint at the top of the file,
|
||||
// but then we'd be excluding the time it took to require the logger.
|
||||
// Instead, we just use Date.now and backdate the timestamp.
|
||||
GlobalPerformanceLogger.markPoint(
|
||||
'initializeCore_start',
|
||||
GlobalPerformanceLogger.currentTimestamp() - (Date.now() - start),
|
||||
);
|
||||
GlobalPerformanceLogger.markPoint('initializeCore_end');
|
||||
@@ -27,7 +27,7 @@ registerModule(
|
||||
);
|
||||
registerModule(
|
||||
'GlobalPerformanceLogger',
|
||||
() => require('../Utilities/GlobalPerformanceLogger').default,
|
||||
() => require('../ReactNative/DeprecatedPerformanceLoggerStub').default,
|
||||
);
|
||||
|
||||
if (__DEV__) {
|
||||
|
||||
+29
-12
@@ -14,7 +14,6 @@ import type {
|
||||
EventCallback,
|
||||
EventListener,
|
||||
} from '../../src/private/webapis/dom/events/EventTarget';
|
||||
import type {IPerformanceLogger} from '../Utilities/createPerformanceLogger';
|
||||
|
||||
import Event from '../../src/private/webapis/dom/events/Event';
|
||||
import {
|
||||
@@ -27,8 +26,6 @@ import ProgressEvent from '../../src/private/webapis/xhr/events/ProgressEvent';
|
||||
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
|
||||
|
||||
const BlobManager = require('../Blob/BlobManager').default;
|
||||
const GlobalPerformanceLogger =
|
||||
require('../Utilities/GlobalPerformanceLogger').default;
|
||||
const RCTNetworking = require('./RCTNetworking').default;
|
||||
const base64 = require('base64-js');
|
||||
const invariant = require('invariant');
|
||||
@@ -58,6 +55,17 @@ type XHRInterceptor = interface {
|
||||
loadingFailed(id: number, error: string): void,
|
||||
};
|
||||
|
||||
/**
|
||||
* Minimal contract for the optional performance logger that callers may attach
|
||||
* via `setPerformanceLogger(...)`. Defined locally so this module stays
|
||||
* self-contained and does not depend on any specific logger implementation.
|
||||
* Any object satisfying these two methods structurally is accepted.
|
||||
*/
|
||||
type XHRPerformanceLogger = interface {
|
||||
startTimespan(key: string): void,
|
||||
stopTimespan(key: string): void,
|
||||
};
|
||||
|
||||
// The native blob module is optional so inject it here if available.
|
||||
if (BlobManager.isAvailable) {
|
||||
BlobManager.addNetworkingHandler();
|
||||
@@ -167,8 +175,7 @@ class XMLHttpRequest extends EventTarget {
|
||||
_timedOut: boolean = false;
|
||||
_trackingName: ?string;
|
||||
_incrementalEvents: boolean = false;
|
||||
_startTime: ?number = null;
|
||||
_performanceLogger: IPerformanceLogger = GlobalPerformanceLogger;
|
||||
_performanceLogger: ?XHRPerformanceLogger = null;
|
||||
|
||||
static __setInterceptor_DO_NOT_USE(interceptor: ?XHRInterceptor) {
|
||||
XMLHttpRequest._interceptor = interceptor;
|
||||
@@ -334,8 +341,10 @@ class XMLHttpRequest extends EventTarget {
|
||||
responseURL: ?string,
|
||||
): void {
|
||||
if (requestId === this._requestId) {
|
||||
this._perfKey != null &&
|
||||
this._performanceLogger.stopTimespan(this._perfKey);
|
||||
const performanceLogger = this._performanceLogger;
|
||||
if (this._perfKey != null && performanceLogger != null) {
|
||||
performanceLogger.stopTimespan(this._perfKey);
|
||||
}
|
||||
this.status = status;
|
||||
this.setResponseHeaders(responseHeaders);
|
||||
this.setReadyState(this.HEADERS_RECEIVED);
|
||||
@@ -521,9 +530,15 @@ class XMLHttpRequest extends EventTarget {
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom extension for setting a custom performance logger
|
||||
* Custom extension that lets callers attach a performance logger receiving
|
||||
* a `network_XMLHttpRequest_<friendlyName>` start/stop timespan around each
|
||||
* dispatched request. The logger only needs to implement
|
||||
* `startTimespan(key)` / `stopTimespan(key)` (see the `XHRPerformanceLogger`
|
||||
* interface above). When no logger is set the timespan is not emitted.
|
||||
*/
|
||||
setPerformanceLogger(performanceLogger: IPerformanceLogger): XMLHttpRequest {
|
||||
setPerformanceLogger(
|
||||
performanceLogger: XHRPerformanceLogger,
|
||||
): XMLHttpRequest {
|
||||
this._performanceLogger = performanceLogger;
|
||||
return this;
|
||||
}
|
||||
@@ -598,9 +613,11 @@ class XMLHttpRequest extends EventTarget {
|
||||
|
||||
const doSend = () => {
|
||||
const friendlyName = this._trackingName ?? this._url;
|
||||
this._perfKey = 'network_XMLHttpRequest_' + String(friendlyName);
|
||||
this._performanceLogger.startTimespan(this._perfKey);
|
||||
this._startTime = performance.now();
|
||||
const performanceLogger = this._performanceLogger;
|
||||
if (performanceLogger != null) {
|
||||
this._perfKey = 'network_XMLHttpRequest_' + String(friendlyName);
|
||||
performanceLogger.startTimespan(this._perfKey);
|
||||
}
|
||||
invariant(
|
||||
this._method,
|
||||
'XMLHttpRequest method needs to be defined (%s).',
|
||||
|
||||
@@ -10,15 +10,10 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const createPerformanceLogger =
|
||||
require('../../Utilities/createPerformanceLogger').default;
|
||||
const GlobalPerformanceLogger =
|
||||
require('../../Utilities/GlobalPerformanceLogger').default;
|
||||
const Platform = require('../../Utilities/Platform').default;
|
||||
const XMLHttpRequest = require('../XMLHttpRequest').default;
|
||||
|
||||
jest.unmock('../../Utilities/Platform');
|
||||
jest.mock('../../Utilities/GlobalPerformanceLogger');
|
||||
let requestId = 1;
|
||||
function setRequestId(id: number) {
|
||||
if (Platform.OS === 'ios') {
|
||||
@@ -246,30 +241,11 @@ describe('XMLHttpRequest', function () {
|
||||
);
|
||||
});
|
||||
|
||||
it('should log to GlobalPerformanceLogger if a custom performance logger is not set', () => {
|
||||
xhr.open('GET', 'blabla');
|
||||
xhr.send();
|
||||
|
||||
expect(GlobalPerformanceLogger.startTimespan).toHaveBeenCalledWith(
|
||||
'network_XMLHttpRequest_blabla',
|
||||
);
|
||||
expect(GlobalPerformanceLogger.stopTimespan).not.toHaveBeenCalled();
|
||||
|
||||
setRequestId(8);
|
||||
xhr.__didReceiveResponse(requestId, 200, {
|
||||
'Content-Type': 'text/plain; charset=utf-8',
|
||||
'Content-Length': '32',
|
||||
});
|
||||
|
||||
expect(GlobalPerformanceLogger.stopTimespan).toHaveBeenCalledWith(
|
||||
'network_XMLHttpRequest_blabla',
|
||||
);
|
||||
});
|
||||
|
||||
it('should log to a custom performance logger if set', () => {
|
||||
const performanceLogger = createPerformanceLogger();
|
||||
jest.spyOn(performanceLogger, 'startTimespan');
|
||||
jest.spyOn(performanceLogger, 'stopTimespan');
|
||||
const performanceLogger = {
|
||||
startTimespan: jest.fn(),
|
||||
stopTimespan: jest.fn(),
|
||||
};
|
||||
|
||||
xhr.setPerformanceLogger(performanceLogger);
|
||||
|
||||
@@ -279,7 +255,6 @@ describe('XMLHttpRequest', function () {
|
||||
expect(performanceLogger.startTimespan).toHaveBeenCalledWith(
|
||||
'network_XMLHttpRequest_blabla',
|
||||
);
|
||||
expect(GlobalPerformanceLogger.startTimespan).not.toHaveBeenCalled();
|
||||
expect(performanceLogger.stopTimespan).not.toHaveBeenCalled();
|
||||
|
||||
setRequestId(9);
|
||||
@@ -291,7 +266,6 @@ describe('XMLHttpRequest', function () {
|
||||
expect(performanceLogger.stopTimespan).toHaveBeenCalledWith(
|
||||
'network_XMLHttpRequest_blabla',
|
||||
);
|
||||
expect(GlobalPerformanceLogger.stopTimespan).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should sort and lowercase response headers', function () {
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
*/
|
||||
|
||||
import type * as React from 'react';
|
||||
import type {IPerformanceLogger} from '../Utilities/IPerformanceLogger';
|
||||
import type {ViewStyle} from '../StyleSheet/StyleSheetTypes';
|
||||
import type {IPerformanceLogger} from './IPerformanceLogger';
|
||||
|
||||
type Task = (taskData: any) => Promise<void>;
|
||||
type TaskProvider = () => Task;
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
|
||||
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
|
||||
import type {RootTag} from '../Types/RootTagTypes';
|
||||
import type {IPerformanceLogger} from '../Utilities/createPerformanceLogger';
|
||||
import type {DisplayModeType} from './DisplayMode';
|
||||
import type {IPerformanceLogger} from './IPerformanceLogger.flow';
|
||||
|
||||
type HeadlessTask = (taskData: any) => Promise<void>;
|
||||
export type TaskProvider = () => HeadlessTask;
|
||||
|
||||
@@ -22,8 +22,8 @@ import type {
|
||||
WrapperComponentProvider,
|
||||
} from './AppRegistry.flow';
|
||||
|
||||
import createPerformanceLogger from '../Utilities/createPerformanceLogger';
|
||||
import SceneTracker from '../Utilities/SceneTracker';
|
||||
import DeprecatedPerformanceLoggerStub from './DeprecatedPerformanceLoggerStub';
|
||||
import {coerceDisplayMode} from './DisplayMode';
|
||||
import HeadlessJsTaskError from './HeadlessJsTaskError';
|
||||
import invariant from 'invariant';
|
||||
@@ -81,20 +81,19 @@ export function registerComponent(
|
||||
componentProvider: ComponentProvider,
|
||||
section?: boolean,
|
||||
): string {
|
||||
const scopedPerformanceLogger = createPerformanceLogger();
|
||||
runnables[appKey] = (appParameters, displayMode) => {
|
||||
const renderApplication = require('./renderApplication').default;
|
||||
renderApplication(
|
||||
componentProviderInstrumentationHook(
|
||||
componentProvider,
|
||||
scopedPerformanceLogger,
|
||||
DeprecatedPerformanceLoggerStub,
|
||||
),
|
||||
appParameters.initialProps,
|
||||
appParameters.rootTag,
|
||||
wrapperComponentProvider && wrapperComponentProvider(appParameters),
|
||||
rootViewStyleProvider && rootViewStyleProvider(appParameters),
|
||||
true, // fabric - deprecated, always true
|
||||
scopedPerformanceLogger,
|
||||
undefined, // formerly scopedPerformanceLogger; reserved positional slot
|
||||
appKey === 'LogBox', // is logbox
|
||||
appKey,
|
||||
displayMode,
|
||||
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {IPerformanceLogger} from './IPerformanceLogger.flow';
|
||||
|
||||
let didWarn: boolean = false;
|
||||
function warnDeprecated(): void {
|
||||
if (didWarn) {
|
||||
return;
|
||||
}
|
||||
didWarn = true;
|
||||
console.warn(
|
||||
'The default `IPerformanceLogger` provided by `react-native` (the ' +
|
||||
"`'GlobalPerformanceLogger'` callable native module and the " +
|
||||
'`scopedPerformanceLogger` argument passed to the hook registered with ' +
|
||||
'`AppRegistry.setComponentProviderInstrumentationHook`) is deprecated and ' +
|
||||
'will be removed in a future release. The instance supplied today is a ' +
|
||||
'no-op stub. Embedders that need a per-app performance logger should ' +
|
||||
'attach their own implementation.',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated The `react-native` package no longer ships a real
|
||||
* `IPerformanceLogger`. This stub is exposed to keep existing extension points
|
||||
* (the `'GlobalPerformanceLogger'` callable native module and the second
|
||||
* argument of `AppRegistry.setComponentProviderInstrumentationHook`'s hook)
|
||||
* source-compatible for embedders. Every method is a no-op that emits a
|
||||
* one-time deprecation warning on first use.
|
||||
*/
|
||||
const DeprecatedPerformanceLoggerStub: IPerformanceLogger = {
|
||||
addTimespan: () => warnDeprecated(),
|
||||
append: () => warnDeprecated(),
|
||||
clear: () => warnDeprecated(),
|
||||
clearCompleted: () => warnDeprecated(),
|
||||
close: () => warnDeprecated(),
|
||||
currentTimestamp: () => {
|
||||
warnDeprecated();
|
||||
return 0;
|
||||
},
|
||||
getExtras: () => {
|
||||
warnDeprecated();
|
||||
return {};
|
||||
},
|
||||
getPoints: () => {
|
||||
warnDeprecated();
|
||||
return {};
|
||||
},
|
||||
getPointExtras: () => {
|
||||
warnDeprecated();
|
||||
return {};
|
||||
},
|
||||
getTimespans: () => {
|
||||
warnDeprecated();
|
||||
return {};
|
||||
},
|
||||
hasTimespan: () => {
|
||||
warnDeprecated();
|
||||
return false;
|
||||
},
|
||||
isClosed: () => {
|
||||
warnDeprecated();
|
||||
return false;
|
||||
},
|
||||
logEverything: () => warnDeprecated(),
|
||||
markPoint: () => warnDeprecated(),
|
||||
removeExtra: () => {
|
||||
warnDeprecated();
|
||||
return undefined;
|
||||
},
|
||||
setExtra: () => warnDeprecated(),
|
||||
startTimespan: () => warnDeprecated(),
|
||||
stopTimespan: () => warnDeprecated(),
|
||||
};
|
||||
|
||||
export default DeprecatedPerformanceLoggerStub;
|
||||
+8
-5
@@ -7,6 +7,8 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
export type ExtraValue = number | string | boolean;
|
||||
export type Extras = {[key: string]: ExtraValue};
|
||||
export type Timespan = {
|
||||
startTime: number;
|
||||
endTime?: number | undefined;
|
||||
@@ -15,11 +17,12 @@ export type Timespan = {
|
||||
endExtras?: Extras | undefined;
|
||||
};
|
||||
|
||||
// Extra values should be serializable primitives
|
||||
export type ExtraValue = number | string | boolean;
|
||||
|
||||
export type Extras = {[key: string]: ExtraValue};
|
||||
|
||||
/**
|
||||
* @deprecated The scoped performance logger argument passed to the
|
||||
* `componentProviderInstrumentationHook` is no longer used by `react-native`
|
||||
* itself and will be removed in a future release. The instance supplied today
|
||||
* is a no-op stub.
|
||||
*/
|
||||
export interface IPerformanceLogger {
|
||||
addTimespan(
|
||||
key: string,
|
||||
+12
-9
@@ -8,6 +8,8 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
export type ExtraValue = number | string | boolean;
|
||||
export type Extras = {[key: string]: ExtraValue};
|
||||
export type Timespan = {
|
||||
startTime: number,
|
||||
endTime?: number,
|
||||
@@ -16,11 +18,12 @@ export type Timespan = {
|
||||
endExtras?: Extras,
|
||||
};
|
||||
|
||||
// Extra values should be serializable primitives
|
||||
export type ExtraValue = number | string | boolean;
|
||||
|
||||
export type Extras = {[key: string]: ExtraValue};
|
||||
|
||||
/**
|
||||
* @deprecated The scoped performance logger argument passed to the
|
||||
* `componentProviderInstrumentationHook` is no longer used by `react-native`
|
||||
* itself and will be removed in a future release. The instance supplied today
|
||||
* is a no-op stub.
|
||||
*/
|
||||
export interface IPerformanceLogger {
|
||||
addTimespan(
|
||||
key: string,
|
||||
@@ -34,10 +37,10 @@ export interface IPerformanceLogger {
|
||||
clearCompleted(): void;
|
||||
close(): void;
|
||||
currentTimestamp(): number;
|
||||
getExtras(): Readonly<{[key: string]: ?ExtraValue, ...}>;
|
||||
getPoints(): Readonly<{[key: string]: ?number, ...}>;
|
||||
getPointExtras(): Readonly<{[key: string]: ?Extras, ...}>;
|
||||
getTimespans(): Readonly<{[key: string]: ?Timespan, ...}>;
|
||||
getExtras(): Readonly<{[key: string]: ?ExtraValue}>;
|
||||
getPoints(): Readonly<{[key: string]: ?number}>;
|
||||
getPointExtras(): Readonly<{[key: string]: ?Extras}>;
|
||||
getTimespans(): Readonly<{[key: string]: ?Timespan}>;
|
||||
hasTimespan(key: string): boolean;
|
||||
isClosed(): boolean;
|
||||
logEverything(): void;
|
||||
@@ -9,10 +9,7 @@
|
||||
*/
|
||||
|
||||
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
|
||||
import type {IPerformanceLogger} from '../Utilities/createPerformanceLogger';
|
||||
|
||||
import GlobalPerformanceLogger from '../Utilities/GlobalPerformanceLogger';
|
||||
import PerformanceLoggerContext from '../Utilities/PerformanceLoggerContext';
|
||||
import AppContainer from './AppContainer';
|
||||
import DisplayMode, {type DisplayModeType} from './DisplayMode';
|
||||
import getCachedComponentWithDebugName from './getCachedComponentWithDebugName';
|
||||
@@ -39,7 +36,10 @@ export default function renderApplication<Props extends Object>(
|
||||
// Keep this parameter for backwards compatibility only. It is always treated as
|
||||
// true internally.
|
||||
fabric?: true | void,
|
||||
scopedPerformanceLogger?: IPerformanceLogger,
|
||||
// Reserved positional slot (formerly `scopedPerformanceLogger`). Preserved so
|
||||
// existing callers that supply this argument continue to type-check; the
|
||||
// value is ignored.
|
||||
_unused?: void,
|
||||
isLogBox?: boolean,
|
||||
debugName?: string,
|
||||
displayMode?: ?DisplayModeType,
|
||||
@@ -47,19 +47,15 @@ export default function renderApplication<Props extends Object>(
|
||||
) {
|
||||
invariant(rootTag, 'Expect to have a valid rootTag, instead got ', rootTag);
|
||||
|
||||
const performanceLogger = scopedPerformanceLogger ?? GlobalPerformanceLogger;
|
||||
|
||||
let renderable: React.MixedElement = (
|
||||
<PerformanceLoggerContext.Provider value={performanceLogger}>
|
||||
<AppContainer
|
||||
rootTag={rootTag}
|
||||
WrapperComponent={WrapperComponent}
|
||||
rootViewStyle={rootViewStyle}
|
||||
initialProps={initialProps ?? Object.freeze({})}
|
||||
internal_excludeLogBox={isLogBox}>
|
||||
<RootComponent {...initialProps} rootTag={rootTag} />
|
||||
</AppContainer>
|
||||
</PerformanceLoggerContext.Provider>
|
||||
<AppContainer
|
||||
rootTag={rootTag}
|
||||
WrapperComponent={WrapperComponent}
|
||||
rootViewStyle={rootViewStyle}
|
||||
initialProps={initialProps ?? Object.freeze({})}
|
||||
internal_excludeLogBox={isLogBox}>
|
||||
<RootComponent {...initialProps} rootTag={rootTag} />
|
||||
</AppContainer>
|
||||
);
|
||||
|
||||
if (__DEV__ && debugName) {
|
||||
@@ -75,8 +71,8 @@ export default function renderApplication<Props extends Object>(
|
||||
|
||||
if (useOffscreen && displayMode != null) {
|
||||
// $FlowFixMe[incompatible-type]
|
||||
// $FlowFixMe[prop-missing]
|
||||
// $FlowFixMe[missing-export]
|
||||
// $FlowFixMe[prop-missing] `unstable_Activity` is not yet in the React types.
|
||||
const Activity: ActivityType = React.unstable_Activity;
|
||||
|
||||
renderable = (
|
||||
@@ -87,16 +83,8 @@ export default function renderApplication<Props extends Object>(
|
||||
);
|
||||
}
|
||||
|
||||
performanceLogger.startTimespan('renderApplication_React_render');
|
||||
performanceLogger.setExtra('usedReactConcurrentRoot', '1');
|
||||
performanceLogger.setExtra('usedReactFabric', '1');
|
||||
performanceLogger.setExtra(
|
||||
'usedReactProfiler',
|
||||
Renderer.isProfilingRenderer(),
|
||||
);
|
||||
Renderer.renderElement({
|
||||
element: renderable,
|
||||
rootTag,
|
||||
});
|
||||
performanceLogger.stopTimespan('renderApplication_React_render');
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {IPerformanceLogger} from './createPerformanceLogger';
|
||||
|
||||
import createPerformanceLogger from './createPerformanceLogger';
|
||||
|
||||
/**
|
||||
* This is a global shared instance of IPerformanceLogger that is created with
|
||||
* createPerformanceLogger().
|
||||
* This logger should be used only for global performance metrics like the ones
|
||||
* that are logged during loading bundle. If you want to log something from your
|
||||
* React component you should use PerformanceLoggerContext instead.
|
||||
*/
|
||||
const GlobalPerformanceLogger: IPerformanceLogger = createPerformanceLogger();
|
||||
|
||||
export default GlobalPerformanceLogger;
|
||||
@@ -1,33 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {IPerformanceLogger} from './createPerformanceLogger';
|
||||
|
||||
import GlobalPerformanceLogger from './GlobalPerformanceLogger';
|
||||
import * as React from 'react';
|
||||
import {createContext, useContext} from 'react';
|
||||
|
||||
/**
|
||||
* This is a React Context that provides a scoped instance of IPerformanceLogger.
|
||||
* We wrap every <AppContainer /> with a Provider for this context so the logger
|
||||
* should be available in every component.
|
||||
* See React docs about using Context: https://react.dev/docs/context.html
|
||||
*/
|
||||
const PerformanceLoggerContext: React.Context<IPerformanceLogger> =
|
||||
createContext(GlobalPerformanceLogger);
|
||||
if (__DEV__) {
|
||||
PerformanceLoggerContext.displayName = 'PerformanceLoggerContext';
|
||||
}
|
||||
|
||||
export function usePerformanceLogger(): IPerformanceLogger {
|
||||
return useContext(PerformanceLoggerContext);
|
||||
}
|
||||
|
||||
export default PerformanceLoggerContext;
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {IPerformanceLogger} from '../createPerformanceLogger';
|
||||
|
||||
const GlobalPerformanceLogger: IPerformanceLogger = jest
|
||||
.unmock('../createPerformanceLogger')
|
||||
.genMockFromModule('../GlobalPerformanceLogger').default;
|
||||
|
||||
export default GlobalPerformanceLogger;
|
||||
-314
@@ -1,314 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {IPerformanceLogger} from '../createPerformanceLogger';
|
||||
import type {Extras} from '../IPerformanceLogger';
|
||||
|
||||
import createPerformanceLogger from '../createPerformanceLogger';
|
||||
import GlobalPerformanceLogger from '../GlobalPerformanceLogger';
|
||||
|
||||
const TIMESPAN_1 = '<timespan_1>';
|
||||
const EXTRA_KEY = '<extra_key>';
|
||||
const EXTRA_VALUE = '<extra_value>';
|
||||
const EXTRA_VALUE_2 = '<extra_value_2>';
|
||||
const POINT = '<point>';
|
||||
const POINT_TIMESTAMP = 99;
|
||||
const POINT_TIMESTAMP_2 = 999;
|
||||
const POINT_ANNOTATION_1: Extras = {extra: 'value1'};
|
||||
const POINT_ANNOTATION_2: Extras = {extra: 'value2'};
|
||||
|
||||
describe('PerformanceLogger', () => {
|
||||
beforeEach(() => {
|
||||
GlobalPerformanceLogger.clear();
|
||||
});
|
||||
describe('close() ', () => {
|
||||
let perfLogger;
|
||||
beforeEach(() => {
|
||||
perfLogger = createPerformanceLogger();
|
||||
});
|
||||
|
||||
it('does not markPoint', () => {
|
||||
perfLogger.close();
|
||||
perfLogger.markPoint(POINT, POINT_TIMESTAMP);
|
||||
expect(perfLogger.getPoints()).toEqual({});
|
||||
});
|
||||
it('does not startTimespan', () => {
|
||||
perfLogger.close();
|
||||
perfLogger.startTimespan(TIMESPAN_1);
|
||||
expect(perfLogger.getTimespans()).toEqual({});
|
||||
});
|
||||
it('does not setExtra', () => {
|
||||
perfLogger.close();
|
||||
perfLogger.setExtra('extra', 'an extra value');
|
||||
expect(perfLogger.getTimespans()).toEqual({});
|
||||
});
|
||||
|
||||
it('does not stopTimespan', () => {
|
||||
perfLogger.startTimespan(TIMESPAN_1);
|
||||
perfLogger.close();
|
||||
let timespan = perfLogger.getTimespans()[TIMESPAN_1];
|
||||
expect(timespan?.endTime).toBeUndefined();
|
||||
expect(timespan?.totalTime).toBeUndefined();
|
||||
perfLogger.stopTimespan(TIMESPAN_1);
|
||||
timespan = perfLogger.getTimespans()[TIMESPAN_1];
|
||||
expect(timespan?.endTime).toBeUndefined();
|
||||
expect(timespan?.totalTime).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
it('starts & stops a timespan', () => {
|
||||
let perfLogger = createPerformanceLogger();
|
||||
perfLogger.startTimespan(TIMESPAN_1);
|
||||
perfLogger.stopTimespan(TIMESPAN_1);
|
||||
expect(perfLogger.hasTimespan(TIMESPAN_1)).toBe(true);
|
||||
expect(perfLogger.getTimespans()[TIMESPAN_1]).toEqual({
|
||||
startTime: expect.any(Number),
|
||||
endTime: expect.any(Number),
|
||||
totalTime: expect.any(Number),
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
let old = perfLogger.getTimespans()[TIMESPAN_1];
|
||||
perfLogger.startTimespan(TIMESPAN_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;
|
||||
perfLogger.addTimespan(TIMESPAN_1, startTime, endTime);
|
||||
expect(perfLogger.getTimespans()[TIMESPAN_1]).toEqual({
|
||||
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);
|
||||
expect(perfLogger.getTimespans()[TIMESPAN_1]).toEqual(existing);
|
||||
});
|
||||
|
||||
it('logs an extra', () => {
|
||||
let perfLogger = createPerformanceLogger();
|
||||
perfLogger.setExtra(EXTRA_KEY, EXTRA_VALUE);
|
||||
expect(perfLogger.getExtras()).toEqual({
|
||||
[EXTRA_KEY]: EXTRA_VALUE,
|
||||
});
|
||||
});
|
||||
|
||||
it('does not override a extra', () => {
|
||||
let perfLogger = createPerformanceLogger();
|
||||
perfLogger.setExtra(EXTRA_KEY, EXTRA_VALUE);
|
||||
expect(perfLogger.getExtras()).toEqual({
|
||||
[EXTRA_KEY]: EXTRA_VALUE,
|
||||
});
|
||||
perfLogger.setExtra(EXTRA_KEY, EXTRA_VALUE_2);
|
||||
expect(perfLogger.getExtras()).toEqual({
|
||||
[EXTRA_KEY]: EXTRA_VALUE,
|
||||
});
|
||||
});
|
||||
|
||||
it('removes an extra', () => {
|
||||
let perfLogger = createPerformanceLogger();
|
||||
perfLogger.setExtra(EXTRA_KEY, EXTRA_VALUE);
|
||||
expect(perfLogger.getExtras()).toEqual({
|
||||
[EXTRA_KEY]: EXTRA_VALUE,
|
||||
});
|
||||
expect(perfLogger.removeExtra(EXTRA_KEY)).toEqual(EXTRA_VALUE);
|
||||
expect(perfLogger.getExtras()).toEqual({});
|
||||
});
|
||||
|
||||
it('logs a point', () => {
|
||||
let perfLogger = createPerformanceLogger();
|
||||
perfLogger.markPoint(POINT, POINT_TIMESTAMP);
|
||||
expect(perfLogger.getPoints()).toEqual({
|
||||
[POINT]: POINT_TIMESTAMP,
|
||||
});
|
||||
});
|
||||
|
||||
it('does not override a point', () => {
|
||||
let perfLogger = createPerformanceLogger();
|
||||
perfLogger.markPoint(POINT, POINT_TIMESTAMP);
|
||||
expect(perfLogger.getPoints()).toEqual({
|
||||
[POINT]: POINT_TIMESTAMP,
|
||||
});
|
||||
perfLogger.markPoint(POINT, POINT_TIMESTAMP_2);
|
||||
expect(perfLogger.getPoints()).toEqual({
|
||||
[POINT]: POINT_TIMESTAMP,
|
||||
});
|
||||
});
|
||||
|
||||
it('global and local loggers do not conflict', () => {
|
||||
let checkLogger = (logger: IPerformanceLogger, shouldBeEmpty: boolean) => {
|
||||
expect(Object.keys(logger.getTimespans())).toEqual(
|
||||
shouldBeEmpty ? [] : [TIMESPAN_1],
|
||||
);
|
||||
expect(logger.getExtras()).toEqual(
|
||||
shouldBeEmpty
|
||||
? {}
|
||||
: {
|
||||
[EXTRA_KEY]: EXTRA_VALUE,
|
||||
},
|
||||
);
|
||||
expect(Object.keys(logger.getPoints())).toEqual(
|
||||
shouldBeEmpty ? [] : [POINT],
|
||||
);
|
||||
};
|
||||
let localPerformanceLogger1 = createPerformanceLogger();
|
||||
let localPerformanceLogger2 = createPerformanceLogger();
|
||||
localPerformanceLogger1.startTimespan(TIMESPAN_1);
|
||||
localPerformanceLogger1.stopTimespan(TIMESPAN_1);
|
||||
localPerformanceLogger1.setExtra(EXTRA_KEY, EXTRA_VALUE);
|
||||
localPerformanceLogger1.markPoint(POINT);
|
||||
checkLogger(localPerformanceLogger1, false);
|
||||
checkLogger(localPerformanceLogger2, true);
|
||||
checkLogger(GlobalPerformanceLogger, true);
|
||||
localPerformanceLogger2.startTimespan(TIMESPAN_1);
|
||||
localPerformanceLogger2.stopTimespan(TIMESPAN_1);
|
||||
localPerformanceLogger2.setExtra(EXTRA_KEY, EXTRA_VALUE);
|
||||
localPerformanceLogger2.markPoint(POINT, undefined);
|
||||
checkLogger(localPerformanceLogger2, false);
|
||||
checkLogger(GlobalPerformanceLogger, true);
|
||||
GlobalPerformanceLogger.startTimespan(TIMESPAN_1);
|
||||
GlobalPerformanceLogger.stopTimespan(TIMESPAN_1);
|
||||
GlobalPerformanceLogger.setExtra(EXTRA_KEY, EXTRA_VALUE);
|
||||
GlobalPerformanceLogger.markPoint(POINT);
|
||||
checkLogger(GlobalPerformanceLogger, false);
|
||||
localPerformanceLogger1.clear();
|
||||
checkLogger(localPerformanceLogger1, true);
|
||||
checkLogger(localPerformanceLogger2, false);
|
||||
checkLogger(GlobalPerformanceLogger, false);
|
||||
GlobalPerformanceLogger.clear();
|
||||
checkLogger(localPerformanceLogger1, true);
|
||||
checkLogger(localPerformanceLogger2, false);
|
||||
checkLogger(GlobalPerformanceLogger, true);
|
||||
localPerformanceLogger2.clear();
|
||||
checkLogger(localPerformanceLogger1, true);
|
||||
checkLogger(localPerformanceLogger2, true);
|
||||
checkLogger(GlobalPerformanceLogger, true);
|
||||
});
|
||||
|
||||
it('records extras for a timespan', () => {
|
||||
let perfLogger = createPerformanceLogger();
|
||||
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,
|
||||
);
|
||||
expect(perfLogger.getTimespans()[TIMESPAN_1]?.endExtras).toEqual(
|
||||
POINT_ANNOTATION_2,
|
||||
);
|
||||
});
|
||||
|
||||
it('records extras for a point', () => {
|
||||
let perfLogger = createPerformanceLogger();
|
||||
perfLogger.markPoint(POINT, POINT_TIMESTAMP, POINT_ANNOTATION_1);
|
||||
|
||||
expect(Object.keys(perfLogger.getPointExtras())).toEqual([POINT]);
|
||||
expect(perfLogger.getPointExtras()[POINT]).toEqual(POINT_ANNOTATION_1);
|
||||
});
|
||||
|
||||
it('should allow extended logger to stopTimespan', () => {
|
||||
const loggerA = createPerformanceLogger();
|
||||
loggerA.startTimespan('loggerA_timespan');
|
||||
const loggerB = createPerformanceLogger();
|
||||
loggerB.append(loggerA);
|
||||
loggerB.stopTimespan('loggerA_timespan');
|
||||
const timespan = loggerB.getTimespans().loggerA_timespan;
|
||||
expect(timespan?.startTime).not.toBeUndefined();
|
||||
expect(timespan?.endTime).not.toBeUndefined();
|
||||
expect(timespan?.totalTime).not.toBeUndefined();
|
||||
expect(loggerA.isClosed()).toBe(false);
|
||||
});
|
||||
|
||||
it('should append logger', () => {
|
||||
const loggerA = createPerformanceLogger();
|
||||
loggerA.addTimespan('loggerA_timespan1', 0, 10);
|
||||
loggerA.addTimespan(
|
||||
'loggerA_timespan2',
|
||||
2,
|
||||
8,
|
||||
{loggerA_timespan2_start: 100},
|
||||
{loggerA_timespan2_end: 200},
|
||||
);
|
||||
loggerA.markPoint('loggerA_point', 5, {loggerA_pointExtra: true});
|
||||
loggerA.setExtra('loggerA_extra', true);
|
||||
|
||||
const loggerB = createPerformanceLogger();
|
||||
loggerB.append(loggerA);
|
||||
loggerB.addTimespan('loggerB_timespan', 0, 10);
|
||||
loggerB.markPoint('loggerB_point', 3);
|
||||
|
||||
expect(loggerA.isClosed()).toBe(false);
|
||||
|
||||
expect(loggerB.getTimespans()).toEqual({
|
||||
loggerA_timespan1: {
|
||||
endExtras: undefined,
|
||||
endTime: 10,
|
||||
startExtras: undefined,
|
||||
startTime: 0,
|
||||
totalTime: 10,
|
||||
},
|
||||
loggerA_timespan2: {
|
||||
endExtras: {
|
||||
loggerA_timespan2_end: 200,
|
||||
},
|
||||
endTime: 8,
|
||||
startExtras: {
|
||||
loggerA_timespan2_start: 100,
|
||||
},
|
||||
startTime: 2,
|
||||
totalTime: 6,
|
||||
},
|
||||
loggerB_timespan: {
|
||||
endExtras: undefined,
|
||||
endTime: 10,
|
||||
startExtras: undefined,
|
||||
startTime: 0,
|
||||
totalTime: 10,
|
||||
},
|
||||
});
|
||||
expect(loggerB.getPoints()).toEqual({
|
||||
loggerA_point: 5,
|
||||
loggerB_point: 3,
|
||||
});
|
||||
expect(loggerB.getPointExtras()).toEqual({
|
||||
loggerA_point: {
|
||||
loggerA_pointExtra: true,
|
||||
},
|
||||
});
|
||||
expect(loggerB.getExtras()).toEqual({
|
||||
loggerA_extra: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,329 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow strict
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type {
|
||||
Extras,
|
||||
ExtraValue,
|
||||
IPerformanceLogger,
|
||||
Timespan,
|
||||
} from './IPerformanceLogger';
|
||||
|
||||
const PRINT_TO_CONSOLE: false = false; // Type as false to prevent accidentally committing `true`;
|
||||
|
||||
export const getCurrentTimestamp: () => number =
|
||||
global.nativeQPLTimestamp ?? (() => global.performance.now());
|
||||
|
||||
class PerformanceLogger implements IPerformanceLogger {
|
||||
_timespans: {[key: string]: ?Timespan} = {};
|
||||
_extras: {[key: string]: ?ExtraValue} = {};
|
||||
_points: {[key: string]: ?number} = {};
|
||||
_pointExtras: {[key: string]: ?Extras, ...} = {};
|
||||
_closed: boolean = false;
|
||||
|
||||
addTimespan(
|
||||
key: string,
|
||||
startTime: number,
|
||||
endTime: number,
|
||||
startExtras?: Extras,
|
||||
endExtras?: Extras,
|
||||
) {
|
||||
if (this._closed) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: addTimespan - has closed ignoring: ',
|
||||
key,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (this._timespans[key]) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: Attempting to add a timespan that already exists ',
|
||||
key,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this._timespans[key] = {
|
||||
startTime,
|
||||
endTime,
|
||||
totalTime: endTime - (startTime || 0),
|
||||
startExtras,
|
||||
endExtras,
|
||||
};
|
||||
}
|
||||
|
||||
append(performanceLogger: IPerformanceLogger) {
|
||||
this._timespans = {
|
||||
...performanceLogger.getTimespans(),
|
||||
...this._timespans,
|
||||
};
|
||||
this._extras = {...performanceLogger.getExtras(), ...this._extras};
|
||||
this._points = {...performanceLogger.getPoints(), ...this._points};
|
||||
this._pointExtras = {
|
||||
...performanceLogger.getPointExtras(),
|
||||
...this._pointExtras,
|
||||
};
|
||||
}
|
||||
|
||||
clear() {
|
||||
this._timespans = {};
|
||||
this._extras = {};
|
||||
this._points = {};
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE) {
|
||||
console.log('PerformanceLogger.js', 'clear');
|
||||
}
|
||||
}
|
||||
|
||||
clearCompleted() {
|
||||
for (const key in this._timespans) {
|
||||
if (this._timespans[key]?.totalTime != null) {
|
||||
delete this._timespans[key];
|
||||
}
|
||||
}
|
||||
this._extras = {};
|
||||
this._points = {};
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE) {
|
||||
console.log('PerformanceLogger.js', 'clearCompleted');
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
this._closed = true;
|
||||
}
|
||||
|
||||
currentTimestamp(): number {
|
||||
return getCurrentTimestamp();
|
||||
}
|
||||
|
||||
getExtras(): {[key: string]: ?ExtraValue} {
|
||||
return this._extras;
|
||||
}
|
||||
|
||||
getPoints(): {[key: string]: ?number} {
|
||||
return this._points;
|
||||
}
|
||||
|
||||
getPointExtras(): {[key: string]: ?Extras} {
|
||||
return this._pointExtras;
|
||||
}
|
||||
|
||||
getTimespans(): {[key: string]: ?Timespan} {
|
||||
return this._timespans;
|
||||
}
|
||||
|
||||
hasTimespan(key: string): boolean {
|
||||
return !!this._timespans[key];
|
||||
}
|
||||
|
||||
isClosed(): boolean {
|
||||
return this._closed;
|
||||
}
|
||||
|
||||
logEverything() {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE) {
|
||||
// log timespans
|
||||
for (const key in this._timespans) {
|
||||
if (this._timespans[key]?.totalTime != null) {
|
||||
console.log(key + ': ' + this._timespans[key].totalTime + 'ms');
|
||||
}
|
||||
}
|
||||
|
||||
// log extras
|
||||
console.log(this._extras);
|
||||
|
||||
// log points
|
||||
for (const key in this._points) {
|
||||
if (this._points[key] != null) {
|
||||
console.log(key + ': ' + this._points[key] + 'ms');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
markPoint(
|
||||
key: string,
|
||||
timestamp?: number = getCurrentTimestamp(),
|
||||
extras?: Extras,
|
||||
) {
|
||||
if (this._closed) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: markPoint - has closed ignoring: ',
|
||||
key,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (this._points[key] != null) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: Attempting to mark a point that has been already logged ',
|
||||
key,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this._points[key] = timestamp;
|
||||
if (extras) {
|
||||
this._pointExtras[key] = extras;
|
||||
}
|
||||
}
|
||||
|
||||
removeExtra(key: string): ?ExtraValue {
|
||||
const value = this._extras[key];
|
||||
delete this._extras[key];
|
||||
return value;
|
||||
}
|
||||
|
||||
setExtra(key: string, value: ExtraValue) {
|
||||
if (this._closed) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log('PerformanceLogger: setExtra - has closed ignoring: ', key);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._extras.hasOwnProperty(key)) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: Attempting to set an extra that already exists ',
|
||||
{key, currentValue: this._extras[key], attemptedValue: value},
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this._extras[key] = value;
|
||||
}
|
||||
|
||||
startTimespan(
|
||||
key: string,
|
||||
timestamp?: number = getCurrentTimestamp(),
|
||||
extras?: Extras,
|
||||
) {
|
||||
if (this._closed) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: startTimespan - has closed ignoring: ',
|
||||
key,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._timespans[key]) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: Attempting to start a timespan that already exists ',
|
||||
key,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this._timespans[key] = {
|
||||
startTime: timestamp,
|
||||
startExtras: extras,
|
||||
};
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE) {
|
||||
console.log('PerformanceLogger.js', 'start: ' + key);
|
||||
}
|
||||
}
|
||||
|
||||
stopTimespan(
|
||||
key: string,
|
||||
timestamp?: number = getCurrentTimestamp(),
|
||||
extras?: Extras,
|
||||
) {
|
||||
if (this._closed) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: stopTimespan - has closed ignoring: ',
|
||||
key,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const timespan = this._timespans[key];
|
||||
if (!timespan || timespan.startTime == null) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: Attempting to end a timespan that has not started ',
|
||||
key,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (timespan.endTime != null) {
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE && __DEV__) {
|
||||
console.log(
|
||||
'PerformanceLogger: Attempting to end a timespan that has already ended ',
|
||||
key,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
timespan.endExtras = extras;
|
||||
timespan.endTime = timestamp;
|
||||
timespan.totalTime = timespan.endTime - (timespan.startTime || 0);
|
||||
/* $FlowFixMe[constant-condition] Error discovered during Constant
|
||||
* Condition roll out. See https://fburl.com/workplace/1v97vimq. */
|
||||
if (PRINT_TO_CONSOLE) {
|
||||
console.log('PerformanceLogger.js', 'end: ' + key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Re-exporting for backwards compatibility with all the clients that
|
||||
// may still import it from this module.
|
||||
export type {Extras, ExtraValue, IPerformanceLogger, Timespan};
|
||||
|
||||
/**
|
||||
* This function creates performance loggers that can be used to collect and log
|
||||
* various performance data such as timespans, points and extras.
|
||||
* The loggers need to have minimal overhead since they're used in production.
|
||||
*/
|
||||
export default function createPerformanceLogger(): IPerformanceLogger {
|
||||
return new PerformanceLogger();
|
||||
}
|
||||
Reference in new issue
Block a user