Skip to content

Add unit tests for new logging in GaugeManager using GaugeCounter. #6953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all 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,8 @@

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

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

/**
Expand All @@ -23,17 +25,31 @@ 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()

@set:VisibleForTesting(otherwise = VisibleForTesting.NONE)
@set:JvmStatic
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")
}

@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun resetCounter() {
counter.set(0)
}

@VisibleForTesting(otherwise = VisibleForTesting.NONE) 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,9 +25,11 @@
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.ShadowPackageManager;

public class FirebasePerformanceTestBase {
Expand All @@ -54,6 +56,12 @@ public class FirebasePerformanceTestBase {

protected Context appContext;

@BeforeClass
public static void setUpBeforeClass() {
// TODO(b/394127311): Explore removing this.
GaugeCounter.INSTANCE.resetCounter();
}

@Before
public void setUpFirebaseApp() {
appContext = ApplicationProvider.getApplicationContext();
Expand Down
Loading
Loading