Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove metrics #479

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 2 additions & 54 deletions lib/Onyx.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure the changes here are reflected in Onyx.d.ts too!

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as Logger from './Logger';
import cache from './OnyxCache';
import * as Str from './Str';
import createDeferredTask from './createDeferredTask';
import * as PerformanceUtils from './metrics/PerformanceUtils';
import * as PerformanceUtils from './PerformanceUtils';
import Storage from './storage';
import utils from './utils';
import unstable_batchedUpdates from './batch';
Expand Down Expand Up @@ -1554,21 +1554,7 @@ function setMemoryOnlyKeys(keyList) {
* },
* });
*/
function init({
keys = {},
initialKeyStates = {},
safeEvictionKeys = [],
maxCachedKeysCount = 1000,
captureMetrics = false,
shouldSyncMultipleInstances = Boolean(global.localStorage),
debugSetState = false,
} = {}) {
if (captureMetrics) {
// The code here is only bundled and applied when the captureMetrics is set
// eslint-disable-next-line no-use-before-define
applyDecorators();
}

function init({keys = {}, initialKeyStates = {}, safeEvictionKeys = [], maxCachedKeysCount = 1000, shouldSyncMultipleInstances = Boolean(global.localStorage), debugSetState = false} = {}) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nab: shouldn't it be formatted like earlier? I think it was more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how prettier formatted it after removing one parameter 😅

if (debugSetState) {
PerformanceUtils.setShouldDebugSetState(true);
}
Expand Down Expand Up @@ -1630,42 +1616,4 @@ const Onyx = {
hasPendingMergeForKey,
};

/**
* Apply calls statistic decorators to benchmark Onyx
*
* @private
*/
function applyDecorators() {
// We're requiring the script dynamically here so that it's only evaluated when decorators are used
const decorate = require('./metrics');

// Re-assign with decorated functions
/* eslint-disable no-func-assign */
get = decorate.decorateWithMetrics(get, 'Onyx:get');
set = decorate.decorateWithMetrics(set, 'Onyx:set');
multiSet = decorate.decorateWithMetrics(multiSet, 'Onyx:multiSet');
clear = decorate.decorateWithMetrics(clear, 'Onyx:clear');
merge = decorate.decorateWithMetrics(merge, 'Onyx:merge');
mergeCollection = decorate.decorateWithMetrics(mergeCollection, 'Onyx:mergeCollection');
getAllKeys = decorate.decorateWithMetrics(getAllKeys, 'Onyx:getAllKeys');
initializeWithDefaultKeyStates = decorate.decorateWithMetrics(initializeWithDefaultKeyStates, 'Onyx:defaults');
update = decorate.decorateWithMetrics(update, 'Onyx:update');
/* eslint-enable */

// Re-expose decorated methods
/* eslint-disable rulesdir/prefer-actions-set-data */
Onyx.set = set;
Onyx.multiSet = multiSet;
Onyx.clear = clear;
Onyx.merge = merge;
Onyx.mergeCollection = mergeCollection;
Onyx.update = update;
/* eslint-enable */

// Expose stats methods on Onyx
Onyx.getMetrics = decorate.getMetrics;
Onyx.resetMetrics = decorate.resetMetrics;
Onyx.printMetrics = decorate.printMetrics;
}

export default Onyx;
66 changes: 66 additions & 0 deletions lib/PerformanceUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import lodashTransform from 'lodash/transform';
import {deepEqual} from 'fast-equals';

type UnknownObject = Record<string, unknown>;

type LogParams = {
keyThatChanged?: string;
difference?: unknown;
previousValue?: unknown;
newValue?: unknown;
};

type Mapping = Record<string, unknown> & {
key: string;
displayName: string;
};

let debugSetState = false;

function setShouldDebugSetState(debug: boolean) {
debugSetState = debug;
}

/**
* Deep diff between two objects. Useful for figuring out what changed about an object from one render to the next so
* that state and props updates can be optimized.
*/
function diffObject<TObject extends UnknownObject, TBase extends UnknownObject>(object: TObject, base: TBase): UnknownObject {
return lodashTransform(object, (result: UnknownObject, value, key) => {
if (deepEqual(value, base[key])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You changed the method here, please make sure it works correctly

return;
}

if (typeof value === 'object' && typeof base[key] === 'object') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried that typeof value === 'object' might work differently here, but I think it's fine 👍

// eslint-disable-next-line no-param-reassign
result[key] = diffObject(value as UnknownObject, base[key] as UnknownObject);
} else {
// eslint-disable-next-line no-param-reassign
result[key] = value;
}
});
}

/**
* Provide insights into why a setState() call occurred by diffing the before and after values.
*/
function logSetStateCall(mapping: Mapping, previousValue: unknown, newValue: unknown, caller: string, keyThatChanged: string) {
if (!debugSetState) {
return;
}

const logParams: LogParams = {};
if (keyThatChanged) {
logParams.keyThatChanged = keyThatChanged;
}
if (newValue && previousValue && typeof newValue === 'object' && typeof previousValue === 'object') {
logParams.difference = diffObject(previousValue as UnknownObject, newValue as UnknownObject);
} else {
logParams.previousValue = previousValue;
logParams.newValue = newValue;
}

console.debug(`[Onyx-Debug] ${mapping.displayName} setState() called. Subscribed to key '${mapping.key}' (${caller})`, logParams);
}

export {logSetStateCall, setShouldDebugSetState};
63 changes: 0 additions & 63 deletions lib/metrics/PerformanceUtils.js

This file was deleted.

10 changes: 0 additions & 10 deletions lib/metrics/index.js

This file was deleted.

Loading
Loading