event_creator.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include "net/nqe/event_creator.h"
  5. #include <stdlib.h>
  6. #include <memory>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/callback.h"
  10. #include "base/strings/string_number_conversions.h"
  11. #include "base/values.h"
  12. #include "net/log/net_log_capture_mode.h"
  13. #include "net/log/net_log_with_source.h"
  14. namespace net::nqe::internal {
  15. namespace {
  16. base::Value NetworkQualityChangedNetLogParams(
  17. base::TimeDelta http_rtt,
  18. base::TimeDelta transport_rtt,
  19. int32_t downstream_throughput_kbps,
  20. EffectiveConnectionType effective_connection_type) {
  21. base::Value::Dict value;
  22. value.Set("http_rtt_ms", static_cast<int>(http_rtt.InMilliseconds()));
  23. value.Set("transport_rtt_ms",
  24. static_cast<int>(transport_rtt.InMilliseconds()));
  25. value.Set("downstream_throughput_kbps", downstream_throughput_kbps);
  26. value.Set("effective_connection_type",
  27. GetNameForEffectiveConnectionType(effective_connection_type));
  28. return base::Value(std::move(value));
  29. }
  30. bool MetricChangedMeaningfully(int32_t past_value, int32_t current_value) {
  31. if ((past_value == INVALID_RTT_THROUGHPUT) !=
  32. (current_value == INVALID_RTT_THROUGHPUT)) {
  33. return true;
  34. }
  35. if (past_value == INVALID_RTT_THROUGHPUT &&
  36. current_value == INVALID_RTT_THROUGHPUT) {
  37. return false;
  38. }
  39. // Create a new entry only if (i) the difference between the two values exceed
  40. // the threshold; and, (ii) the ratio of the values also exceeds the
  41. // threshold.
  42. static const int kMinDifferenceInMetrics = 100;
  43. static const float kMinRatio = 1.2f;
  44. if (std::abs(past_value - current_value) < kMinDifferenceInMetrics) {
  45. // The absolute change in the value is not sufficient enough.
  46. return false;
  47. }
  48. if (past_value < (kMinRatio * current_value) &&
  49. current_value < (kMinRatio * past_value)) {
  50. // The relative change in the value is not sufficient enough.
  51. return false;
  52. }
  53. return true;
  54. }
  55. } // namespace
  56. EventCreator::EventCreator(NetLogWithSource net_log) : net_log_(net_log) {}
  57. EventCreator::~EventCreator() {
  58. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  59. }
  60. void EventCreator::MaybeAddNetworkQualityChangedEventToNetLog(
  61. EffectiveConnectionType effective_connection_type,
  62. const NetworkQuality& network_quality) {
  63. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  64. // Check if any of the network quality metrics changed meaningfully.
  65. bool effective_connection_type_changed =
  66. past_effective_connection_type_ != effective_connection_type;
  67. bool http_rtt_changed = MetricChangedMeaningfully(
  68. past_network_quality_.http_rtt().InMilliseconds(),
  69. network_quality.http_rtt().InMilliseconds());
  70. bool transport_rtt_changed = MetricChangedMeaningfully(
  71. past_network_quality_.transport_rtt().InMilliseconds(),
  72. network_quality.transport_rtt().InMilliseconds());
  73. bool kbps_changed = MetricChangedMeaningfully(
  74. past_network_quality_.downstream_throughput_kbps(),
  75. network_quality.downstream_throughput_kbps());
  76. if (!effective_connection_type_changed && !http_rtt_changed &&
  77. !transport_rtt_changed && !kbps_changed) {
  78. // Return since none of the metrics changed meaningfully.
  79. return;
  80. }
  81. past_effective_connection_type_ = effective_connection_type;
  82. past_network_quality_ = network_quality;
  83. net_log_.AddEvent(NetLogEventType::NETWORK_QUALITY_CHANGED, [&] {
  84. return NetworkQualityChangedNetLogParams(
  85. network_quality.http_rtt(), network_quality.transport_rtt(),
  86. network_quality.downstream_throughput_kbps(),
  87. effective_connection_type);
  88. });
  89. }
  90. } // namespace net::nqe::internal