mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Refactor of Fb4aReactFabricPerfLogger
Summary: This diff refactors Fb4aReactFabricPerfLogger in order consume fabric commit statistics outside of fabric changelog: [internal] internal Reviewed By: sshic Differential Revision: D34056540 fbshipit-source-id: 8f3b8003e1601ca36c9ba57be874a40bad455235
This commit is contained in:
committed by
Facebook GitHub Bot
parent
8dddff5547
commit
78e03b6fb7
-129
@@ -1,129 +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.
|
||||
*/
|
||||
|
||||
package com.facebook.react.fabric;
|
||||
|
||||
import static com.facebook.react.bridge.ReactMarkerConstants.*;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.common.logging.FLog;
|
||||
import com.facebook.react.bridge.ReactMarker;
|
||||
import com.facebook.react.bridge.ReactMarkerConstants;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ConsoleReactFabricPerfLogger implements ReactMarker.FabricMarkerListener {
|
||||
|
||||
private final Map<Integer, FabricCommitPoint> mFabricCommitMarkers = new HashMap<>();
|
||||
|
||||
private static class FabricCommitPoint {
|
||||
public long commitStart;
|
||||
public long commitEnd;
|
||||
public long finishTransactionStart;
|
||||
public long finishTransactionEnd;
|
||||
public long diffStart;
|
||||
public long diffEnd;
|
||||
public long updateUIMainThreadEnd;
|
||||
public long layoutStart;
|
||||
public long layoutEnd;
|
||||
public long batchExecutionStart;
|
||||
public long batchExecutionEnd;
|
||||
public long updateUIMainThreadStart;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logFabricMarker(
|
||||
ReactMarkerConstants name, @Nullable String tag, int instanceKey, long timestamp) {
|
||||
|
||||
if (isFabricCommitMarker(name)) {
|
||||
FabricCommitPoint commitPoint = mFabricCommitMarkers.get(instanceKey);
|
||||
if (commitPoint == null) {
|
||||
commitPoint = new FabricCommitPoint();
|
||||
mFabricCommitMarkers.put(instanceKey, commitPoint);
|
||||
}
|
||||
updateFabricCommitPoint(name, commitPoint, timestamp);
|
||||
|
||||
if (name == ReactMarkerConstants.FABRIC_BATCH_EXECUTION_END) {
|
||||
FLog.i(
|
||||
FabricUIManager.TAG,
|
||||
"Statistic of Fabric commit #: "
|
||||
+ instanceKey
|
||||
+ "\n - Total commit time: "
|
||||
+ (commitPoint.finishTransactionEnd - commitPoint.commitStart)
|
||||
+ " ms.\n - Layout: "
|
||||
+ (commitPoint.layoutEnd - commitPoint.layoutStart)
|
||||
+ " ms.\n - Diffing: "
|
||||
+ (commitPoint.diffEnd - commitPoint.diffStart)
|
||||
+ " ms.\n"
|
||||
+ " - FinishTransaction (Diffing + Processing + Serialization of MutationInstructions): "
|
||||
+ (commitPoint.finishTransactionEnd - commitPoint.finishTransactionStart)
|
||||
+ " ms.\n"
|
||||
+ " - Mounting: "
|
||||
+ (commitPoint.batchExecutionEnd - commitPoint.batchExecutionStart)
|
||||
+ " ms.");
|
||||
mFabricCommitMarkers.remove(instanceKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isFabricCommitMarker(ReactMarkerConstants name) {
|
||||
return name == FABRIC_COMMIT_START
|
||||
|| name == FABRIC_COMMIT_END
|
||||
|| name == FABRIC_FINISH_TRANSACTION_START
|
||||
|| name == FABRIC_FINISH_TRANSACTION_END
|
||||
|| name == FABRIC_DIFF_START
|
||||
|| name == FABRIC_DIFF_END
|
||||
|| name == FABRIC_LAYOUT_START
|
||||
|| name == FABRIC_LAYOUT_END
|
||||
|| name == FABRIC_BATCH_EXECUTION_START
|
||||
|| name == FABRIC_BATCH_EXECUTION_END
|
||||
|| name == FABRIC_UPDATE_UI_MAIN_THREAD_START
|
||||
|| name == FABRIC_UPDATE_UI_MAIN_THREAD_END;
|
||||
}
|
||||
|
||||
private static void updateFabricCommitPoint(
|
||||
ReactMarkerConstants name, FabricCommitPoint commitPoint, long timestamp) {
|
||||
switch (name) {
|
||||
case FABRIC_COMMIT_START:
|
||||
commitPoint.commitStart = timestamp;
|
||||
break;
|
||||
case FABRIC_COMMIT_END:
|
||||
commitPoint.commitEnd = timestamp;
|
||||
break;
|
||||
case FABRIC_FINISH_TRANSACTION_START:
|
||||
commitPoint.finishTransactionStart = timestamp;
|
||||
break;
|
||||
case FABRIC_FINISH_TRANSACTION_END:
|
||||
commitPoint.finishTransactionEnd = timestamp;
|
||||
break;
|
||||
case FABRIC_DIFF_START:
|
||||
commitPoint.diffStart = timestamp;
|
||||
break;
|
||||
case FABRIC_DIFF_END:
|
||||
commitPoint.diffEnd = timestamp;
|
||||
break;
|
||||
case FABRIC_LAYOUT_START:
|
||||
commitPoint.layoutStart = timestamp;
|
||||
break;
|
||||
case FABRIC_LAYOUT_END:
|
||||
commitPoint.layoutEnd = timestamp;
|
||||
break;
|
||||
case FABRIC_BATCH_EXECUTION_START:
|
||||
commitPoint.batchExecutionStart = timestamp;
|
||||
break;
|
||||
case FABRIC_BATCH_EXECUTION_END:
|
||||
commitPoint.batchExecutionEnd = timestamp;
|
||||
break;
|
||||
case FABRIC_UPDATE_UI_MAIN_THREAD_START:
|
||||
commitPoint.updateUIMainThreadStart = timestamp;
|
||||
break;
|
||||
case FABRIC_UPDATE_UI_MAIN_THREAD_END:
|
||||
commitPoint.updateUIMainThreadEnd = timestamp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package com.facebook.react.fabric;
|
||||
|
||||
import static com.facebook.react.bridge.ReactMarkerConstants.*;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.react.bridge.ReactMarker;
|
||||
import com.facebook.react.bridge.ReactMarkerConstants;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DevToolsReactPerfLogger implements ReactMarker.FabricMarkerListener {
|
||||
|
||||
private final Map<Integer, FabricCommitPoint> mFabricCommitMarkers = new HashMap<>();
|
||||
private final List<DevToolsReactPerfLoggerListener> mDevToolsReactPerfLoggerListeners =
|
||||
new ArrayList<>();
|
||||
|
||||
public interface DevToolsReactPerfLoggerListener {
|
||||
|
||||
void onFabricCommitEnd(FabricCommitPoint commitPoint);
|
||||
}
|
||||
|
||||
public static class FabricCommitPoint {
|
||||
private final long mCommitNumber;
|
||||
private final Map<ReactMarkerConstants, Long> mPoints = new HashMap<>();
|
||||
|
||||
private FabricCommitPoint(int commitNumber) {
|
||||
this.mCommitNumber = commitNumber;
|
||||
}
|
||||
|
||||
private void addPoint(ReactMarkerConstants key, long time) {
|
||||
mPoints.put(key, time);
|
||||
}
|
||||
|
||||
private long getValue(ReactMarkerConstants marker) {
|
||||
Long value = mPoints.get(marker);
|
||||
return value != null ? value : -1;
|
||||
}
|
||||
|
||||
public long getCommitNumber() {
|
||||
return mCommitNumber;
|
||||
}
|
||||
|
||||
public long getCommitStart() {
|
||||
return getValue(FABRIC_COMMIT_START);
|
||||
}
|
||||
|
||||
public long getCommitEnd() {
|
||||
return getValue(FABRIC_COMMIT_END);
|
||||
}
|
||||
|
||||
public long getFinishTransactionStart() {
|
||||
return getValue(FABRIC_FINISH_TRANSACTION_START);
|
||||
}
|
||||
|
||||
public long getFinishTransactionEnd() {
|
||||
return getValue(FABRIC_FINISH_TRANSACTION_END);
|
||||
}
|
||||
|
||||
public long getDiffStart() {
|
||||
return getValue(FABRIC_DIFF_START);
|
||||
}
|
||||
|
||||
public long getDiffEnd() {
|
||||
return getValue(FABRIC_DIFF_END);
|
||||
}
|
||||
|
||||
public long getLayoutStart() {
|
||||
return getValue(FABRIC_LAYOUT_START);
|
||||
}
|
||||
|
||||
public long getLayoutEnd() {
|
||||
return getValue(FABRIC_LAYOUT_END);
|
||||
}
|
||||
|
||||
public long getBatchExecutionStart() {
|
||||
return getValue(FABRIC_BATCH_EXECUTION_START);
|
||||
}
|
||||
|
||||
public long getBatchExecutionEnd() {
|
||||
return getValue(FABRIC_BATCH_EXECUTION_END);
|
||||
}
|
||||
|
||||
public long getUpdateUIMainThreadStart() {
|
||||
return getValue(FABRIC_UPDATE_UI_MAIN_THREAD_START);
|
||||
}
|
||||
|
||||
public long getUpdateUIMainThreadEnd() {
|
||||
return getValue(FABRIC_UPDATE_UI_MAIN_THREAD_END);
|
||||
}
|
||||
}
|
||||
|
||||
public void addDevToolsReactPerfLoggerListener(DevToolsReactPerfLoggerListener listener) {
|
||||
mDevToolsReactPerfLoggerListeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeDevToolsReactPerfLoggerListener(DevToolsReactPerfLoggerListener listener) {
|
||||
mDevToolsReactPerfLoggerListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logFabricMarker(
|
||||
ReactMarkerConstants name, @Nullable String tag, int instanceKey, long timestamp) {
|
||||
|
||||
if (isFabricCommitMarker(name)) {
|
||||
FabricCommitPoint commitPoint = mFabricCommitMarkers.get(instanceKey);
|
||||
if (commitPoint == null) {
|
||||
commitPoint = new FabricCommitPoint(instanceKey);
|
||||
mFabricCommitMarkers.put(instanceKey, commitPoint);
|
||||
}
|
||||
commitPoint.addPoint(name, timestamp);
|
||||
|
||||
if (name == ReactMarkerConstants.FABRIC_BATCH_EXECUTION_END) {
|
||||
onFabricCommitEnd(commitPoint);
|
||||
mFabricCommitMarkers.remove(instanceKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void onFabricCommitEnd(FabricCommitPoint commitPoint) {
|
||||
for (DevToolsReactPerfLoggerListener listener : mDevToolsReactPerfLoggerListeners) {
|
||||
listener.onFabricCommitEnd(commitPoint);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isFabricCommitMarker(ReactMarkerConstants name) {
|
||||
return name == FABRIC_COMMIT_START
|
||||
|| name == FABRIC_COMMIT_END
|
||||
|| name == FABRIC_FINISH_TRANSACTION_START
|
||||
|| name == FABRIC_FINISH_TRANSACTION_END
|
||||
|| name == FABRIC_DIFF_START
|
||||
|| name == FABRIC_DIFF_END
|
||||
|| name == FABRIC_LAYOUT_START
|
||||
|| name == FABRIC_LAYOUT_END
|
||||
|| name == FABRIC_BATCH_EXECUTION_START
|
||||
|| name == FABRIC_BATCH_EXECUTION_END
|
||||
|| name == FABRIC_UPDATE_UI_MAIN_THREAD_START
|
||||
|| name == FABRIC_UPDATE_UI_MAIN_THREAD_END;
|
||||
}
|
||||
}
|
||||
@@ -105,7 +105,7 @@ public class FabricUIManager implements UIManager, LifecycleEventListener {
|
||||
ReactFeatureFlags.enableFabricLogs
|
||||
|| PrinterHolder.getPrinter()
|
||||
.shouldDisplayLogMessage(ReactDebugOverlayTags.FABRIC_UI_MANAGER);
|
||||
public ConsoleReactFabricPerfLogger mConsoleReactFabricPerfLogger;
|
||||
public DevToolsReactPerfLogger mDevToolsReactPerfLogger;
|
||||
|
||||
static {
|
||||
FabricSoLoader.staticInit();
|
||||
@@ -366,8 +366,32 @@ public class FabricUIManager implements UIManager, LifecycleEventListener {
|
||||
mEventDispatcher.registerEventEmitter(FABRIC, new FabricEventEmitter(this));
|
||||
mEventDispatcher.addBatchEventDispatchedListener(mEventBeatManager);
|
||||
if (ENABLE_FABRIC_LOGS) {
|
||||
mConsoleReactFabricPerfLogger = new ConsoleReactFabricPerfLogger();
|
||||
ReactMarker.addFabricListener(mConsoleReactFabricPerfLogger);
|
||||
mDevToolsReactPerfLogger = new DevToolsReactPerfLogger();
|
||||
mDevToolsReactPerfLogger.addDevToolsReactPerfLoggerListener(
|
||||
new DevToolsReactPerfLogger.DevToolsReactPerfLoggerListener() {
|
||||
@Override
|
||||
public void onFabricCommitEnd(DevToolsReactPerfLogger.FabricCommitPoint commitPoint) {
|
||||
FLog.i(
|
||||
TAG,
|
||||
"Statistic of Fabric commit #: "
|
||||
+ commitPoint.getCommitNumber()
|
||||
+ "\n - Total commit time: "
|
||||
+ (commitPoint.getFinishTransactionEnd() - commitPoint.getCommitStart())
|
||||
+ " ms.\n - Layout: "
|
||||
+ (commitPoint.getLayoutEnd() - commitPoint.getLayoutStart())
|
||||
+ " ms.\n - Diffing: "
|
||||
+ (commitPoint.getDiffEnd() - commitPoint.getDiffStart())
|
||||
+ " ms.\n"
|
||||
+ " - FinishTransaction (Diffing + Processing + Serialization of MutationInstructions): "
|
||||
+ (commitPoint.getFinishTransactionEnd()
|
||||
- commitPoint.getFinishTransactionStart())
|
||||
+ " ms.\n"
|
||||
+ " - Mounting: "
|
||||
+ (commitPoint.getBatchExecutionEnd() - commitPoint.getBatchExecutionStart())
|
||||
+ " ms.");
|
||||
}
|
||||
});
|
||||
ReactMarker.addFabricListener(mDevToolsReactPerfLogger);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,8 +402,8 @@ public class FabricUIManager implements UIManager, LifecycleEventListener {
|
||||
public void onCatalystInstanceDestroy() {
|
||||
FLog.i(TAG, "FabricUIManager.onCatalystInstanceDestroy");
|
||||
|
||||
if (mConsoleReactFabricPerfLogger != null) {
|
||||
ReactMarker.removeFabricListener(mConsoleReactFabricPerfLogger);
|
||||
if (mDevToolsReactPerfLogger != null) {
|
||||
ReactMarker.removeFabricListener(mDevToolsReactPerfLogger);
|
||||
}
|
||||
|
||||
if (mDestroyed) {
|
||||
|
||||
Reference in New Issue
Block a user