#include "content/common/android/cpu_time_metrics_internal.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "content/common/process_visibility_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace internal {
namespace {
void WorkForOneCpuSec(base::WaitableEvent* event) {
auto initial_ticks = base::ThreadTicks::Now();
while (!event->IsSignaled()) {
if (base::ThreadTicks::Now() > initial_ticks + base::Seconds(1)) {
event->Signal();
}
}
}
TEST(CpuTimeMetricsTest, RecordsMetricsForeground) {
ProcessVisibilityTracker::GetInstance();
base::test::TaskEnvironment task_environment;
base::HistogramTester histograms;
base::Thread thread1("StackSamplingProfiler");
thread1.StartAndWaitForTesting();
ASSERT_TRUE(thread1.IsRunning());
base::WaitableEvent event;
ProcessCpuTimeMetrics::SetIgnoreHistogramAllocatorForTesting(true);
std::unique_ptr<ProcessCpuTimeMetrics> metrics =
ProcessCpuTimeMetrics::CreateForTesting();
metrics->WaitForCollectionForTesting();
ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged(true);
metrics->WaitForCollectionForTesting();
thread1.task_runner()->PostTask(
FROM_HERE, BindOnce(&WorkForOneCpuSec, base::Unretained(&event)));
event.Wait();
ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged(false);
metrics->WaitForCollectionForTesting();
static constexpr int kBrowserProcessBucket = 2;
static constexpr int kSamplingProfilerThreadBucket = 24;
int browser_cpu_seconds = histograms.GetBucketCount(
"Power.CpuTimeSecondsPerProcessType", kBrowserProcessBucket);
EXPECT_GE(browser_cpu_seconds, 1);
int browser_cpu_seconds_foreground = histograms.GetBucketCount(
"Power.CpuTimeSecondsPerProcessType.Foreground", kBrowserProcessBucket);
EXPECT_GE(browser_cpu_seconds_foreground, 1);
int thread_cpu_seconds =
histograms.GetBucketCount("Power.CpuTimeSecondsPerThreadType.Browser",
kSamplingProfilerThreadBucket);
EXPECT_GE(thread_cpu_seconds, 1);
thread1.Stop();
ProcessCpuTimeMetrics::SetIgnoreHistogramAllocatorForTesting(false);
}
TEST(CpuTimeMetricsTest, RecordsMetricsBackground) {
ProcessVisibilityTracker::GetInstance();
base::test::TaskEnvironment task_environment;
base::HistogramTester histograms;
base::Thread thread1("StackSamplingProfiler");
thread1.StartAndWaitForTesting();
ASSERT_TRUE(thread1.IsRunning());
base::WaitableEvent event;
ProcessCpuTimeMetrics::SetIgnoreHistogramAllocatorForTesting(true);
std::unique_ptr<ProcessCpuTimeMetrics> metrics =
ProcessCpuTimeMetrics::CreateForTesting();
metrics->WaitForCollectionForTesting();
ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged(false);
metrics->WaitForCollectionForTesting();
thread1.task_runner()->PostTask(
FROM_HERE, BindOnce(&WorkForOneCpuSec, base::Unretained(&event)));
event.Wait();
ProcessVisibilityTracker::GetInstance()->OnProcessVisibilityChanged(true);
metrics->WaitForCollectionForTesting();
static constexpr int kBrowserProcessBucket = 2;
static constexpr int kSamplingProfilerThreadBucket = 24;
int browser_cpu_seconds = histograms.GetBucketCount(
"Power.CpuTimeSecondsPerProcessType", kBrowserProcessBucket);
EXPECT_GE(browser_cpu_seconds, 1);
int browser_cpu_seconds_background = histograms.GetBucketCount(
"Power.CpuTimeSecondsPerProcessType.Background", kBrowserProcessBucket);
EXPECT_GE(browser_cpu_seconds_background, 1);
int thread_cpu_seconds =
histograms.GetBucketCount("Power.CpuTimeSecondsPerThreadType.Browser",
kSamplingProfilerThreadBucket);
EXPECT_GE(thread_cpu_seconds, 1);
thread1.Stop();
ProcessCpuTimeMetrics::SetIgnoreHistogramAllocatorForTesting(false);
}
}
}
}