Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.firebase.perf.session.gauges

import com.google.firebase.perf.logging.AndroidLogger
import java.util.concurrent.atomic.AtomicInteger

/**
Expand All @@ -23,17 +24,30 @@ import java.util.concurrent.atomic.AtomicInteger
object GaugeCounter {
private const val MAX_METRIC_COUNT = 25
private val counter = AtomicInteger(0)
private val gaugeManager: GaugeManager = GaugeManager.getInstance()
private val logger = AndroidLogger.getInstance()
// TODO(b/394127311): Setting this as a var for a unit test. Refactor it.
var gaugeManager: GaugeManager = GaugeManager.getInstance()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could do something like:

var gaugeManager: ...
   internal set

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe even @set:VisibleForTesting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


fun incrementCounter() {
val metricsCount = counter.incrementAndGet()

if (metricsCount >= MAX_METRIC_COUNT) {
gaugeManager.logGaugeMetrics()
}

logger.debug("Incremented logger to $metricsCount")
}

fun decrementCounter() {
counter.decrementAndGet()
val curr = counter.decrementAndGet()
logger.debug("Decremented logger to $curr")
}

// TODO: Add annotation to only call from tests
fun resetCounter() {
counter.set(0)
}

// TODO: Add annotation to only call from tests
fun count(): Int = counter.get()
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,19 @@ public void initializeGaugeMetadataManager(Context appContext) {

@Override
public void onUpdateAppState(ApplicationProcessState applicationProcessState) {
this.applicationProcessState = applicationProcessState;

// If it isn't a verbose session (or unset) update the app state and return.
if (session == null || !session.isVerbose()) {
this.applicationProcessState = applicationProcessState;
return;
}

// If it's a verbose session, start collecting gauges for the new app state.
// Log existing gauges to the current app state.
logGaugeMetrics();

// Update App State.
this.applicationProcessState = applicationProcessState;

// Start collecting gauges for the new app state.
startCollectingGauges(this.applicationProcessState, session.getTimer());
}

Expand All @@ -132,6 +138,7 @@ public void startCollectingGauges(PerfSession session) {
stopCollectingGauges();
}

// TODO(b/394127311): Explore always setting the app state as FOREGROUND.
ApplicationProcessState gaugeCollectionApplicationProcessState = applicationProcessState;
if (gaugeCollectionApplicationProcessState
== ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN) {
Expand Down Expand Up @@ -419,4 +426,9 @@ private long getMemoryGaugeCollectionFrequencyMs(
return memoryGaugeCollectionFrequency;
}
}

@VisibleForTesting
void setApplicationProcessState(ApplicationProcessState applicationProcessState) {
this.applicationProcessState = applicationProcessState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@
import com.google.firebase.perf.config.ConfigResolver;
import com.google.firebase.perf.session.PerfSession;
import com.google.firebase.perf.session.SessionManager;
import com.google.firebase.perf.session.gauges.GaugeCounter;
import com.google.firebase.perf.util.ImmutableBundle;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.robolectric.shadows.ShadowLog;
import org.robolectric.shadows.ShadowPackageManager;

public class FirebasePerformanceTestBase {
@BeforeClass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do it @After instead? Then be a good citizen to other unit tests in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason the unit tests fails on Github Actions (but not locally) with @After. Reverted it and added a TODO.

public static void setUpBeforeClass() {
GaugeCounter.INSTANCE.resetCounter();
}

/**
* The following values are needed by Firebase to identify the project and application that all
Expand All @@ -56,6 +63,7 @@ public class FirebasePerformanceTestBase {

@Before
public void setUpFirebaseApp() {
ShadowLog.stream = System.out;
appContext = ApplicationProvider.getApplicationContext();

ShadowPackageManager shadowPackageManager = shadowOf(appContext.getPackageManager());
Expand Down
Loading
Loading