keepalive_statistics_recorder.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2018 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. #include "services/network/keepalive_statistics_recorder.h"
  5. #include <algorithm>
  6. namespace network {
  7. KeepaliveStatisticsRecorder::KeepaliveStatisticsRecorder() = default;
  8. KeepaliveStatisticsRecorder::~KeepaliveStatisticsRecorder() = default;
  9. void KeepaliveStatisticsRecorder::Register(
  10. const base::UnguessableToken& top_level_frame_id) {
  11. auto it = per_top_level_frame_records_.find(top_level_frame_id);
  12. if (it == per_top_level_frame_records_.end()) {
  13. per_top_level_frame_records_.insert(
  14. std::make_pair(top_level_frame_id, PerTopLevelFrameStats()));
  15. return;
  16. }
  17. ++it->second.num_registrations;
  18. }
  19. void KeepaliveStatisticsRecorder::Unregister(
  20. const base::UnguessableToken& top_level_frame_id) {
  21. auto it = per_top_level_frame_records_.find(top_level_frame_id);
  22. DCHECK(it != per_top_level_frame_records_.end());
  23. if (it->second.num_registrations == 1) {
  24. per_top_level_frame_records_.erase(it);
  25. return;
  26. }
  27. --it->second.num_registrations;
  28. }
  29. void KeepaliveStatisticsRecorder::OnLoadStarted(
  30. const base::UnguessableToken& top_level_frame_id,
  31. int request_size) {
  32. auto it = per_top_level_frame_records_.find(top_level_frame_id);
  33. if (it != per_top_level_frame_records_.end()) {
  34. ++it->second.num_inflight_requests;
  35. it->second.total_request_size += request_size;
  36. if (it->second.peak_inflight_requests < it->second.num_inflight_requests) {
  37. it->second.peak_inflight_requests = it->second.num_inflight_requests;
  38. }
  39. }
  40. ++num_inflight_requests_;
  41. if (peak_inflight_requests_ < num_inflight_requests_) {
  42. peak_inflight_requests_ = num_inflight_requests_;
  43. }
  44. }
  45. void KeepaliveStatisticsRecorder::OnLoadFinished(
  46. const base::UnguessableToken& top_level_frame_id,
  47. int request_size) {
  48. auto it = per_top_level_frame_records_.find(top_level_frame_id);
  49. if (it != per_top_level_frame_records_.end()) {
  50. --it->second.num_inflight_requests;
  51. DCHECK_GE(it->second.total_request_size, request_size);
  52. it->second.total_request_size -= request_size;
  53. }
  54. --num_inflight_requests_;
  55. }
  56. int KeepaliveStatisticsRecorder::NumInflightRequestsPerTopLevelFrame(
  57. const base::UnguessableToken& top_level_frame_id) const {
  58. auto it = per_top_level_frame_records_.find(top_level_frame_id);
  59. if (it == per_top_level_frame_records_.end())
  60. return 0;
  61. return it->second.num_inflight_requests;
  62. }
  63. int KeepaliveStatisticsRecorder::GetTotalRequestSizePerTopLevelFrame(
  64. const base::UnguessableToken& top_level_frame_id) const {
  65. auto it = per_top_level_frame_records_.find(top_level_frame_id);
  66. if (it == per_top_level_frame_records_.end())
  67. return 0;
  68. return it->second.total_request_size;
  69. }
  70. } // namespace network