forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepalive_statistics_recorder.h
75 lines (60 loc) · 2.71 KB
/
keepalive_statistics_recorder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_NETWORK_KEEPALIVE_STATISTICS_RECORDER_H_
#define SERVICES_NETWORK_KEEPALIVE_STATISTICS_RECORDER_H_
#include <map>
#include "base/component_export.h"
#include "base/memory/weak_ptr.h"
#include "base/unguessable_token.h"
namespace network {
// KeepaliveStatisticsRecorder keeps tracks of the number of inflight requests
// with "keepalive" set.
class COMPONENT_EXPORT(NETWORK_SERVICE) KeepaliveStatisticsRecorder final {
public:
struct PerTopLevelFrameStats {
int num_registrations = 1;
int num_inflight_requests = 0;
int peak_inflight_requests = 0;
int total_request_size = 0;
};
KeepaliveStatisticsRecorder();
KeepaliveStatisticsRecorder(const KeepaliveStatisticsRecorder&) = delete;
KeepaliveStatisticsRecorder& operator=(const KeepaliveStatisticsRecorder&) =
delete;
~KeepaliveStatisticsRecorder();
// Registers / Unregisters |top_level_frame| to this object.
// There can be multiple Register / Unregister calls with the same
// |top_level_frame|, and this object thinks a an entry for |top_level_frame|
// is gone when the number of Register calls with |top_level_frame| equals to
// the number of Unregister calls with |token|.
void Register(const base::UnguessableToken& top_level_frame_id);
void Unregister(const base::UnguessableToken& top_level_frame_id);
// Called when a request with keepalive set starts.
void OnLoadStarted(const base::UnguessableToken& top_level_frame_id,
int request_size);
// Called when a request with keepalive set finishes.
void OnLoadFinished(const base::UnguessableToken& top_level_frame_id,
int request_size);
const std::map<base::UnguessableToken, PerTopLevelFrameStats>&
per_top_level_frame_records() const {
return per_top_level_frame_records_;
}
int NumInflightRequestsPerTopLevelFrame(
const base::UnguessableToken& top_level_frame_id) const;
int GetTotalRequestSizePerTopLevelFrame(
const base::UnguessableToken& top_level_frame_id) const;
int num_inflight_requests() const { return num_inflight_requests_; }
int peak_inflight_requests() const { return peak_inflight_requests_; }
base::WeakPtr<KeepaliveStatisticsRecorder> AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
std::map<base::UnguessableToken, PerTopLevelFrameStats>
per_top_level_frame_records_;
int num_inflight_requests_ = 0;
int peak_inflight_requests_ = 0;
base::WeakPtrFactory<KeepaliveStatisticsRecorder> weak_ptr_factory_{this};
};
} // namespace network
#endif // SERVICES_NETWORK_KEEPALIVE_STATISTICS_RECORDER_H_