@@ -60,12 +60,14 @@ public final class GaugeManagerTest extends FirebasePerformanceTestBase {
60
60
// This is a guesstimate of the max amount of time to wait before any pending metrics' collection
61
61
// might take.
62
62
private static final long TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS = 20 ;
63
- private static final long APPROX_NUMBER_OF_DATA_POINTS_PER_GAUGE_METRIC = 20 ;
64
63
private static final long DEFAULT_CPU_GAUGE_COLLECTION_FREQUENCY_BG_MS = 100 ;
65
64
private static final long DEFAULT_CPU_GAUGE_COLLECTION_FREQUENCY_FG_MS = 50 ;
66
65
private static final long DEFAULT_MEMORY_GAUGE_COLLECTION_FREQUENCY_BG_MS = 120 ;
67
66
private static final long DEFAULT_MEMORY_GAUGE_COLLECTION_FREQUENCY_FG_MS = 60 ;
68
67
68
+ // See [com.google.firebase.perf.session.gauges.GaugeCounter].
69
+ private static final long MAX_GAUGE_COUNTER_LIMIT = 50 ;
70
+
69
71
private GaugeManager testGaugeManager = null ;
70
72
private FakeScheduledExecutorService fakeScheduledExecutorService = null ;
71
73
private TransportManager mockTransportManager = null ;
@@ -340,8 +342,7 @@ public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() {
340
342
// There's no job to log the gauges.
341
343
assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
342
344
343
- // Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
344
- generateMetricsAndIncrementCounter (20 );
345
+ generateMetricsAndIncrementCounter (MAX_GAUGE_COUNTER_LIMIT - 10 );
345
346
346
347
// There's still no job to log the gauges.
347
348
assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
@@ -366,7 +367,7 @@ public void testGaugeCounterStartsAJobToConsumeTheGeneratedMetrics() {
366
367
int recordedGaugeMetricsCount =
367
368
recordedGaugeMetric .getAndroidMemoryReadingsCount ()
368
369
+ recordedGaugeMetric .getCpuMetricReadingsCount ();
369
- assertThat (recordedGaugeMetricsCount ).isEqualTo (30 );
370
+ assertThat (recordedGaugeMetricsCount ).isEqualTo (MAX_GAUGE_COUNTER_LIMIT );
370
371
371
372
assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
372
373
}
@@ -383,8 +384,7 @@ public void testGaugeCounterIsDecrementedWhenLogged() {
383
384
// There's no job to log the gauges.
384
385
assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
385
386
386
- // Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
387
- generateMetricsAndIncrementCounter (20 );
387
+ generateMetricsAndIncrementCounter (MAX_GAUGE_COUNTER_LIMIT - 10 );
388
388
389
389
// There's still no job to log the gauges.
390
390
assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
@@ -395,9 +395,47 @@ public void testGaugeCounterIsDecrementedWhenLogged() {
395
395
assertThat (fakeScheduledExecutorService .getDelayToNextTask (TimeUnit .MILLISECONDS ))
396
396
.isEqualTo (TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS );
397
397
398
- assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCounter + 30 );
398
+ assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCounter + MAX_GAUGE_COUNTER_LIMIT );
399
+ fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
400
+
401
+ assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCounter );
402
+ }
403
+
404
+ @ Test
405
+ public void testDuplicateGaugeLoggingIsAvoided () {
406
+ int priorGaugeCounter = GaugeCounter .count ();
407
+ PerfSession fakeSession = createTestSession (1 );
408
+ testGaugeManager .setApplicationProcessState (ApplicationProcessState .FOREGROUND );
409
+ testGaugeManager .startCollectingGauges (fakeSession );
410
+ GaugeCounter .setGaugeManager (testGaugeManager );
411
+
412
+ // There's no job to log the gauges.
413
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
414
+
415
+ generateMetricsAndIncrementCounter (MAX_GAUGE_COUNTER_LIMIT - 20 );
416
+
417
+ // There's still no job to log the gauges.
418
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
419
+
420
+ generateMetricsAndIncrementCounter (MAX_GAUGE_COUNTER_LIMIT );
421
+
422
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isFalse ();
423
+ assertThat (fakeScheduledExecutorService .getDelayToNextTask (TimeUnit .MILLISECONDS ))
424
+ .isEqualTo (TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS );
425
+
399
426
fakeScheduledExecutorService .simulateSleepExecutingAtMostOneTask ();
427
+ assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
400
428
429
+ GaugeMetric recordedGaugeMetric =
430
+ getLastRecordedGaugeMetric (ApplicationProcessState .FOREGROUND );
431
+
432
+ // It flushes all the metrics in the ConcurrentLinkedQueues that were added.
433
+ int recordedGaugeMetricsCount =
434
+ recordedGaugeMetric .getAndroidMemoryReadingsCount ()
435
+ + recordedGaugeMetric .getCpuMetricReadingsCount ();
436
+ assertThat (recordedGaugeMetricsCount ).isEqualTo (2 * MAX_GAUGE_COUNTER_LIMIT - 20 );
437
+
438
+ assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
401
439
assertThat (GaugeCounter .count ()).isEqualTo (priorGaugeCounter );
402
440
}
403
441
@@ -410,7 +448,7 @@ public void testUpdateAppStateHandlesMultipleAppStates() {
410
448
GaugeCounter .setGaugeManager (testGaugeManager );
411
449
412
450
// Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
413
- generateMetricsAndIncrementCounter (10 );
451
+ generateMetricsAndIncrementCounter (MAX_GAUGE_COUNTER_LIMIT - 10 );
414
452
415
453
// There's no job to log the gauges.
416
454
assertThat (fakeScheduledExecutorService .isEmpty ()).isTrue ();
@@ -425,7 +463,7 @@ public void testUpdateAppStateHandlesMultipleAppStates() {
425
463
shadowOf (Looper .getMainLooper ()).idle ();
426
464
427
465
// Generate additional metrics in the new app state.
428
- generateMetricsAndIncrementCounter (26 );
466
+ generateMetricsAndIncrementCounter (MAX_GAUGE_COUNTER_LIMIT + 1 );
429
467
430
468
GaugeMetric recordedGaugeMetric =
431
469
getLastRecordedGaugeMetric (ApplicationProcessState .FOREGROUND );
@@ -434,7 +472,7 @@ public void testUpdateAppStateHandlesMultipleAppStates() {
434
472
int recordedGaugeMetricsCount =
435
473
recordedGaugeMetric .getAndroidMemoryReadingsCount ()
436
474
+ recordedGaugeMetric .getCpuMetricReadingsCount ();
437
- assertThat (recordedGaugeMetricsCount ).isEqualTo (10 );
475
+ assertThat (recordedGaugeMetricsCount ).isEqualTo (MAX_GAUGE_COUNTER_LIMIT - 10 );
438
476
439
477
assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
440
478
@@ -448,7 +486,7 @@ public void testUpdateAppStateHandlesMultipleAppStates() {
448
486
recordedGaugeMetricsCount =
449
487
recordedGaugeMetric .getAndroidMemoryReadingsCount ()
450
488
+ recordedGaugeMetric .getCpuMetricReadingsCount ();
451
- assertThat (recordedGaugeMetricsCount ).isEqualTo (26 );
489
+ assertThat (recordedGaugeMetricsCount ).isEqualTo (MAX_GAUGE_COUNTER_LIMIT + 1 );
452
490
453
491
assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
454
492
}
@@ -462,7 +500,7 @@ public void testGaugeManagerHandlesMultipleSessionIds() {
462
500
GaugeCounter .setGaugeManager (testGaugeManager );
463
501
464
502
// Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
465
- generateMetricsAndIncrementCounter (10 );
503
+ generateMetricsAndIncrementCounter (MAX_GAUGE_COUNTER_LIMIT - 10 );
466
504
467
505
PerfSession updatedPerfSession = createTestSession (2 );
468
506
updatedPerfSession .setGaugeAndEventCollectionEnabled (true );
@@ -479,7 +517,7 @@ public void testGaugeManagerHandlesMultipleSessionIds() {
479
517
shadowOf (Looper .getMainLooper ()).idle ();
480
518
481
519
// Generate metrics for the new session.
482
- generateMetricsAndIncrementCounter (26 );
520
+ generateMetricsAndIncrementCounter (MAX_GAUGE_COUNTER_LIMIT + 1 );
483
521
484
522
GaugeMetric recordedGaugeMetric =
485
523
getLastRecordedGaugeMetric (ApplicationProcessState .BACKGROUND );
@@ -488,7 +526,7 @@ public void testGaugeManagerHandlesMultipleSessionIds() {
488
526
int recordedGaugeMetricsCount =
489
527
recordedGaugeMetric .getAndroidMemoryReadingsCount ()
490
528
+ recordedGaugeMetric .getCpuMetricReadingsCount ();
491
- assertThat (recordedGaugeMetricsCount ).isEqualTo (10 );
529
+ assertThat (recordedGaugeMetricsCount ).isEqualTo (MAX_GAUGE_COUNTER_LIMIT - 10 );
492
530
493
531
assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
494
532
@@ -502,7 +540,7 @@ public void testGaugeManagerHandlesMultipleSessionIds() {
502
540
recordedGaugeMetricsCount =
503
541
recordedGaugeMetric .getAndroidMemoryReadingsCount ()
504
542
+ recordedGaugeMetric .getCpuMetricReadingsCount ();
505
- assertThat (recordedGaugeMetricsCount ).isEqualTo (26 );
543
+ assertThat (recordedGaugeMetricsCount ).isEqualTo (MAX_GAUGE_COUNTER_LIMIT + 1 );
506
544
507
545
assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (2 ));
508
546
}
@@ -559,10 +597,10 @@ public void testLogGaugeMetadataSendDataToTransport() {
559
597
when (fakeGaugeMetadataManager .getMaxAppJavaHeapMemoryKb ()).thenReturn (1000 );
560
598
when (fakeGaugeMetadataManager .getMaxEncouragedAppJavaHeapMemoryKb ()).thenReturn (800 );
561
599
562
- testGaugeManager .logGaugeMetadata (testSessionId (1 ), ApplicationProcessState . FOREGROUND );
600
+ testGaugeManager .logGaugeMetadata (testSessionId (1 ));
563
601
564
602
GaugeMetric recordedGaugeMetric =
565
- getLastRecordedGaugeMetric (ApplicationProcessState .FOREGROUND );
603
+ getLastRecordedGaugeMetric (ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN );
566
604
GaugeMetadata recordedGaugeMetadata = recordedGaugeMetric .getGaugeMetadata ();
567
605
568
606
assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
@@ -586,9 +624,7 @@ public void testLogGaugeMetadataDoesNotLogWhenGaugeMetadataManagerNotAvailable()
586
624
new Lazy <>(() -> fakeCpuGaugeCollector ),
587
625
new Lazy <>(() -> fakeMemoryGaugeCollector ));
588
626
589
- assertThat (
590
- testGaugeManager .logGaugeMetadata (testSessionId (1 ), ApplicationProcessState .FOREGROUND ))
591
- .isFalse ();
627
+ assertThat (testGaugeManager .logGaugeMetadata (testSessionId (1 ))).isFalse ();
592
628
}
593
629
594
630
@ Test
@@ -603,17 +639,13 @@ public void testLogGaugeMetadataLogsAfterApplicationContextIsSet() {
603
639
new Lazy <>(() -> fakeCpuGaugeCollector ),
604
640
new Lazy <>(() -> fakeMemoryGaugeCollector ));
605
641
606
- assertThat (
607
- testGaugeManager .logGaugeMetadata (testSessionId (1 ), ApplicationProcessState .FOREGROUND ))
608
- .isFalse ();
642
+ assertThat (testGaugeManager .logGaugeMetadata (testSessionId (1 ))).isFalse ();
609
643
610
644
testGaugeManager .initializeGaugeMetadataManager (ApplicationProvider .getApplicationContext ());
611
- assertThat (
612
- testGaugeManager .logGaugeMetadata (testSessionId (1 ), ApplicationProcessState .FOREGROUND ))
613
- .isTrue ();
645
+ assertThat (testGaugeManager .logGaugeMetadata (testSessionId (1 ))).isTrue ();
614
646
615
647
GaugeMetric recordedGaugeMetric =
616
- getLastRecordedGaugeMetric (ApplicationProcessState .FOREGROUND );
648
+ getLastRecordedGaugeMetric (ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN );
617
649
GaugeMetadata recordedGaugeMetadata = recordedGaugeMetric .getGaugeMetadata ();
618
650
619
651
assertThat (recordedGaugeMetric .getSessionId ()).isEqualTo (testSessionId (1 ));
@@ -638,7 +670,7 @@ private long getMinimumBackgroundCollectionFrequency() {
638
670
}
639
671
640
672
// Simulates the behavior of Cpu and Memory Gauge collector.
641
- private void generateMetricsAndIncrementCounter (int count ) {
673
+ private void generateMetricsAndIncrementCounter (long count ) {
642
674
// TODO(b/394127311): Explore actually collecting metrics using the fake Cpu and Memory
643
675
// metric collectors.
644
676
Random random = new Random ();
@@ -679,8 +711,16 @@ private AndroidMemoryReading createFakeAndroidMetricReading(int currentUsedAppJa
679
711
private GaugeMetric getLastRecordedGaugeMetric (
680
712
ApplicationProcessState expectedApplicationProcessState ) {
681
713
ArgumentCaptor <GaugeMetric > argMetric = ArgumentCaptor .forClass (GaugeMetric .class );
682
- verify (mockTransportManager , times (1 ))
683
- .log (argMetric .capture (), eq (expectedApplicationProcessState ));
714
+
715
+ // TODO(b/394127311): Revisit transportManager.log method which is only being called in unit
716
+ // tests.
717
+ if (expectedApplicationProcessState
718
+ == ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN ) {
719
+ verify (mockTransportManager , times (1 )).log (argMetric .capture ());
720
+ } else {
721
+ verify (mockTransportManager , times (1 ))
722
+ .log (argMetric .capture (), eq (expectedApplicationProcessState ));
723
+ }
684
724
reset (mockTransportManager );
685
725
// Required after resetting the mock. By default we assume that Transport is initialized.
686
726
when (mockTransportManager .isInitialized ()).thenReturn (true );
0 commit comments