Skip to content

Commit

Permalink
SKETCH Add performance tracking for rendering
Browse files Browse the repository at this point in the history
Differential Revision: D3709400

fbshipit-source-id: a006b60feb3fc5cb55cc2e6f08275fcc8de1d3e1
  • Loading branch information
Scott Buckfelder authored and Facebook Github Bot 4 committed Aug 30, 2016
1 parent 5d32075 commit 16f76d4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

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;
Expand Down Expand Up @@ -143,6 +145,20 @@ public void removeLifecycleEventListener(LifecycleEventListener listener) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -146,6 +149,14 @@ public void updateRootNodeSize(
}
}

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.
*/
Expand Down Expand Up @@ -739,10 +750,13 @@ protected void calculateRootLayout(ReactShadowNode cssRoot) {
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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import javax.annotation.Nullable;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.facebook.common.logging.FLog;
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;
Expand Down Expand Up @@ -60,7 +62,7 @@
* 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
Expand Down Expand Up @@ -135,6 +137,13 @@ private static Map<String, Object> createConstants(List<ViewManager> viewManager
}
}

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.
Expand Down

0 comments on commit 16f76d4

Please sign in to comment.