SKETCH Add performance tracking for rendering

Differential Revision: D3709400

fbshipit-source-id: a006b60feb3fc5cb55cc2e6f08275fcc8de1d3e1
This commit is contained in:
Scott Buckfelder 2016-08-30 14:15:48 -07:00 committed by Facebook Github Bot 4
parent 5d32075363
commit 16f76d4407
4 changed files with 55 additions and 1 deletions

View File

@ -0,0 +1,15 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.bridge;
import java.util.Map;
public interface PerformanceCounter {
public Map<String,Double> getPerformanceCounters();
}

View File

@ -13,6 +13,8 @@ import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
@ -143,6 +145,20 @@ public class ReactContext extends ContextWrapper {
mLifecycleEventListeners.remove(listener);
}
public Map<String, Map<String,Double>> getAllPerformanceCounters() {
Map<String, Map<String,Double>> totalPerfMap =
new HashMap<>();
if (mCatalystInstance != null) {
for (NativeModule nativeModule : mCatalystInstance.getNativeModules()) {
if (nativeModule instanceof PerformanceCounter) {
PerformanceCounter perfCounterModule = (PerformanceCounter) nativeModule;
totalPerfMap.put(nativeModule.getName(), perfCounterModule.getPerformanceCounters());
}
}
}
return totalPerfMap;
}
public void addActivityEventListener(ActivityEventListener listener) {
mActivityEventListeners.add(listener);
}

View File

@ -46,6 +46,9 @@ public class UIImplementation {
private final int[] mMeasureBuffer = new int[4];
private final ReactApplicationContext mReactContext;
private double mLayoutCount = 0.0;
private double mLayoutTimer = 0.0;
public UIImplementation(ReactApplicationContext reactContext, List<ViewManager> viewManagers) {
this(reactContext, new ViewManagerRegistry(viewManagers));
}
@ -146,6 +149,14 @@ public class UIImplementation {
}
}
public double getLayoutCount() {
return mLayoutCount;
}
public double getLayoutTimer() {
return mLayoutTimer;
}
/**
* Invoked by React to create a new node with a given tag, class name and properties.
*/
@ -739,10 +750,13 @@ public class UIImplementation {
SystraceMessage.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "cssRoot.calculateLayout")
.arg("rootTag", cssRoot.getReactTag())
.flush();
double startTime = (double) System.nanoTime();
try {
cssRoot.calculateLayout(mLayoutContext);
} finally {
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
mLayoutTimer = mLayoutTimer + ((double)System.nanoTime() - startTime)/ 1000000000.0;
mLayoutCount = mLayoutCount + 1;
}
}

View File

@ -12,6 +12,7 @@ package com.facebook.react.uimanager;
import javax.annotation.Nullable;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.facebook.common.logging.FLog;
@ -19,6 +20,7 @@ import com.facebook.react.animation.Animation;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.OnBatchCompleteListener;
import com.facebook.react.bridge.PerformanceCounter;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
@ -60,7 +62,7 @@ import com.facebook.systrace.SystraceMessage;
* TODO(5483063): Don't dispatch the view hierarchy at the end of a batch if no UI changes occurred
*/
public class UIManagerModule extends ReactContextBaseJavaModule implements
OnBatchCompleteListener, LifecycleEventListener {
OnBatchCompleteListener, LifecycleEventListener, PerformanceCounter {
// Keep in sync with ReactIOSTagHandles JS module - see that file for an explanation on why the
// increment here is 10
@ -135,6 +137,13 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
}
}
public Map<String,Double> getPerformanceCounters() {
Map<String,Double> perfMap = new HashMap<>();
perfMap.put("LayoutCount", mUIImplementation.getLayoutCount());
perfMap.put("LayoutTimer", mUIImplementation.getLayoutTimer());
return perfMap;
}
/**
* Registers a new root view. JS can use the returned tag with manageChildren to add/remove
* children to this view.