| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/nqe/event_creator.h" | 5 #include "net/nqe/event_creator.h" |
| 6 | 6 |
| 7 #include <stdlib.h> |
| 7 #include <memory> | 8 #include <memory> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "net/log/net_log_capture_mode.h" | 14 #include "net/log/net_log_capture_mode.h" |
| 14 #include "net/log/net_log_with_source.h" | 15 #include "net/log/net_log_with_source.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 namespace nqe { | 19 namespace nqe { |
| 19 | 20 |
| 20 namespace internal { | 21 namespace internal { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 std::unique_ptr<base::Value> EffectiveConnectionTypeChangedNetLogCallback( | 25 std::unique_ptr<base::Value> NetworkQualityChangedNetLogCallback( |
| 25 base::TimeDelta http_rtt, | 26 base::TimeDelta http_rtt, |
| 26 base::TimeDelta transport_rtt, | 27 base::TimeDelta transport_rtt, |
| 27 int32_t downstream_throughput_kbps, | 28 int32_t downstream_throughput_kbps, |
| 28 EffectiveConnectionType effective_connection_type, | 29 EffectiveConnectionType effective_connection_type, |
| 29 NetLogCaptureMode capture_mode) { | 30 NetLogCaptureMode capture_mode) { |
| 30 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 31 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 31 dict->SetInteger("http_rtt_ms", http_rtt.InMilliseconds()); | 32 dict->SetInteger("http_rtt_ms", http_rtt.InMilliseconds()); |
| 32 dict->SetInteger("transport_rtt_ms", transport_rtt.InMilliseconds()); | 33 dict->SetInteger("transport_rtt_ms", transport_rtt.InMilliseconds()); |
| 33 dict->SetInteger("downstream_throughput_kbps", downstream_throughput_kbps); | 34 dict->SetInteger("downstream_throughput_kbps", downstream_throughput_kbps); |
| 34 dict->SetString("effective_connection_type", | 35 dict->SetString("effective_connection_type", |
| 35 GetNameForEffectiveConnectionType(effective_connection_type)); | 36 GetNameForEffectiveConnectionType(effective_connection_type)); |
| 36 return std::move(dict); | 37 return std::move(dict); |
| 37 } | 38 } |
| 38 | 39 |
| 40 bool MetricChangedMeaningfully(int32_t past_value, int32_t current_value) { |
| 41 if ((past_value == INVALID_RTT_THROUGHPUT) != |
| 42 (current_value == INVALID_RTT_THROUGHPUT)) { |
| 43 return true; |
| 44 } |
| 45 |
| 46 if (past_value == INVALID_RTT_THROUGHPUT && |
| 47 current_value == INVALID_RTT_THROUGHPUT) { |
| 48 return false; |
| 49 } |
| 50 |
| 51 // Create a new entry only if (i) the difference between the two values exceed |
| 52 // the threshold; and, (ii) the ratio of the values also exceeds the |
| 53 // threshold. |
| 54 static const int kMinDifferenceInMetrics = 100; |
| 55 static const float kMinRatio = 1.2f; |
| 56 |
| 57 if (std::abs(past_value - current_value) < kMinDifferenceInMetrics) { |
| 58 // The absolute change in the value is not sufficient enough. |
| 59 return false; |
| 60 } |
| 61 |
| 62 if (past_value < (kMinRatio * current_value) && |
| 63 current_value < (kMinRatio * past_value)) { |
| 64 // The relative change in the value is not sufficient enough. |
| 65 return false; |
| 66 } |
| 67 |
| 68 return true; |
| 69 } |
| 70 |
| 39 } // namespace | 71 } // namespace |
| 40 | 72 |
| 41 EventCreator::EventCreator(NetLogWithSource net_log) | 73 EventCreator::EventCreator(NetLogWithSource net_log) |
| 42 : net_log_(net_log), | 74 : net_log_(net_log), |
| 43 past_effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {} | 75 past_effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {} |
| 44 | 76 |
| 45 EventCreator::~EventCreator() { | 77 EventCreator::~EventCreator() { |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | 78 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 } | 79 } |
| 48 | 80 |
| 49 void EventCreator::MaybeAddEffectiveConnectionTypeChangedEventToNetLog( | 81 void EventCreator::MaybeAddNetworkQualityChangedEventToNetLog( |
| 50 EffectiveConnectionType effective_connection_type, | 82 EffectiveConnectionType effective_connection_type, |
| 51 const NetworkQuality& network_quality) { | 83 const NetworkQuality& network_quality) { |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | 84 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 | 85 |
| 54 // Check if any of the network quality metrics changed meaningfully. | 86 // Check if any of the network quality metrics changed meaningfully. |
| 55 bool effective_connection_type_changed = | 87 bool effective_connection_type_changed = |
| 56 past_effective_connection_type_ != effective_connection_type; | 88 past_effective_connection_type_ != effective_connection_type; |
| 57 bool http_rtt_changed = (past_network_quality_.http_rtt() == InvalidRTT()) != | 89 bool http_rtt_changed = MetricChangedMeaningfully( |
| 58 (network_quality.http_rtt() == InvalidRTT()); | 90 past_network_quality_.http_rtt().InMilliseconds(), |
| 59 bool transport_rtt_changed = | 91 network_quality.http_rtt().InMilliseconds()); |
| 60 (past_network_quality_.transport_rtt() == InvalidRTT()) != | 92 |
| 61 (network_quality.transport_rtt() == InvalidRTT()); | 93 bool transport_rtt_changed = MetricChangedMeaningfully( |
| 62 bool kbps_changed = | 94 past_network_quality_.transport_rtt().InMilliseconds(), |
| 63 (past_network_quality_.downstream_throughput_kbps() == | 95 network_quality.transport_rtt().InMilliseconds()); |
| 64 INVALID_RTT_THROUGHPUT) != | 96 bool kbps_changed = MetricChangedMeaningfully( |
| 65 (network_quality.downstream_throughput_kbps() == INVALID_RTT_THROUGHPUT); | 97 past_network_quality_.downstream_throughput_kbps(), |
| 98 network_quality.downstream_throughput_kbps()); |
| 66 | 99 |
| 67 if (!effective_connection_type_changed && !http_rtt_changed && | 100 if (!effective_connection_type_changed && !http_rtt_changed && |
| 68 !transport_rtt_changed && !kbps_changed) { | 101 !transport_rtt_changed && !kbps_changed) { |
| 69 // Return since none of the metrics changed meaningfully. | 102 // Return since none of the metrics changed meaningfully. |
| 70 return; | 103 return; |
| 71 } | 104 } |
| 72 | 105 |
| 73 past_effective_connection_type_ = effective_connection_type; | 106 past_effective_connection_type_ = effective_connection_type; |
| 74 past_network_quality_ = network_quality; | 107 past_network_quality_ = network_quality; |
| 75 | 108 |
| 76 net_log_.AddEvent( | 109 net_log_.AddEvent( |
| 77 NetLogEventType::NETWORK_QUALITY_CHANGED, | 110 NetLogEventType::NETWORK_QUALITY_CHANGED, |
| 78 base::Bind(&EffectiveConnectionTypeChangedNetLogCallback, | 111 base::Bind(&NetworkQualityChangedNetLogCallback, |
| 79 network_quality.http_rtt(), network_quality.transport_rtt(), | 112 network_quality.http_rtt(), network_quality.transport_rtt(), |
| 80 network_quality.downstream_throughput_kbps(), | 113 network_quality.downstream_throughput_kbps(), |
| 81 effective_connection_type)); | 114 effective_connection_type)); |
| 82 } | 115 } |
| 83 | 116 |
| 84 } // namespace internal | 117 } // namespace internal |
| 85 | 118 |
| 86 } // namespace nqe | 119 } // namespace nqe |
| 87 | 120 |
| 88 } // namespace net | 121 } // namespace net |
| OLD | NEW |