File tree Expand file tree Collapse file tree 8 files changed +30
-12
lines changed
main/java/com/google/firebase/perf/session/gauges
test/java/com/google/firebase/perf Expand file tree Collapse file tree 8 files changed +30
-12
lines changed Original file line number Diff line number Diff line change @@ -166,7 +166,7 @@ private synchronized void scheduleCpuMetricCollectionWithRate(
166166 CpuMetricReading currCpuReading = syncCollectCpuMetric (referenceTime );
167167 if (currCpuReading != null ) {
168168 cpuMetricReadings .add (currCpuReading );
169- GaugeCounter .INSTANCE . incrementCounter ();
169+ GaugeCounter .incrementCounter ();
170170 }
171171 },
172172 /* initialDelay */ 0 ,
@@ -186,7 +186,7 @@ private synchronized void scheduleCpuMetricCollectionOnce(Timer referenceTime) {
186186 CpuMetricReading currCpuReading = syncCollectCpuMetric (referenceTime );
187187 if (currCpuReading != null ) {
188188 cpuMetricReadings .add (currCpuReading );
189- GaugeCounter .INSTANCE . incrementCounter ();
189+ GaugeCounter .incrementCounter ();
190190 }
191191 },
192192 /* initialDelay */ 0 ,
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ object GaugeCounter {
3131 @set:JvmStatic
3232 var gaugeManager: GaugeManager = GaugeManager .getInstance()
3333
34+ @JvmStatic
3435 fun incrementCounter () {
3536 val metricsCount = counter.incrementAndGet()
3637
@@ -41,15 +42,17 @@ object GaugeCounter {
4142 logger.debug(" Incremented logger to $metricsCount " )
4243 }
4344
45+ @JvmStatic
4446 fun decrementCounter () {
4547 val curr = counter.decrementAndGet()
4648 logger.debug(" Decremented logger to $curr " )
4749 }
4850
4951 @VisibleForTesting(otherwise = VisibleForTesting .NONE )
52+ @JvmStatic
5053 fun resetCounter () {
5154 counter.set(0 )
5255 }
5356
54- @VisibleForTesting(otherwise = VisibleForTesting .NONE ) fun count (): Int = counter.get()
57+ @VisibleForTesting(otherwise = VisibleForTesting .NONE ) @JvmStatic fun count (): Int = counter.get()
5558}
Original file line number Diff line number Diff line change @@ -250,19 +250,18 @@ private void logExistingGaugeMetrics(
250250 */
251251 private void syncFlush (String sessionId , ApplicationProcessState appState ) {
252252 GaugeMetric .Builder gaugeMetricBuilder = GaugeMetric .newBuilder ();
253- GaugeCounter gaugeCounter = GaugeCounter .INSTANCE ;
254253
255254 // Adding CPU metric readings.
256255 while (!cpuGaugeCollector .get ().cpuMetricReadings .isEmpty ()) {
257256 gaugeMetricBuilder .addCpuMetricReadings (cpuGaugeCollector .get ().cpuMetricReadings .poll ());
258- gaugeCounter .decrementCounter ();
257+ GaugeCounter .decrementCounter ();
259258 }
260259
261260 // Adding Memory metric readings.
262261 while (!memoryGaugeCollector .get ().memoryMetricReadings .isEmpty ()) {
263262 gaugeMetricBuilder .addAndroidMemoryReadings (
264263 memoryGaugeCollector .get ().memoryMetricReadings .poll ());
265- gaugeCounter .decrementCounter ();
264+ GaugeCounter .decrementCounter ();
266265 }
267266
268267 // Adding Session ID info.
Original file line number Diff line number Diff line change @@ -129,7 +129,7 @@ private synchronized void scheduleMemoryMetricCollectionWithRate(
129129 AndroidMemoryReading memoryReading = syncCollectMemoryMetric (referenceTime );
130130 if (memoryReading != null ) {
131131 memoryMetricReadings .add (memoryReading );
132- GaugeCounter .INSTANCE . incrementCounter ();
132+ GaugeCounter .incrementCounter ();
133133 }
134134 },
135135 /* initialDelay */ 0 ,
@@ -149,7 +149,7 @@ private synchronized void scheduleMemoryMetricCollectionOnce(Timer referenceTime
149149 AndroidMemoryReading memoryReading = syncCollectMemoryMetric (referenceTime );
150150 if (memoryReading != null ) {
151151 memoryMetricReadings .add (memoryReading );
152- GaugeCounter .INSTANCE . incrementCounter ();
152+ GaugeCounter .incrementCounter ();
153153 }
154154 },
155155 /* initialDelay */ 0 ,
Original file line number Diff line number Diff line change @@ -59,7 +59,7 @@ public class FirebasePerformanceTestBase {
5959 @ BeforeClass
6060 public static void setUpBeforeClass () {
6161 // TODO(b/394127311): Explore removing this.
62- GaugeCounter .INSTANCE . resetCounter ();
62+ GaugeCounter .resetCounter ();
6363 }
6464
6565 @ Before
Original file line number Diff line number Diff line change @@ -60,9 +60,11 @@ public void tearDown() {
6060 @ Test
6161 public void testStartCollectingAddsCpuMetricReadingsToTheConcurrentLinkedQueue ()
6262 throws Exception {
63+ int priorGaugeCount = GaugeCounter .count ();
6364 testGaugeCollector .startCollecting (100 , new Timer ());
6465 fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
6566 assertThat (testGaugeCollector .cpuMetricReadings ).hasSize (1 );
67+ assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCount + 1 );
6668 }
6769
6870 @ Test
@@ -246,11 +248,17 @@ public void testCollectCpuMetricDoesntStartCollectingWithInvalidCpuMetricCollect
246248
247249 @ Test
248250 public void testCollectOnce_addOnlyOneCpuMetricReadingToQueue () {
251+ int priorGaugeCount = GaugeCounter .count ();
249252 assertThat (testGaugeCollector .cpuMetricReadings ).isEmpty ();
250253 testGaugeCollector .collectOnce (new Timer ());
251254
252255 fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
256+
257+ // Simulate running an additional task.
258+ fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
259+
253260 assertThat (testGaugeCollector .cpuMetricReadings ).hasSize (1 );
261+ assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCount + 1 );
254262 }
255263
256264 @ Test
Original file line number Diff line number Diff line change @@ -429,7 +429,7 @@ public void testGaugeManagerHandlesMultipleSessionIds() {
429429 fakeSession .setGaugeAndEventCollectionEnabled (true );
430430 testGaugeManager .setApplicationProcessState (ApplicationProcessState .BACKGROUND );
431431 testGaugeManager .startCollectingGauges (fakeSession );
432- GaugeCounter .INSTANCE . setGaugeManager (testGaugeManager );
432+ GaugeCounter .setGaugeManager (testGaugeManager );
433433
434434 // Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
435435 generateMetricsAndIncrementCounter (10 );
@@ -615,10 +615,10 @@ private void generateMetricsAndIncrementCounter(int count) {
615615 for (int i = 0 ; i < count ; ++i ) {
616616 if (random .nextInt (2 ) == 0 ) {
617617 fakeCpuGaugeCollector .cpuMetricReadings .add (createFakeCpuMetricReading (100 , 200 ));
618- GaugeCounter .INSTANCE . incrementCounter ();
618+ GaugeCounter .incrementCounter ();
619619 } else {
620620 fakeMemoryGaugeCollector .memoryMetricReadings .add (createFakeAndroidMetricReading (100 ));
621- GaugeCounter .INSTANCE . incrementCounter ();
621+ GaugeCounter .incrementCounter ();
622622 }
623623 }
624624 }
Original file line number Diff line number Diff line change @@ -59,9 +59,11 @@ private void mockMemory() {
5959
6060 @ Test
6161 public void testStartCollecting_addsMemoryMetricReadingsToQueue () {
62+ int priorGaugeCount = GaugeCounter .count ();
6263 testGaugeCollector .startCollecting (/* memoryMetricCollectionRateMs= */ 100 , new Timer ());
6364 fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
6465 assertThat (testGaugeCollector .memoryMetricReadings ).hasSize (1 );
66+ assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCount + 1 );
6567 }
6668
6769 @ Test
@@ -148,11 +150,17 @@ public void testCollectedMemoryMetric_containsApproximatelyCorrectTimestamp() {
148150
149151 @ Test
150152 public void testCollectOnce_addOnlyOneMemoryMetricReadingToQueue () {
153+ int priorGaugeCount = GaugeCounter .count ();
151154 assertThat (testGaugeCollector .memoryMetricReadings ).isEmpty ();
152155 testGaugeCollector .collectOnce (new Timer ());
153156
154157 fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
158+
159+ // Simulate running an additional task.
160+ fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
161+
155162 assertThat (testGaugeCollector .memoryMetricReadings ).hasSize (1 );
163+ assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCount + 1 );
156164 }
157165
158166 @ Test
You can’t perform that action at this time.
0 commit comments