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

Native set load measured #8242

Merged
merged 5 commits into from
Oct 14, 2024
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
27 changes: 27 additions & 0 deletions app/managers/performance_metrics_manager/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import RNUtils from '@mattermost/rnutils';
import performance from 'react-native-performance';

import {mockApiClient} from '@test/mock_api_client';
Expand Down Expand Up @@ -64,6 +65,12 @@ describe('load metrics', () => {
'setInterval',
]}).setSystemTime(new Date(TEST_EPOCH));
PerformanceMetricsManager = new PerformanceMetricsManagerClass();

const mockHasRegisteredLoad = {hasRegisteredLoad: false};
jest.mocked(RNUtils.setHasRegisteredLoad).mockImplementation(() => {
mockHasRegisteredLoad.hasRegisteredLoad = true;
});
jest.mocked(RNUtils.getHasRegisteredLoad).mockImplementation(() => mockHasRegisteredLoad);
});
afterEach(async () => {
jest.useRealTimers();
Expand Down Expand Up @@ -91,6 +98,26 @@ describe('load metrics', () => {
expect(mockApiClient.post).toHaveBeenCalledWith(expectedUrl, expectedRequest);
});

it('only register load once', async () => {
performance.mark('nativeLaunchStart');
const measure = getMeasure(TEST_EPOCH, 0);
const expectedRequest = getBaseReportRequest(measure.timestamp, measure.timestamp + 1);
expectedRequest.body.histograms = [measure];

PerformanceMetricsManager.setLoadTarget('HOME');
PerformanceMetricsManager.finishLoad('HOME', serverUrl);
await TestHelper.tick();
jest.advanceTimersByTime(INTERVAL_TIME);
await TestHelper.tick();
expect(mockApiClient.post).toHaveBeenCalledWith(expectedUrl, expectedRequest);
mockApiClient.post.mockClear();
PerformanceMetricsManager.finishLoad('HOME', serverUrl);
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: we expected to not see another call if we only call finishLoad() again, right? Besides that scenario, is there any merit to see if we do another call to performance.mark() and then finishLoad() during the same "session"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what you mean with that. The performance.mark() call we do in the test is to mock what happens in the native side, since unit tests don't deal with native side. Calling again performance.mark(), on top of not giving us extra information, would not reflect what happens on the native side.

Or did I misunderstood your comment?

Copy link
Contributor

Choose a reason for hiding this comment

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

Silly me, I misunderstood. performance.mark() just instantiate a new class with value from markName and will not affect what you're testing here which is finishLoad().

Copy link
Contributor

Choose a reason for hiding this comment

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

Follow-up suggestion, I think there might be a merit to cover the skipLoadMetric() just to make sure that no API calls are being posted.

await TestHelper.tick();
jest.advanceTimersByTime(INTERVAL_TIME);
await TestHelper.tick();
expect(mockApiClient.post).not.toHaveBeenCalled();
});

it('retry if the mark is not yet present', async () => {
const measure = getMeasure(TEST_EPOCH + (RETRY_TIME * 2), RETRY_TIME);
const expectedRequest = getBaseReportRequest(measure.timestamp, measure.timestamp + 1);
Expand Down
8 changes: 4 additions & 4 deletions app/managers/performance_metrics_manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import RNUtils from '@mattermost/rnutils';
import {AppState, type AppStateStatus} from 'react-native';
import performance from 'react-native-performance';

Expand All @@ -18,7 +19,6 @@ const MAX_RETRIES = 3;
class PerformanceMetricsManager {
private target: Target;
private batchers: {[serverUrl: string]: Batcher} = {};
private hasRegisteredLoad = false;
private lastAppStateIsActive = AppState.currentState === 'active';

constructor() {
Expand Down Expand Up @@ -49,15 +49,15 @@ class PerformanceMetricsManager {
}

public skipLoadMetric() {
this.hasRegisteredLoad = true;
RNUtils.setHasRegisteredLoad();
}

public finishLoad(location: Target, serverUrl: string) {
this.finishLoadWithRetries(location, serverUrl, 0);
}

private finishLoadWithRetries(location: Target, serverUrl: string, retries: number) {
if (this.target !== location || this.hasRegisteredLoad) {
if (this.target !== location || RNUtils.getHasRegisteredLoad().hasRegisteredLoad) {
return;
}

Expand All @@ -79,7 +79,7 @@ class PerformanceMetricsManager {
logWarning('We could not retrieve the mobile load metric');
}

this.hasRegisteredLoad = true;
RNUtils.setHasRegisteredLoad();
}

public startMetric(metricName: MetricName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class RNUtilsModuleImpl(private val reactContext: ReactApplicationContext) {
const val NAME = "RNUtils"

private var context: ReactApplicationContext? = null
private var hasRegisteredLoad = false

fun sendJSEvent(eventName: String, data: ReadableMap?) {
if (context?.hasActiveReactInstance() == true) {
Expand Down Expand Up @@ -73,6 +74,16 @@ class RNUtilsModuleImpl(private val reactContext: ReactApplicationContext) {
return SplitView.getWindowDimensions()
}

fun setHasRegisteredLoad() {
hasRegisteredLoad = true
}

fun getHasRegisteredLoad(): WritableMap {
val map = Arguments.createMap()
map.putBoolean("hasRegisteredLoad", hasRegisteredLoad)
return map
}

fun unlockOrientation() {}

fun lockPortrait() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class RNUtilsModule(val reactContext: ReactApplicationContext) : NativeRNUtilsSp

override fun getWindowDimensions(): WritableMap? = implementation.getWindowDimensions()

override fun setHasRegisteredLoad() = implementation.setHasRegisteredLoad()
override fun getHasRegisteredLoad(): WritableMap = implementation.getHasRegisteredLoad()

override fun unlockOrientation() {
implementation.unlockOrientation()
}
Expand Down
10 changes: 10 additions & 0 deletions libraries/@mattermost/rnutils/android/src/oldarch/RNUtilsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class RNUtilsModule(context: ReactApplicationContext) :
return implementation.getWindowDimensions()
}

@ReactMethod
fun setHasRegisteredLoad() {
implementation.setHasRegisteredLoad()
}

@ReactMethod(isBlockingSynchronousMethod = true)
fun getHasRegisteredLoad(): WritableMap {
return implementation.getHasRegisteredLoad()
}

@ReactMethod
fun unlockOrientation() {
implementation.unlockOrientation()
Expand Down
16 changes: 16 additions & 0 deletions libraries/@mattermost/rnutils/ios/RNUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ - (NSDictionary *)constantsToExport {
return [wrapper getWindowDimensions];
}

RCT_REMAP_BLOCKING_SYNCHRONOUS_METHOD(getHasRegisteredLoad, NSDictionary*, getLoad) {
return [wrapper getHasRegisteredLoad];
}

RCT_REMAP_METHOD(setHasRegisteredLoad, setLoad) {
[wrapper setHasRegisteredLoad];
}

RCT_REMAP_METHOD(unlockOrientation, unlock) {
[wrapper unlockOrientation];
}
Expand Down Expand Up @@ -166,6 +174,14 @@ - (NSDictionary *)getWindowDimensions {
return [wrapper getWindowDimensions];
}

- (NSDictionary *)getHasRegisteredLoad {
return [wrapper getHasRegisteredLoad];
}

- (void)setHasRegisteredLoad {
[wrapper setHasRegisteredLoad];
}

- (void)lockPortrait {
[wrapper lockOrientation];
}
Expand Down
9 changes: 9 additions & 0 deletions libraries/@mattermost/rnutils/ios/RNUtilsWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React

@objc public class RNUtilsWrapper: NSObject {
@objc public weak var delegate: RNUtilsDelegate? = nil
@objc private var hasRegisteredLoad = false

deinit {
DispatchQueue.main.async {
Expand Down Expand Up @@ -251,6 +252,14 @@ import React
group.wait()
return dimensions
}

@objc public func setHasRegisteredLoad() {
hasRegisteredLoad = true
}

@objc public func getHasRegisteredLoad() -> Dictionary<String, Any> {
return ["hasRegisteredLoad": hasRegisteredLoad]
larkox marked this conversation as resolved.
Show resolved Hide resolved
}

@objc public func unlockOrientation() {
DispatchQueue.main.async {
Expand Down
7 changes: 7 additions & 0 deletions libraries/@mattermost/rnutils/src/NativeRNUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export type WindowDimensionsChanged = Readonly<{
height: Double;
}>

type HasRegisteredLoadResponse = {
hasRegisteredLoad: boolean;
}

export interface Spec extends TurboModule {
readonly getConstants: () => Constants;

Expand All @@ -55,6 +59,9 @@ export interface Spec extends TurboModule {
unlockOrientation: () => void;
lockPortrait: () => void;

getHasRegisteredLoad: () => HasRegisteredLoadResponse;
setHasRegisteredLoad: () => void;

deleteDatabaseDirectory: (databaseName: string, shouldRemoveDirectory: boolean) => DatabaseOperationResult;
renameDatabase: (databaseName: string, newDatabaseName: string) => DatabaseOperationResult;
deleteEntitiesFile: () => boolean;
Expand Down
2 changes: 2 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ jest.doMock('react-native', () => {
addListener: jest.fn(),
removeListeners: jest.fn(),
isRunningInSplitView: jest.fn().mockReturnValue({isSplit: false, isTablet: false}),
getHasRegisteredLoad: jest.fn().mockReturnValue({hasRegisteredLoad: false}),
setHasRegisteredLoad: jest.fn(),

getDeliveredNotifications: jest.fn().mockResolvedValue([]),
removeChannelNotifications: jest.fn().mockImplementation(),
Expand Down