Skip to content

Commit 860b24e

Browse files
committed
fix Visu's comments
1 parent 456bbc8 commit 860b24e

File tree

3 files changed

+55
-44
lines changed

3 files changed

+55
-44
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/application/FrameMetricsRecorder.java

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@
3131
*
3232
* <p>IMPORTANT: each recorder holds a reference to an Activity, so it is very important to
3333
* dereference each recorder at or before its Activity's onDestroy. Similar for Fragments.
34+
*
35+
* <p>Relationship of Fragment recording to Activity recording is like a stopwatch. The stopwatch
36+
* itself is for the Activity, Fragments traces are laps in the stopwatch. Stopwatch can only ever
37+
* be for the Activity by-definition because "frames" refer to a frame for a window, and
38+
* FrameMetrics can only come from an Activity's Window. Fragment traces are laps in the stopwatch,
39+
* because the frame metrics data is still for the Activity window, fragment traces are just
40+
* intervals in the Activity frames recording that has the name "fragment X" attached to them.
3441
*/
3542
public class FrameMetricsRecorder {
3643
private static final AndroidLogger logger = AndroidLogger.getInstance();
3744

3845
private final Activity activity;
3946
private final FrameMetricsAggregator frameMetricsAggregator;
40-
private final Map<Fragment, PerfFrameMetrics> subTraceMap;
47+
private final Map<Fragment, PerfFrameMetrics> fragmentSnapshotMap;
4148

4249
private boolean isRecording = false;
4350

@@ -57,7 +64,7 @@ public FrameMetricsRecorder(Activity activity) {
5764
Map<Fragment, PerfFrameMetrics> subTraceMap) {
5865
this.activity = activity;
5966
this.frameMetricsAggregator = frameMetricsAggregator;
60-
this.subTraceMap = subTraceMap;
67+
this.fragmentSnapshotMap = subTraceMap;
6168
}
6269

6370
/** Starts recording FrameMetrics for the activity window. */
@@ -81,9 +88,9 @@ public Optional<PerfFrameMetrics> stop() {
8188
logger.warn("Cannot stop because no recording was started");
8289
return Optional.absent();
8390
}
84-
if (!subTraceMap.isEmpty()) {
91+
if (!fragmentSnapshotMap.isEmpty()) {
8592
logger.warn(
86-
"Sub-traces are still ongoing! Sub-traces should be stopped first before stopping Activity screen trace.");
93+
"Sub-recordings are still ongoing! Sub-recordings should be stopped first before stopping Activity screen trace.");
8794
}
8895
Optional<PerfFrameMetrics> data = this.snapshot();
8996
try {
@@ -99,57 +106,61 @@ public Optional<PerfFrameMetrics> stop() {
99106
}
100107

101108
/**
102-
* Starts a sub-trace in the current recording.
109+
* Starts a Fragment sub-recording in the current Activity recording. Fragments are sub-recordings
110+
* due to the way frame metrics work: frame metrics can only comes from an activity's window. An
111+
* analogy for sub-recording is a lap in a stopwatch.
103112
*
104-
* @param key a UI state to associate this sub-trace with. e.g.) fragment
113+
* @param fragment a Fragment to associate this sub-trace with.
105114
*/
106-
public void startSubTrace(Fragment key) {
115+
public void startFragment(Fragment fragment) {
107116
if (!isRecording) {
108-
logger.warn("Cannot start sub-trace because FrameMetricsAggregator is not recording");
117+
logger.warn("Cannot start sub-recording because FrameMetricsAggregator is not recording");
109118
return;
110119
}
111-
if (subTraceMap.containsKey(key)) {
120+
if (fragmentSnapshotMap.containsKey(fragment)) {
112121
logger.warn(
113-
"Cannot start sub-trace because one is already ongoing with the key %s",
114-
key.getClass().getSimpleName());
122+
"Cannot start sub-recording because one is already ongoing with the key %s",
123+
fragment.getClass().getSimpleName());
115124
return;
116125
}
117126
Optional<PerfFrameMetrics> snapshot = this.snapshot();
118127
if (!snapshot.isAvailable()) {
119-
logger.warn("startSubTrace(%s): snapshot() failed", key.getClass().getSimpleName());
128+
logger.warn("startFragment(%s): snapshot() failed", fragment.getClass().getSimpleName());
120129
return;
121130
}
122-
subTraceMap.put(key, snapshot.get());
131+
fragmentSnapshotMap.put(fragment, snapshot.get());
123132
}
124133

125134
/**
126-
* Stops the sub-trace associated with the given UI state.
135+
* Stops the sub-recording associated with the given Fragment. Fragments are sub-recordings due to
136+
* the way frame metrics work: frame metrics can only comes from an activity's window. An analogy
137+
* for sub-recording is a lap in a stopwatch.
127138
*
128-
* @param key the UI state associated with the sub-trace to be stopped.
129-
* @return FrameMetrics accumulated during this sub-trace.
139+
* @param fragment the Fragment associated with the sub-recording to be stopped.
140+
* @return FrameMetrics accumulated during this sub-recording.
130141
*/
131-
public Optional<PerfFrameMetrics> stopSubTrace(Fragment key) {
142+
public Optional<PerfFrameMetrics> stopFragment(Fragment fragment) {
132143
if (!isRecording) {
133-
logger.warn("Cannot stop sub-trace because FrameMetricsAggregator is not recording");
144+
logger.warn("Cannot stop sub-recording because FrameMetricsAggregator is not recording");
134145
return Optional.absent();
135146
}
136-
if (!subTraceMap.containsKey(key)) {
147+
if (!fragmentSnapshotMap.containsKey(fragment)) {
137148
logger.warn(
138-
"Sub-trace associated with key %s was not started or does not exist",
139-
key.getClass().getSimpleName());
149+
"Sub-recording associated with key %s was not started or does not exist",
150+
fragment.getClass().getSimpleName());
140151
return Optional.absent();
141152
}
142-
PerfFrameMetrics snapshotStart = subTraceMap.remove(key);
153+
PerfFrameMetrics snapshotStart = fragmentSnapshotMap.remove(fragment);
143154
Optional<PerfFrameMetrics> snapshotEnd = this.snapshot();
144155
if (!snapshotEnd.isAvailable()) {
145-
logger.warn("stopSubTrace(%s): snapshot() failed", key.getClass().getSimpleName());
156+
logger.warn("stopFragment(%s): snapshot() failed", fragment.getClass().getSimpleName());
146157
return Optional.absent();
147158
}
148-
return Optional.of(snapshotEnd.get().subtract(snapshotStart));
159+
return Optional.of(snapshotEnd.get().deltaFrameMetricsFromSnapshot(snapshotStart));
149160
}
150161

151162
/**
152-
* Calculate total frames, slow frames, and frozen frames from SparseIntArray[] recorded by {@link
163+
* Snapshots total frames, slow frames, and frozen frames from SparseIntArray[] recorded by {@link
153164
* FrameMetricsAggregator}.
154165
*
155166
* @return {@link PerfFrameMetrics} at the time of snapshot.

firebase-perf/src/main/java/com/google/firebase/perf/metrics/FrameMetricsCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public int getTotalFrames() {
5656
* @param that the subtrahend PerfFrameMetrics object.
5757
* @return difference of this and the argument.
5858
*/
59-
public PerfFrameMetrics subtract(PerfFrameMetrics that) {
59+
public PerfFrameMetrics deltaFrameMetricsFromSnapshot(PerfFrameMetrics that) {
6060
int newTotalFrames = this.totalFrames - that.getTotalFrames();
6161
int newSlowFrames = this.slowFrames - that.getSlowFrames();
6262
int newFrozenFrames = this.frozenFrames - that.getFrozenFrames();

firebase-perf/src/test/java/com/google/firebase/perf/application/FrameMetricsRecorderTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public void startAndStop_calledTwice_ignoresSecondCall() {
8282
assertThat(result2.isAvailable()).isFalse();
8383

8484
assertThat(result1.get().getTotalFrames()).isEqualTo(1 + 3 + 1);
85-
assertThat(result1.get().getTotalFrames()).isEqualTo(3 + 1);
86-
assertThat(result1.get().getTotalFrames()).isEqualTo(1);
85+
assertThat(result1.get().getSlowFrames()).isEqualTo(3 + 1);
86+
assertThat(result1.get().getFrozenFrames()).isEqualTo(1);
8787
}
8888

8989
@Test
@@ -101,9 +101,9 @@ public void startAndStopSubTrace_activityRecordingNeverStarted_returnsEmptyResul
101101
Fragment fragment = new Fragment();
102102

103103
stubFrameMetricsAggregatorData(fma, frameTimes1);
104-
recorder.startSubTrace(fragment);
104+
recorder.startFragment(fragment);
105105
stubFrameMetricsAggregatorData(fma, frameTimes2);
106-
Optional<PerfFrameMetrics> result = recorder.stopSubTrace(fragment);
106+
Optional<PerfFrameMetrics> result = recorder.stopFragment(fragment);
107107
assertThat(result.isAvailable()).isFalse();
108108
}
109109

@@ -114,9 +114,9 @@ public void startAndStopSubTrace_activityRecordingHasEnded_returnsEmptyResult()
114114
recorder.start();
115115
recorder.stop();
116116
stubFrameMetricsAggregatorData(fma, frameTimes1);
117-
recorder.startSubTrace(fragment);
117+
recorder.startFragment(fragment);
118118
stubFrameMetricsAggregatorData(fma, frameTimes2);
119-
Optional<PerfFrameMetrics> result = recorder.stopSubTrace(fragment);
119+
Optional<PerfFrameMetrics> result = recorder.stopFragment(fragment);
120120
assertThat(result.isAvailable()).isFalse();
121121
}
122122

@@ -131,13 +131,13 @@ public void startAndStopSubTrace_whenCalledTwice_ignoresSecondCall() {
131131
// comments are in this format: total frames, slow frames, frozen frames
132132
recorder.start();
133133
stubFrameMetricsAggregatorData(fma, new int[][] {{1, 1}, {17, 1}, {800, 1}}); // 3, 2, 1
134-
recorder.startSubTrace(fragment);
134+
recorder.startFragment(fragment);
135135
stubFrameMetricsAggregatorData(fma, new int[][] {{1, 2}, {17, 3}, {800, 2}}); // ignored
136-
recorder.startSubTrace(fragment); // this call is ignored
136+
recorder.startFragment(fragment); // this call is ignored
137137
stubFrameMetricsAggregatorData(fma, new int[][] {{1, 5}, {17, 4}, {800, 5}}); // 14, 9, 5
138-
Optional<PerfFrameMetrics> result1 = recorder.stopSubTrace(fragment);
138+
Optional<PerfFrameMetrics> result1 = recorder.stopFragment(fragment);
139139
stubFrameMetricsAggregatorData(fma, new int[][] {{1, 6}, {17, 5}, {800, 5}}); // ignored
140-
Optional<PerfFrameMetrics> result2 = recorder.stopSubTrace(fragment); // this call is ignored
140+
Optional<PerfFrameMetrics> result2 = recorder.stopFragment(fragment); // this call is ignored
141141

142142
// total = 14 - 3 = 11, slow = 9 - 2 = 7, frozen = 5 - 1 = 4
143143
assertThat(result1.get().getTotalFrames()).isEqualTo(14 - 3);
@@ -153,8 +153,8 @@ public void stopAndStopSubTrace_whenNoSubTraceWithGivenKeyExists_returnsEmptyRes
153153

154154
recorder.start();
155155

156-
recorder.startSubTrace(fragment1);
157-
Optional<PerfFrameMetrics> result = recorder.stopSubTrace(fragment2);
156+
recorder.startFragment(fragment1);
157+
Optional<PerfFrameMetrics> result = recorder.stopFragment(fragment2);
158158

159159
assertThat(result.isAvailable()).isFalse();
160160
}
@@ -164,9 +164,9 @@ public void startAndStopSubTrace_duringActivityRecording_returnsValidResult() {
164164
Fragment fragment = new Fragment();
165165
recorder.start();
166166
stubFrameMetricsAggregatorData(fma, frameTimes1);
167-
recorder.startSubTrace(fragment);
167+
recorder.startFragment(fragment);
168168
stubFrameMetricsAggregatorData(fma, frameTimes2);
169-
Optional<PerfFrameMetrics> result = recorder.stopSubTrace(fragment);
169+
Optional<PerfFrameMetrics> result = recorder.stopFragment(fragment);
170170
assertThat(result.isAvailable()).isTrue();
171171
// frameTimes2 - frameTimes1
172172
assertThat(result.get().getTotalFrames()).isEqualTo(9);
@@ -181,13 +181,13 @@ public void startAndStopSubTrace_whenTwoSubTracesOverlap_returnsCorrectResults()
181181
recorder.start();
182182
// comments are in this format: total frames, slow frames, frozen frames
183183
stubFrameMetricsAggregatorData(fma, new int[][] {{1, 1}, {17, 1}, {800, 1}}); // 3, 2, 1
184-
recorder.startSubTrace(fragment1);
184+
recorder.startFragment(fragment1);
185185
stubFrameMetricsAggregatorData(fma, new int[][] {{1, 2}, {17, 3}, {800, 2}}); // 7, 5, 2
186-
recorder.startSubTrace(fragment2);
186+
recorder.startFragment(fragment2);
187187
stubFrameMetricsAggregatorData(fma, new int[][] {{1, 5}, {17, 4}, {800, 5}}); // 14, 9, 5
188-
Optional<PerfFrameMetrics> subTrace1 = recorder.stopSubTrace(fragment1);
188+
Optional<PerfFrameMetrics> subTrace1 = recorder.stopFragment(fragment1);
189189
stubFrameMetricsAggregatorData(fma, new int[][] {{1, 6}, {17, 5}, {800, 5}}); // 16, 10, 5
190-
Optional<PerfFrameMetrics> subTrace2 = recorder.stopSubTrace(fragment2);
190+
Optional<PerfFrameMetrics> subTrace2 = recorder.stopFragment(fragment2);
191191

192192
// 3rd snapshot - 1st snapshot
193193
assertThat(subTrace1.get().getTotalFrames()).isEqualTo(14 - 3);

0 commit comments

Comments
 (0)