Skip to content

Fireperf: Add Fragment Screen Performance Support #3591

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

Merged
merged 16 commits into from
Apr 7, 2022
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
1 change: 1 addition & 0 deletions firebase-perf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Refer [GMaven](https://maven.google.com/web/index.html?q=firebase-perf#com.googl
## Unreleased

* {{fixed}} Fixed a bug where screen traces were not capturing frame metrics for multi-activity apps.
* {{feature}} Added support for measuring screen performance metrics for "Activity Fragments" out-of-the-box.

## Released

Expand Down
3 changes: 3 additions & 0 deletions firebase-perf/dev-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<meta-data
android:name="sessions_sampling_percentage"
android:value="100.0" />
<meta-data
android:name="fragment_sampling_percentage"
android:value="100.0" />

<receiver
android:name=".FirebasePerfTestReceiver"
Expand Down
3 changes: 3 additions & 0 deletions firebase-perf/e2e-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<meta-data
android:name="sessions_sampling_percentage"
android:value="100.0" />
<meta-data
android:name="fragment_sampling_percentage"
android:value="100.0" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.Context;
import android.os.Bundle;
import android.util.SparseIntArray;
import androidx.annotation.NonNull;
import androidx.core.app.FrameMetricsAggregator;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.common.util.VisibleForTesting;
import com.google.firebase.perf.config.ConfigResolver;
import com.google.firebase.perf.logging.AndroidLogger;
import com.google.firebase.perf.metrics.FrameMetricsCalculator;
import com.google.firebase.perf.metrics.Trace;
import com.google.firebase.perf.session.SessionManager;
import com.google.firebase.perf.transport.TransportManager;
import com.google.firebase.perf.util.Clock;
import com.google.firebase.perf.util.Constants;
import com.google.firebase.perf.util.Constants.CounterNames;
import com.google.firebase.perf.util.ScreenTraceUtil;
import com.google.firebase.perf.util.Timer;
import com.google.firebase.perf.util.Utils;
import com.google.firebase.perf.v1.ApplicationProcessState;
import com.google.firebase.perf.v1.TraceMetric;
import java.lang.ref.WeakReference;
Expand All @@ -52,6 +53,7 @@ public class AppStateMonitor implements ActivityLifecycleCallbacks {
"androidx.core.app.FrameMetricsAggregator";

private static volatile AppStateMonitor instance;
private static boolean hasFrameMetricsAggregator = false;

private final WeakHashMap<Activity, Boolean> activityToResumedMap = new WeakHashMap<>();
private final WeakHashMap<Activity, Trace> activityToScreenTraceMap = new WeakHashMap<>();
Expand All @@ -75,7 +77,6 @@ public class AppStateMonitor implements ActivityLifecycleCallbacks {

private boolean isRegisteredForLifecycleCallbacks = false;
private boolean isColdStart = true;
private boolean hasFrameMetricsAggregator = false;

public static AppStateMonitor getInstance() {
if (instance == null) {
Expand All @@ -89,13 +90,22 @@ public static AppStateMonitor getInstance() {
}

AppStateMonitor(TransportManager transportManager, Clock clock) {
this(
transportManager,
clock,
ConfigResolver.getInstance(),
hasFrameMetricsAggregatorClass() ? new FrameMetricsAggregator() : null);
}

AppStateMonitor(
TransportManager transportManager,
Clock clock,
ConfigResolver configResolver,
FrameMetricsAggregator frameMetricsAggregator) {
this.transportManager = transportManager;
this.clock = clock;
configResolver = ConfigResolver.getInstance();
hasFrameMetricsAggregator = hasFrameMetricsAggregatorClass();
if (hasFrameMetricsAggregator) {
frameMetricsAggregator = new FrameMetricsAggregator();
}
this.configResolver = configResolver;
this.frameMetricsAggregator = frameMetricsAggregator;
}

public synchronized void registerActivityLifecycleCallbacks(Context context) {
Expand Down Expand Up @@ -140,7 +150,18 @@ public void incrementTsnsCount(int value) {
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (isScreenTraceSupported() && configResolver.isPerformanceMonitoringEnabled()) {
if (activity instanceof FragmentActivity) {
FragmentActivity fragmentActivity = (FragmentActivity) activity;
fragmentActivity
.getSupportFragmentManager()
.registerFragmentLifecycleCallbacks(
new FragmentStateMonitor(clock, transportManager, this, frameMetricsAggregator),
true);
}
}
}

@Override
public void onActivityDestroyed(Activity activity) {}
Expand Down Expand Up @@ -192,7 +213,7 @@ public synchronized void onActivityResumed(Activity activity) {
}

// Screen trace is after session update so the sessionId is not added twice to the Trace
if (isScreenTraceSupported(activity) && configResolver.isPerformanceMonitoringEnabled()) {
if (isScreenTraceSupported() && configResolver.isPerformanceMonitoringEnabled()) {
// Starts recording frame metrics for this activity.
/**
* TODO: Only add activities that are hardware acceleration enabled so that calling {@link
Expand Down Expand Up @@ -297,7 +318,7 @@ public void onActivityPaused(Activity activity) {}
/** Stops screen trace right after onPause because of b/210055697 */
@Override
public void onActivityPostPaused(@NonNull Activity activity) {
if (isScreenTraceSupported(activity)) {
if (isScreenTraceSupported()) {
sendScreenTrace(activity);
}
}
Expand Down Expand Up @@ -333,50 +354,15 @@ private void sendScreenTrace(Activity activity) {
} catch (IllegalArgumentException ignored) {
logger.debug("View not hardware accelerated. Unable to collect screen trace.");
}
SparseIntArray[] arr = frameMetricsAggregator.reset();
if (arr != null) {
SparseIntArray frameTimes = arr[FrameMetricsAggregator.TOTAL_INDEX];
if (frameTimes != null) {
for (int i = 0; i < frameTimes.size(); i++) {
int frameTime = frameTimes.keyAt(i);
int numFrames = frameTimes.valueAt(i);
totalFrames += numFrames;
if (frameTime > Constants.FROZEN_FRAME_TIME) {
// Frozen frames mean the app appear frozen. The recommended thresholds is 700ms
frozenFrames += numFrames;
}
if (frameTime > Constants.SLOW_FRAME_TIME) {
// Slow frames are anything above 16ms (i.e. 60 frames/second)
slowFrames += numFrames;
}
}
}
}
if (totalFrames == 0 && slowFrames == 0 && frozenFrames == 0) {
FrameMetricsCalculator.PerfFrameMetrics perfFrameMetrics =
FrameMetricsCalculator.calculateFrameMetrics(frameMetricsAggregator.reset());
if (perfFrameMetrics.getTotalFrames() == 0
&& perfFrameMetrics.getSlowFrames() == 0
&& perfFrameMetrics.getFrozenFrames() == 0) {
// All metrics are zero, no need to send screen trace.
// return;
}
// Only incrementMetric if corresponding metric is non-zero.
if (totalFrames > 0) {
screenTrace.putMetric(Constants.CounterNames.FRAMES_TOTAL.toString(), totalFrames);
}
if (slowFrames > 0) {
screenTrace.putMetric(Constants.CounterNames.FRAMES_SLOW.toString(), slowFrames);
}
if (frozenFrames > 0) {
screenTrace.putMetric(Constants.CounterNames.FRAMES_FROZEN.toString(), frozenFrames);
}
if (Utils.isDebugLoggingEnabled(activity.getApplicationContext())) {
logger.debug(
"sendScreenTrace name:"
+ getScreenTraceName(activity)
+ " _fr_tot:"
+ totalFrames
+ " _fr_slo:"
+ slowFrames
+ " _fr_fzn:"
+ frozenFrames);
return;
}
ScreenTraceUtil.addFrameCounters(screenTrace, perfFrameMetrics);
// Stop and record trace
screenTrace.stop();
}
Expand Down Expand Up @@ -418,10 +404,9 @@ private void sendSessionLog(String name, Timer startTime, Timer endTime) {
/**
* Only send screen trace if FrameMetricsAggregator exists.
*
* @param activity The Activity for which we're monitoring the screen rendering performance.
* @return true if supported, false if not.
*/
private boolean isScreenTraceSupported(Activity activity) {
protected boolean isScreenTraceSupported() {
return hasFrameMetricsAggregator;
}

Expand All @@ -430,11 +415,13 @@ private boolean isScreenTraceSupported(Activity activity) {
* updated to 26.1.0 (b/69954793), there will be ClassNotFoundException. This method is to check
* if FrameMetricsAggregator exists to avoid ClassNotFoundException.
*/
private boolean hasFrameMetricsAggregatorClass() {
private static boolean hasFrameMetricsAggregatorClass() {
try {
Class<?> initializerClass = Class.forName(FRAME_METRICS_AGGREGATOR_CLASSNAME);
hasFrameMetricsAggregator = true;
return true;
} catch (ClassNotFoundException e) {
hasFrameMetricsAggregator = false;
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.perf.application;

import androidx.annotation.NonNull;
import androidx.core.app.FrameMetricsAggregator;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.google.android.gms.common.util.VisibleForTesting;
import com.google.firebase.perf.logging.AndroidLogger;
import com.google.firebase.perf.metrics.FrameMetricsCalculator;
import com.google.firebase.perf.metrics.FrameMetricsCalculator.PerfFrameMetrics;
import com.google.firebase.perf.metrics.Trace;
import com.google.firebase.perf.transport.TransportManager;
import com.google.firebase.perf.util.Clock;
import com.google.firebase.perf.util.Constants;
import com.google.firebase.perf.util.ScreenTraceUtil;
import java.util.WeakHashMap;

public class FragmentStateMonitor extends FragmentManager.FragmentLifecycleCallbacks {
private static final AndroidLogger logger = AndroidLogger.getInstance();
private final WeakHashMap<Fragment, Trace> fragmentToTraceMap = new WeakHashMap<>();
private final WeakHashMap<Fragment, PerfFrameMetrics> fragmentToMetricsMap = new WeakHashMap<>();
private final Clock clock;
private final TransportManager transportManager;
private final AppStateMonitor appStateMonitor;
private final FrameMetricsAggregator frameMetricsAggregator;

public FragmentStateMonitor(
Clock clock,
TransportManager transportManager,
AppStateMonitor appStateMonitor,
FrameMetricsAggregator fma) {
this.clock = clock;
this.transportManager = transportManager;
this.appStateMonitor = appStateMonitor;
this.frameMetricsAggregator = fma;
}

/**
* Fragment screen trace name is prefix "_st_" concatenates with Fragment's class name.
*
* @param fragment fragment object.
* @return Fragment screen trace name.
*/
public String getFragmentScreenTraceName(Fragment fragment) {
return Constants.SCREEN_TRACE_PREFIX + fragment.getClass().getSimpleName();
}

@Override
public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) {
super.onFragmentResumed(fm, f);
// Start Fragment screen trace
logger.debug("FragmentMonitor %s.onFragmentResumed", f.getClass().getSimpleName());
Trace fragmentTrace =
new Trace(getFragmentScreenTraceName(f), transportManager, clock, appStateMonitor);
fragmentTrace.start();

if (f.getParentFragment() != null) {
fragmentTrace.putAttribute(
Constants.PARENT_FRAGMENT_ATTRIBUTE_KEY,
f.getParentFragment().getClass().getSimpleName());
}
if (f.getActivity() != null) {
fragmentTrace.putAttribute(
Constants.ACTIVITY_ATTRIBUTE_KEY, f.getActivity().getClass().getSimpleName());
}
fragmentToTraceMap.put(f, fragmentTrace);

PerfFrameMetrics perfFrameMetrics =
FrameMetricsCalculator.calculateFrameMetrics(this.frameMetricsAggregator.getMetrics());
fragmentToMetricsMap.put(f, perfFrameMetrics);
}

@Override
public void onFragmentPaused(@NonNull FragmentManager fm, @NonNull Fragment f) {
super.onFragmentPaused(fm, f);
// Stop Fragment screen trace
logger.debug("FragmentMonitor %s.onFragmentPaused ", f.getClass().getSimpleName());
if (!fragmentToTraceMap.containsKey(f)) {
logger.warn("FragmentMonitor: missed a fragment trace from %s", f.getClass().getSimpleName());
return;
}

Trace fragmentTrace = fragmentToTraceMap.get(f);
fragmentToTraceMap.remove(f);
PerfFrameMetrics prePerfFrameMetrics = fragmentToMetricsMap.get(f);
fragmentToMetricsMap.remove(f);

PerfFrameMetrics curPerfFrameMetrics =
FrameMetricsCalculator.calculateFrameMetrics(this.frameMetricsAggregator.getMetrics());

int totalFrames = curPerfFrameMetrics.getTotalFrames() - prePerfFrameMetrics.getTotalFrames();
int slowFrames = curPerfFrameMetrics.getSlowFrames() - prePerfFrameMetrics.getSlowFrames();
int frozenFrames =
curPerfFrameMetrics.getFrozenFrames() - prePerfFrameMetrics.getFrozenFrames();

if (totalFrames == 0 && slowFrames == 0 && frozenFrames == 0) {
// All metrics are zero, no need to send screen trace.
return;
}
ScreenTraceUtil.addFrameCounters(
fragmentTrace, new PerfFrameMetrics(totalFrames, slowFrames, frozenFrames));
fragmentTrace.stop();
}

@VisibleForTesting
WeakHashMap<Fragment, Trace> getFragmentToTraceMap() {
return fragmentToTraceMap;
}

@VisibleForTesting
WeakHashMap<Fragment, PerfFrameMetrics> getFragmentToMetricsMap() {
return fragmentToMetricsMap;
}
}
Loading