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 1414
1515package  com.google.firebase.perf.session.gauges 
1616
17+ import  androidx.annotation.VisibleForTesting 
18+ import  com.google.firebase.perf.logging.AndroidLogger 
1719import  java.util.concurrent.atomic.AtomicInteger 
1820
1921/* *
@@ -23,17 +25,31 @@ import java.util.concurrent.atomic.AtomicInteger
2325object  GaugeCounter {
2426  private  const  val  MAX_METRIC_COUNT  =  25 
2527  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()
2733
2834  fun  incrementCounter () {
2935    val  metricsCount =  counter.incrementAndGet()
3036
3137    if  (metricsCount >=  MAX_METRIC_COUNT ) {
3238      gaugeManager.logGaugeMetrics()
3339    }
40+ 
41+     logger.debug(" Incremented logger to $metricsCount " 
3442  }
3543
3644  fun  decrementCounter () {
37-     counter.decrementAndGet()
45+     val  curr =  counter.decrementAndGet()
46+     logger.debug(" Decremented logger to $curr " 
3847  }
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()
3955}
Original file line number Diff line number Diff line change @@ -100,13 +100,19 @@ public void initializeGaugeMetadataManager(Context appContext) {
100100
101101  @ Override 
102102  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. 
105104    if  (session  == null  || !session .isVerbose ()) {
105+       this .applicationProcessState  = applicationProcessState ;
106106      return ;
107107    }
108108
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. 
110116    startCollectingGauges (this .applicationProcessState , session .getTimer ());
111117  }
112118
@@ -132,6 +138,7 @@ public void startCollectingGauges(PerfSession session) {
132138      stopCollectingGauges ();
133139    }
134140
141+     // TODO(b/394127311): Explore always setting the app state as FOREGROUND. 
135142    ApplicationProcessState  gaugeCollectionApplicationProcessState  = applicationProcessState ;
136143    if  (gaugeCollectionApplicationProcessState 
137144        == ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN ) {
@@ -419,4 +426,9 @@ private long getMemoryGaugeCollectionFrequencyMs(
419426      return  memoryGaugeCollectionFrequency ;
420427    }
421428  }
429+ 
430+   @ VisibleForTesting 
431+   void  setApplicationProcessState (ApplicationProcessState  applicationProcessState ) {
432+     this .applicationProcessState  = applicationProcessState ;
433+   }
422434}
Original file line number Diff line number Diff line change 2525import  com .google .firebase .perf .config .ConfigResolver ;
2626import  com .google .firebase .perf .session .PerfSession ;
2727import  com .google .firebase .perf .session .SessionManager ;
28+ import  com .google .firebase .perf .session .gauges .GaugeCounter ;
2829import  com .google .firebase .perf .util .ImmutableBundle ;
2930import  org .junit .After ;
3031import  org .junit .Before ;
32+ import  org .junit .BeforeClass ;
3133import  org .robolectric .shadows .ShadowPackageManager ;
3234
3335public  class  FirebasePerformanceTestBase  {
@@ -54,6 +56,12 @@ public class FirebasePerformanceTestBase {
5456
5557  protected  Context  appContext ;
5658
59+   @ BeforeClass 
60+   public  static  void  setUpBeforeClass () {
61+     // TODO(b/394127311): Explore removing this. 
62+     GaugeCounter .INSTANCE .resetCounter ();
63+   }
64+ 
5765  @ Before 
5866  public  void  setUpFirebaseApp () {
5967    appContext  = ApplicationProvider .getApplicationContext ();
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments