Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Unified Diff: content/browser/service_worker/service_worker_lifetime_tracker_unittest.cc

Issue 2706923003: Add UMA for how long service workers run for. (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_lifetime_tracker_unittest.cc
diff --git a/content/browser/service_worker/service_worker_lifetime_tracker_unittest.cc b/content/browser/service_worker/service_worker_lifetime_tracker_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9cd77679383d29ec2d56f98c4a1ec425997e311a
--- /dev/null
+++ b/content/browser/service_worker/service_worker_lifetime_tracker_unittest.cc
@@ -0,0 +1,116 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/service_worker/service_worker_lifetime_tracker.h"
+
+#include "base/test/histogram_tester.h"
+#include "base/test/simple_test_tick_clock.h"
+#include "base/time/tick_clock.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "content/public/test/test_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+class ServiceWorkerLifetimeTrackerTest : public testing::Test {
+ public:
+ ServiceWorkerLifetimeTrackerTest()
+ : tick_clock_(new base::SimpleTestTickClock()),
+ tracker_(base::WrapUnique(tick_clock_)) {}
+
+ base::SimpleTestTickClock* tick_clock() { return tick_clock_; }
+ ServiceWorkerLifetimeTracker* tracker() { return &tracker_; }
+
+ void AdvanceAndRunTimer(bool expect_running) {
+ // Advance to the scheduled time and run the timer task.
+ tick_clock()->SetNowTicks(tracker()->timer_.desired_run_time());
+ tracker()->timer_.user_task().Run();
+
+ // The timer's desired run time is still unchanged here. So reset it if the
+ // timer is still running.
+ if (expect_running) {
+ EXPECT_TRUE(tracker()->timer_.IsRunning());
+ tracker()->timer_.Reset();
+ } else {
+ EXPECT_FALSE(tracker()->timer_.IsRunning());
+ }
+ }
+
+ private:
+ TestBrowserThreadBundle thread_bundle_;
+
+ // Not owned.
+ base::SimpleTestTickClock* tick_clock_;
+
+ ServiceWorkerLifetimeTracker tracker_;
+};
+
+TEST_F(ServiceWorkerLifetimeTrackerTest, Metrics) {
+ int64_t kVersion1 = 13; // dummy value
+ int64_t kVersion2 = 14; // dummy value
+
+ tick_clock()->SetNowTicks(base::TimeTicks::Now());
+
+ // Start a worker to kick off the timer.
+ tracker()->StartTiming(kVersion2);
+
+ // Run a worker for 10 seconds.
+ {
+ base::HistogramTester metrics;
+ tracker()->StartTiming(kVersion1);
+ tick_clock()->Advance(base::TimeDelta::FromSeconds(10));
+ tracker()->StopTiming(kVersion1);
+ metrics.ExpectTimeBucketCount("ServiceWorker.Runtime",
+ base::TimeDelta::FromSeconds(10), 1);
+ }
+
+ // Advance to the 5 minute mark and run the timer. It should not record
+ // StillRunning.
+ {
+ base::HistogramTester metrics;
+ AdvanceAndRunTimer(true /* expect_running */);
+ metrics.ExpectTotalCount("ServiceWorker.StillRunningTime", 0);
+ }
+
+ // Advance again. It should record StillRunning.
+ {
+ base::HistogramTester metrics;
+ AdvanceAndRunTimer(true /* expect_running */);
+ metrics.ExpectTimeBucketCount("ServiceWorker.StillRunningTime",
+ base::TimeDelta::FromMinutes(10), 1);
+ }
+
+ // Advance 30 seconds and stop the worker. It should record Runtime.
+ {
+ base::HistogramTester metrics;
+ tick_clock()->Advance(base::TimeDelta::FromSeconds(30));
+ tracker()->StopTiming(kVersion2);
+ metrics.ExpectTimeBucketCount(
+ "ServiceWorker.Runtime",
+ base::TimeDelta::FromMinutes(10) + base::TimeDelta::FromSeconds(30), 1);
+ }
+
+ {
+ base::HistogramTester metrics;
+ // Start a worker and abort the timing.
+ tracker()->StartTiming(kVersion1);
+ tick_clock()->Advance(base::TimeDelta::FromSeconds(10));
+ tracker()->AbortTiming(kVersion1);
+
+ // Advance to the next 5 minute mark. It should not record another
+ // StillRunning.
+ AdvanceAndRunTimer(false /* expect_running */);
+
+ // Aborting multiple times should be fine.
+ tracker()->AbortTiming(kVersion1);
+ tracker()->AbortTiming(kVersion1);
+ // StopTiming should not record another StillRunning.
+ tracker()->StopTiming(kVersion1);
+
+ metrics.ExpectTotalCount("ServiceWorker.StillRunningTime", 0);
+ metrics.ExpectTotalCount("ServiceWorker.Runtime", 0);
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698