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

[NoQA] chore: add perf tests for SidebarUtils #30909

Merged
merged 10 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ type ActorDetails = {
*/
function getOptionData(
report: Report,
reportActions: Record<string, ReportAction[]>,
reportActions: Record<string, ReportAction>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Has this been wrong til now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it should be Record<string, ReportAction[]>, ReportActions is an objects here, not an array.

Copy link
Contributor

Choose a reason for hiding this comment

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

you mean Record<string, ReportAction>?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes - Record<string, ReportAction> 😉

personalDetails: Record<number, PersonalDetails>,
preferredLocale: ValueOf<typeof CONST.LOCALES>,
policy: Policy,
Expand Down
97 changes: 97 additions & 0 deletions tests/perf-test/SidebarUtils.perf-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import Onyx, {OnyxCollection} from 'react-native-onyx';
import {measureFunction} from 'reassure';
import SidebarUtils from '@libs/SidebarUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {PersonalDetails} from '@src/types/onyx';
import Policy from '@src/types/onyx/Policy';
import Report from '@src/types/onyx/Report';
import ReportAction, {ReportActions} from '@src/types/onyx/ReportAction';
import createCollection from '../utils/collections/createCollection';
import createPersonalDetails from '../utils/collections/personalDetails';
import createRandomPolicy from '../utils/collections/policies';
import createRandomReportAction from '../utils/collections/reportActions';
import createRandomReport from '../utils/collections/reports';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
}),
);

// Clear out Onyx after each test so that each test starts with a clean slate
afterEach(() => {
Onyx.clear();
});

const getMockedReports = (length = 500) =>
createCollection<Report>(
(item) => `${ONYXKEYS.COLLECTION.REPORT}${item.reportID}`,
(index) => createRandomReport(index),
length,
);

const reportActions = createCollection<ReportAction>(
(item) => `${item.reportActionID}`,
(index) => createRandomReportAction(index),
);

const personalDetails = createCollection<PersonalDetails>(
(item) => item.accountID,
(index) => createPersonalDetails(index),
);

const mockedResponseMap = getMockedReports(5000) as Record<`${typeof ONYXKEYS.COLLECTION.REPORT}`, Report>;
const runs = CONST.PERFORMANCE_TESTS.RUNS;

test('getOptionData on 5k reports', async () => {
const report = createRandomReport(1);
const preferredLocale = 'en';
const policy = createRandomPolicy(1);
const parentReportAction = createRandomReportAction(1);

Onyx.multiSet({
...mockedResponseMap,
});

await waitForBatchedUpdates();
await measureFunction(() => SidebarUtils.getOptionData(report, reportActions, personalDetails, preferredLocale, policy, parentReportAction), {runs});
});

test('getOrderedReportIDs on 5k reports', async () => {
const currentReportId = '1';
const allReports = getMockedReports();
const betas = [CONST.BETAS.DEFAULT_ROOMS, CONST.BETAS.POLICY_ROOMS];

const policies = createCollection<Policy>(
(item) => `${ONYXKEYS.COLLECTION.POLICY}${item.id}`,
(index) => createRandomPolicy(index),
);

const allReportActions = Object.fromEntries(
Object.keys(reportActions).map((key) => [
key,
[
{
errors: reportActions[key].errors ?? [],
message: [
{
moderationDecision: {
decision: reportActions[key].message?.[0]?.moderationDecision?.decision,
},
},
],
},
],
]),
) as unknown as OnyxCollection<ReportActions>;

Onyx.multiSet({
...mockedResponseMap,
});

await waitForBatchedUpdates();
await measureFunction(() => SidebarUtils.getOrderedReportIDs(currentReportId, allReports, betas, policies, CONST.PRIORITY_MODE.DEFAULT, allReportActions), {runs});
});
2 changes: 1 addition & 1 deletion tests/utils/collections/createCollection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function createCollection<T>(createKey: (item: T, index: number) => string, createItem: (index: number) => T, length = 500): Record<string, T> {
export default function createCollection<T>(createKey: (item: T, index: number) => string | number, createItem: (index: number) => T, length = 500): Record<string, T> {
const map: Record<string, T> = {};

for (let i = 0; i < length; i++) {
Expand Down
12 changes: 12 additions & 0 deletions tests/utils/collections/personalDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {randAvatar, randEmail, randWord} from '@ngneat/falso';
import type {PersonalDetails} from '@src/types/onyx';

export default function createPersonalDetails(index: number): PersonalDetails {
return {
accountID: index,
avatar: randAvatar(),
displayName: randWord(),
lastName: randWord(),
login: randEmail(),
};
}
Loading