File tree Expand file tree Collapse file tree 4 files changed +221
-318
lines changed
main/java/com/google/firebase/perf/session/gauges
test/java/com/google/firebase/perf Expand file tree Collapse file tree 4 files changed +221
-318
lines changed Original file line number Diff line number Diff line change 14
14
15
15
package com.google.firebase.perf.session.gauges
16
16
17
+ import androidx.annotation.VisibleForTesting
18
+ import com.google.firebase.perf.logging.AndroidLogger
17
19
import java.util.concurrent.atomic.AtomicInteger
18
20
19
21
/* *
@@ -23,17 +25,31 @@ import java.util.concurrent.atomic.AtomicInteger
23
25
object GaugeCounter {
24
26
private const val MAX_METRIC_COUNT = 25
25
27
private val counter = AtomicInteger (0 )
26
- private val gaugeManager: GaugeManager = GaugeManager .getInstance()
28
+ private val logger = AndroidLogger .getInstance()
29
+
30
+ @set:VisibleForTesting(otherwise = VisibleForTesting .NONE )
31
+ @set:JvmStatic
32
+ var gaugeManager: GaugeManager = GaugeManager .getInstance()
27
33
28
34
fun incrementCounter () {
29
35
val metricsCount = counter.incrementAndGet()
30
36
31
37
if (metricsCount >= MAX_METRIC_COUNT ) {
32
38
gaugeManager.logGaugeMetrics()
33
39
}
40
+
41
+ logger.debug(" Incremented logger to $metricsCount " )
34
42
}
35
43
36
44
fun decrementCounter () {
37
- counter.decrementAndGet()
45
+ val curr = counter.decrementAndGet()
46
+ logger.debug(" Decremented logger to $curr " )
38
47
}
48
+
49
+ @VisibleForTesting(otherwise = VisibleForTesting .NONE )
50
+ fun resetCounter () {
51
+ counter.set(0 )
52
+ }
53
+
54
+ @VisibleForTesting(otherwise = VisibleForTesting .NONE ) fun count (): Int = counter.get()
39
55
}
Original file line number Diff line number Diff line change @@ -100,13 +100,19 @@ public void initializeGaugeMetadataManager(Context appContext) {
100
100
101
101
@ Override
102
102
public void onUpdateAppState (ApplicationProcessState applicationProcessState ) {
103
- this .applicationProcessState = applicationProcessState ;
104
-
103
+ // If it isn't a verbose session (or unset) update the app state and return.
105
104
if (session == null || !session .isVerbose ()) {
105
+ this .applicationProcessState = applicationProcessState ;
106
106
return ;
107
107
}
108
108
109
- // If it's a verbose session, start collecting gauges for the new app state.
109
+ // Log existing gauges to the current app state.
110
+ logGaugeMetrics ();
111
+
112
+ // Update App State.
113
+ this .applicationProcessState = applicationProcessState ;
114
+
115
+ // Start collecting gauges for the new app state.
110
116
startCollectingGauges (this .applicationProcessState , session .getTimer ());
111
117
}
112
118
@@ -132,6 +138,7 @@ public void startCollectingGauges(PerfSession session) {
132
138
stopCollectingGauges ();
133
139
}
134
140
141
+ // TODO(b/394127311): Explore always setting the app state as FOREGROUND.
135
142
ApplicationProcessState gaugeCollectionApplicationProcessState = applicationProcessState ;
136
143
if (gaugeCollectionApplicationProcessState
137
144
== ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN ) {
@@ -419,4 +426,9 @@ private long getMemoryGaugeCollectionFrequencyMs(
419
426
return memoryGaugeCollectionFrequency ;
420
427
}
421
428
}
429
+
430
+ @ VisibleForTesting
431
+ void setApplicationProcessState (ApplicationProcessState applicationProcessState ) {
432
+ this .applicationProcessState = applicationProcessState ;
433
+ }
422
434
}
Original file line number Diff line number Diff line change 25
25
import com .google .firebase .perf .config .ConfigResolver ;
26
26
import com .google .firebase .perf .session .PerfSession ;
27
27
import com .google .firebase .perf .session .SessionManager ;
28
+ import com .google .firebase .perf .session .gauges .GaugeCounter ;
28
29
import com .google .firebase .perf .util .ImmutableBundle ;
29
30
import org .junit .After ;
30
31
import org .junit .Before ;
32
+ import org .junit .BeforeClass ;
31
33
import org .robolectric .shadows .ShadowPackageManager ;
32
34
33
35
public class FirebasePerformanceTestBase {
@@ -54,6 +56,12 @@ public class FirebasePerformanceTestBase {
54
56
55
57
protected Context appContext ;
56
58
59
+ @ BeforeClass
60
+ public static void setUpBeforeClass () {
61
+ // TODO(b/394127311): Explore removing this.
62
+ GaugeCounter .INSTANCE .resetCounter ();
63
+ }
64
+
57
65
@ Before
58
66
public void setUpFirebaseApp () {
59
67
appContext = ApplicationProvider .getApplicationContext ();
You can’t perform that action at this time.
0 commit comments