Skip to content

Commit

Permalink
[battery] Report if the browser is shutting down in the UKMs
Browse files Browse the repository at this point in the history
(cherry picked from commit 55427f2)

Bug: 1153193
Change-Id: I549a17a607e23a051fb51996185c2b5e341b1bdf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2722854
Reviewed-by: François Doray <fdoray@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Commit-Queue: Sébastien Marchand <sebmarchand@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#858694}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2740851
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Auto-Submit: Sébastien Marchand <sebmarchand@chromium.org>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/branch-heads/4430@{#205}
Cr-Branched-From: e5ce7dc-refs/heads/master@{#857950}
  • Loading branch information
sebmarchand authored and Chromium LUCI CQ committed Mar 7, 2021
1 parent c799fc7 commit cf68e8e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions chrome/browser/lifetime/browser_shutdown.cc
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,9 @@ bool IsTryingToQuit() {
return g_trying_to_quit;
}

base::AutoReset<ShutdownType> SetShutdownTypeForTesting(
ShutdownType shutdown_type) {
return base::AutoReset<ShutdownType>(&g_shutdown_type, shutdown_type);
}

} // namespace browser_shutdown
5 changes: 5 additions & 0 deletions chrome/browser/lifetime/browser_shutdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <stdint.h>

#include "base/auto_reset.h"
#include "build/build_config.h"

class PrefRegistrySimple;
Expand Down Expand Up @@ -117,6 +118,10 @@ void SetTryingToQuit(bool quitting);
// General accessor.
bool IsTryingToQuit();

// Allows setting a fake shutdown type for testing purposes.
base::AutoReset<ShutdownType> SetShutdownTypeForTesting(
ShutdownType shutdown_type);

} // namespace browser_shutdown

#endif // CHROME_BROWSER_LIFETIME_BROWSER_SHUTDOWN_H_
2 changes: 2 additions & 0 deletions chrome/browser/metrics/power/power_metrics_reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/performance_monitor/process_monitor.h"
#include "services/metrics/public/cpp/metrics_utils.h"
#include "services/metrics/public/cpp/ukm_builders.h"
Expand Down Expand Up @@ -163,6 +164,7 @@ void PowerMetricsReporter::ReportUKMs(
.InSeconds()));
builder.SetVideoCaptureSeconds(ukm::GetExponentialBucketMinForUserTiming(
usage_metrics.time_capturing_video.InSeconds()));
builder.SetBrowserShuttingDown(browser_shutdown::HasShutdownStarted());

builder.Record(ukm_recorder);
}
Expand Down
34 changes: 34 additions & 0 deletions chrome/browser/metrics/power/power_metrics_reporter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/metrics/usage_scenario/usage_scenario_data_store.h"
#include "chrome/browser/performance_monitor/process_monitor.h"
#include "components/ukm/test_ukm_recorder.h"
Expand Down Expand Up @@ -255,6 +256,8 @@ TEST_F(PowerMetricsReporterUnitTest, UKMs) {
entries[0], UkmEntry::kVideoCaptureSecondsName,
ukm::GetExponentialBucketMinForUserTiming(
fake_interval_data.time_capturing_video.InSeconds()));
test_ukm_recorder_.ExpectEntryMetric(
entries[0], UkmEntry::kBrowserShuttingDownName, false);

histogram_tester_.ExpectUniqueSample(kBatteryDischargeRateHistogramName, 2500,
1);
Expand All @@ -263,6 +266,37 @@ TEST_F(PowerMetricsReporterUnitTest, UKMs) {
PowerMetricsReporterAccess::BatteryDischargeMode::kDischarging, 1);
}

TEST_F(PowerMetricsReporterUnitTest, UKMsBrowserShuttingDown) {
UsageScenarioDataStore::IntervalData fake_interval_data = {};
fake_interval_data.source_id_for_longest_visible_origin = 42;
task_environment_.FastForwardBy(kExpectedMetricsCollectionInterval);
battery_states_.push(BatteryLevelProvider::BatteryState{
1, 1, 0.50, true, base::TimeTicks::Now()});
data_store_.SetIntervalDataToReturn(fake_interval_data);

performance_monitor::ProcessMonitor::Metrics fake_metrics = {};
fake_metrics.cpu_usage = 0.5;
#if defined(OS_MAC)
fake_metrics.idle_wakeups = 42;
fake_metrics.package_idle_wakeups = 43;
fake_metrics.energy_impact = 44;
#endif

{
auto fake_shutdown = browser_shutdown::SetShutdownTypeForTesting(
browser_shutdown::ShutdownType::kBrowserExit);
EXPECT_TRUE(browser_shutdown::HasShutdownStarted());
process_monitor_.NotifyObserversForOnAggregatedMetricsSampled(fake_metrics);
}

auto entries = test_ukm_recorder_.GetEntriesByName(
ukm::builders::PowerUsageScenariosIntervalData::kEntryName);
EXPECT_EQ(1u, entries.size());

test_ukm_recorder_.ExpectEntryMetric(
entries[0], UkmEntry::kBrowserShuttingDownName, true);
}

TEST_F(PowerMetricsReporterUnitTest, UKMsPluggedIn) {
// Update the latest reported battery state to pretend that the system isn't
// running on battery.
Expand Down

0 comments on commit cf68e8e

Please sign in to comment.