keepalive_statistics_recorder.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #ifndef SERVICES_NETWORK_KEEPALIVE_STATISTICS_RECORDER_H_
  5. #define SERVICES_NETWORK_KEEPALIVE_STATISTICS_RECORDER_H_
  6. #include <map>
  7. #include "base/component_export.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/unguessable_token.h"
  11. namespace network {
  12. // KeepaliveStatisticsRecorder keeps tracks of the number of inflight requests
  13. // with "keepalive" set.
  14. class COMPONENT_EXPORT(NETWORK_SERVICE) KeepaliveStatisticsRecorder
  15. : public base::SupportsWeakPtr<KeepaliveStatisticsRecorder> {
  16. public:
  17. struct PerTopLevelFrameStats {
  18. int num_registrations = 1;
  19. int num_inflight_requests = 0;
  20. int peak_inflight_requests = 0;
  21. int total_request_size = 0;
  22. };
  23. KeepaliveStatisticsRecorder();
  24. KeepaliveStatisticsRecorder(const KeepaliveStatisticsRecorder&) = delete;
  25. KeepaliveStatisticsRecorder& operator=(const KeepaliveStatisticsRecorder&) =
  26. delete;
  27. ~KeepaliveStatisticsRecorder();
  28. // Registers / Unregisters |top_level_frame| to this object.
  29. // There can be multiple Register / Unregister calls with the same
  30. // |top_level_frame|, and this object thinks a an entry for |top_level_frame|
  31. // is gone when the number of Register calls with |top_level_frame| equals to
  32. // the number of Unregister calls with |token|.
  33. void Register(const base::UnguessableToken& top_level_frame_id);
  34. void Unregister(const base::UnguessableToken& top_level_frame_id);
  35. // Called when a request with keepalive set starts.
  36. void OnLoadStarted(const base::UnguessableToken& top_level_frame_id,
  37. int request_size);
  38. // Called when a request with keepalive set finishes.
  39. void OnLoadFinished(const base::UnguessableToken& top_level_frame_id,
  40. int request_size);
  41. const std::map<base::UnguessableToken, PerTopLevelFrameStats>&
  42. per_top_level_frame_records() const {
  43. return per_top_level_frame_records_;
  44. }
  45. int NumInflightRequestsPerTopLevelFrame(
  46. const base::UnguessableToken& top_level_frame_id) const;
  47. int GetTotalRequestSizePerTopLevelFrame(
  48. const base::UnguessableToken& top_level_frame_id) const;
  49. int num_inflight_requests() const { return num_inflight_requests_; }
  50. int peak_inflight_requests() const { return peak_inflight_requests_; }
  51. private:
  52. std::map<base::UnguessableToken, PerTopLevelFrameStats>
  53. per_top_level_frame_records_;
  54. int num_inflight_requests_ = 0;
  55. int peak_inflight_requests_ = 0;
  56. };
  57. } // namespace network
  58. #endif // SERVICES_NETWORK_KEEPALIVE_STATISTICS_RECORDER_H_