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

Side by Side Diff: content/browser/service_worker/service_worker_lifetime_tracker.cc

Issue 2706923003: Add UMA for how long service workers run for. (Closed)
Patch Set: comments 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/service_worker/service_worker_lifetime_tracker.h"
6
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "base/time/default_tick_clock.h"
10 #include "content/browser/service_worker/service_worker_metrics.h"
11
12 namespace content {
13
14 // Five minutes is somewhat ad-hoc. We want to capture workers that are
15 // running for a long time in UMA even if they never stop. A timer running too
16 // frequently would be wasteful and too infrequently could miss long-running
17 // workers before browser shutdown. Five minutes seems about right; it's also
18 // the default keep-alive time limit for a service worker event so workers
19 // running longer than this are a bit special: they must have had multiple
20 // events in their lifetime.
21 constexpr base::TimeDelta kTimerDuration = base::TimeDelta::FromMinutes(5);
22
23 ServiceWorkerLifetimeTracker::ServiceWorkerLifetimeTracker()
24 : ServiceWorkerLifetimeTracker(base::MakeUnique<base::DefaultTickClock>()) {
25 }
26
27 ServiceWorkerLifetimeTracker::ServiceWorkerLifetimeTracker(
28 std::unique_ptr<base::TickClock> tick_clock)
29 : tick_clock_(std::move(tick_clock)), timer_(tick_clock_.get()) {}
30
31 ServiceWorkerLifetimeTracker::~ServiceWorkerLifetimeTracker() = default;
32
33 void ServiceWorkerLifetimeTracker::StartTiming(int64_t version_id) {
34 DCHECK(!base::ContainsKey(running_workers_, version_id));
falken 2017/02/24 08:32:26 I'm hitting this DCHECK in unit tests, but more ge
35
36 running_workers_[version_id] = tick_clock_->NowTicks();
37 if (!timer_.IsRunning()) {
38 timer_.Start(FROM_HERE, kTimerDuration, this,
39 &ServiceWorkerLifetimeTracker::RecordHistograms);
40 }
41 }
42
43 void ServiceWorkerLifetimeTracker::StopTiming(int64_t version_id) {
44 auto it = running_workers_.find(version_id);
45 // The entry may have been removed by AbortTiming.
46 if (it == running_workers_.end())
47 return;
48 ServiceWorkerMetrics::RecordRuntime(tick_clock_->NowTicks() - it->second);
49 running_workers_.erase(it);
50 }
51
52 void ServiceWorkerLifetimeTracker::AbortTiming(int64_t version_id) {
53 auto it = running_workers_.find(version_id);
54 // The entry may have been removed by AbortTiming.
55 if (it == running_workers_.end())
56 return;
57 running_workers_.erase(it);
58 }
59
60 void ServiceWorkerLifetimeTracker::RecordHistograms() {
61 base::TimeTicks now = tick_clock_->NowTicks();
62
63 for (auto& item : running_workers_) {
64 base::TimeDelta runtime = now - item.second;
65 if (runtime > kTimerDuration)
66 ServiceWorkerMetrics::RecordStillRunning(runtime);
67 }
68
69 if (running_workers_.empty())
70 timer_.Stop();
71 }
72
73 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698