|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.fabric; |
| 9 | + |
| 10 | +import static com.facebook.react.bridge.ReactMarkerConstants.*; |
| 11 | + |
| 12 | +import androidx.annotation.Nullable; |
| 13 | +import com.facebook.common.logging.FLog; |
| 14 | +import com.facebook.react.bridge.ReactMarker; |
| 15 | +import com.facebook.react.bridge.ReactMarkerConstants; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +public class ConsoleReactFabricPerfLogger implements ReactMarker.FabricMarkerListener { |
| 20 | + |
| 21 | + private final Map<Integer, FabricCommitPoint> mFabricCommitMarkers = new HashMap<>(); |
| 22 | + |
| 23 | + private static class FabricCommitPoint { |
| 24 | + public long commitStart; |
| 25 | + public long commitEnd; |
| 26 | + public long finishTransactionStart; |
| 27 | + public long finishTransactionEnd; |
| 28 | + public long diffStart; |
| 29 | + public long diffEnd; |
| 30 | + public long updateUIMainThreadEnd; |
| 31 | + public long layoutStart; |
| 32 | + public long layoutEnd; |
| 33 | + public long batchExecutionStart; |
| 34 | + public long batchExecutionEnd; |
| 35 | + public long updateUIMainThreadStart; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void logFabricMarker( |
| 40 | + ReactMarkerConstants name, @Nullable String tag, int instanceKey, long timestamp) { |
| 41 | + |
| 42 | + if (isFabricCommitMarker(name)) { |
| 43 | + FabricCommitPoint commitPoint = mFabricCommitMarkers.get(instanceKey); |
| 44 | + if (commitPoint == null) { |
| 45 | + commitPoint = new FabricCommitPoint(); |
| 46 | + mFabricCommitMarkers.put(instanceKey, commitPoint); |
| 47 | + } |
| 48 | + updateFabricCommitPoint(name, commitPoint, timestamp); |
| 49 | + |
| 50 | + if (name == ReactMarkerConstants.FABRIC_BATCH_EXECUTION_END) { |
| 51 | + FLog.i( |
| 52 | + FabricUIManager.TAG, |
| 53 | + "Statistic of Fabric commit #: " |
| 54 | + + instanceKey |
| 55 | + + "\n - Total commit time: " |
| 56 | + + (commitPoint.finishTransactionEnd - commitPoint.commitStart) |
| 57 | + + " ms.\n - Layout: " |
| 58 | + + (commitPoint.layoutEnd - commitPoint.layoutStart) |
| 59 | + + " ms.\n - Diffing: " |
| 60 | + + (commitPoint.diffEnd - commitPoint.diffStart) |
| 61 | + + " ms.\n" |
| 62 | + + " - FinishTransaction (Diffing + Processing + Serialization of MutationInstructions): " |
| 63 | + + (commitPoint.finishTransactionEnd - commitPoint.finishTransactionStart) |
| 64 | + + " ms.\n" |
| 65 | + + " - Mounting: " |
| 66 | + + (commitPoint.batchExecutionEnd - commitPoint.batchExecutionStart) |
| 67 | + + " ms."); |
| 68 | + mFabricCommitMarkers.remove(instanceKey); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static boolean isFabricCommitMarker(ReactMarkerConstants name) { |
| 74 | + return name == FABRIC_COMMIT_START |
| 75 | + || name == FABRIC_COMMIT_END |
| 76 | + || name == FABRIC_FINISH_TRANSACTION_START |
| 77 | + || name == FABRIC_FINISH_TRANSACTION_END |
| 78 | + || name == FABRIC_DIFF_START |
| 79 | + || name == FABRIC_DIFF_END |
| 80 | + || name == FABRIC_LAYOUT_START |
| 81 | + || name == FABRIC_LAYOUT_END |
| 82 | + || name == FABRIC_BATCH_EXECUTION_START |
| 83 | + || name == FABRIC_BATCH_EXECUTION_END |
| 84 | + || name == FABRIC_UPDATE_UI_MAIN_THREAD_START |
| 85 | + || name == FABRIC_UPDATE_UI_MAIN_THREAD_END; |
| 86 | + } |
| 87 | + |
| 88 | + private static void updateFabricCommitPoint( |
| 89 | + ReactMarkerConstants name, FabricCommitPoint commitPoint, long timestamp) { |
| 90 | + switch (name) { |
| 91 | + case FABRIC_COMMIT_START: |
| 92 | + commitPoint.commitStart = timestamp; |
| 93 | + break; |
| 94 | + case FABRIC_COMMIT_END: |
| 95 | + commitPoint.commitEnd = timestamp; |
| 96 | + break; |
| 97 | + case FABRIC_FINISH_TRANSACTION_START: |
| 98 | + commitPoint.finishTransactionStart = timestamp; |
| 99 | + break; |
| 100 | + case FABRIC_FINISH_TRANSACTION_END: |
| 101 | + commitPoint.finishTransactionEnd = timestamp; |
| 102 | + break; |
| 103 | + case FABRIC_DIFF_START: |
| 104 | + commitPoint.diffStart = timestamp; |
| 105 | + break; |
| 106 | + case FABRIC_DIFF_END: |
| 107 | + commitPoint.diffEnd = timestamp; |
| 108 | + break; |
| 109 | + case FABRIC_LAYOUT_START: |
| 110 | + commitPoint.layoutStart = timestamp; |
| 111 | + break; |
| 112 | + case FABRIC_LAYOUT_END: |
| 113 | + commitPoint.layoutEnd = timestamp; |
| 114 | + break; |
| 115 | + case FABRIC_BATCH_EXECUTION_START: |
| 116 | + commitPoint.batchExecutionStart = timestamp; |
| 117 | + break; |
| 118 | + case FABRIC_BATCH_EXECUTION_END: |
| 119 | + commitPoint.batchExecutionEnd = timestamp; |
| 120 | + break; |
| 121 | + case FABRIC_UPDATE_UI_MAIN_THREAD_START: |
| 122 | + commitPoint.updateUIMainThreadStart = timestamp; |
| 123 | + break; |
| 124 | + case FABRIC_UPDATE_UI_MAIN_THREAD_END: |
| 125 | + commitPoint.updateUIMainThreadEnd = timestamp; |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments