Chromium Code Reviews| 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 // If the difference between the current network quality metric and the value of |
| 26 // the metric when the net log entry was last created is more than | |
| 27 // |kMinDifferenceInMetrics|, then a new log entry is created. For RTTs, the | |
| 28 // difference in last entry and the current value should be more than | |
| 29 // |kMinDifferenceInMetrics| milliseconds. For throughput, the difference | |
| 30 // must be more than |kMinDifferenceInMetrics| kilobits per second. | |
| 31 static const int kMinDifferenceInMetrics = 100; | |
|
RyanSturm
2017/03/03 22:10:21
This is fine, but you might consider having the di
tbansal1
2017/03/03 23:06:35
Yeah, percentage difference is one way but then we
| |
| 32 | |
| 33 std::unique_ptr<base::Value> NetworkQualityChangedNetLogCallback( | |
| 25 base::TimeDelta http_rtt, | 34 base::TimeDelta http_rtt, |
| 26 base::TimeDelta transport_rtt, | 35 base::TimeDelta transport_rtt, |
| 27 int32_t downstream_throughput_kbps, | 36 int32_t downstream_throughput_kbps, |
| 28 EffectiveConnectionType effective_connection_type, | 37 EffectiveConnectionType effective_connection_type, |
| 29 NetLogCaptureMode capture_mode) { | 38 NetLogCaptureMode capture_mode) { |
| 30 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 39 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 31 dict->SetInteger("http_rtt_ms", http_rtt.InMilliseconds()); | 40 dict->SetInteger("http_rtt_ms", http_rtt.InMilliseconds()); |
| 32 dict->SetInteger("transport_rtt_ms", transport_rtt.InMilliseconds()); | 41 dict->SetInteger("transport_rtt_ms", transport_rtt.InMilliseconds()); |
| 33 dict->SetInteger("downstream_throughput_kbps", downstream_throughput_kbps); | 42 dict->SetInteger("downstream_throughput_kbps", downstream_throughput_kbps); |
| 34 dict->SetString("effective_connection_type", | 43 dict->SetString("effective_connection_type", |
| 35 GetNameForEffectiveConnectionType(effective_connection_type)); | 44 GetNameForEffectiveConnectionType(effective_connection_type)); |
| 36 return std::move(dict); | 45 return std::move(dict); |
| 37 } | 46 } |
| 38 | 47 |
| 48 bool MetricChangedMeaningfully(int32_t past_value, int32_t current_value) { | |
|
RyanSturm
2017/03/03 22:10:21
nit: the behavior is already correct, but adding a
tbansal1
2017/03/03 23:06:35
Done.
| |
| 49 if ((past_value == INVALID_RTT_THROUGHPUT) != | |
| 50 (current_value == INVALID_RTT_THROUGHPUT)) { | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 return std::abs(past_value - current_value) >= kMinDifferenceInMetrics; | |
| 55 } | |
| 56 | |
| 39 } // namespace | 57 } // namespace |
| 40 | 58 |
| 41 EventCreator::EventCreator(NetLogWithSource net_log) | 59 EventCreator::EventCreator(NetLogWithSource net_log) |
| 42 : net_log_(net_log), | 60 : net_log_(net_log), |
| 43 past_effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {} | 61 past_effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {} |
| 44 | 62 |
| 45 EventCreator::~EventCreator() { | 63 EventCreator::~EventCreator() { |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 } | 65 } |
| 48 | 66 |
| 49 void EventCreator::MaybeAddEffectiveConnectionTypeChangedEventToNetLog( | 67 void EventCreator::MaybeAddNetworkQualityChangedEventToNetLog( |
| 50 EffectiveConnectionType effective_connection_type, | 68 EffectiveConnectionType effective_connection_type, |
| 51 const NetworkQuality& network_quality) { | 69 const NetworkQuality& network_quality) { |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 | 71 |
| 54 // Check if any of the network quality metrics changed meaningfully. | 72 // Check if any of the network quality metrics changed meaningfully. |
| 55 bool effective_connection_type_changed = | 73 bool effective_connection_type_changed = |
| 56 past_effective_connection_type_ != effective_connection_type; | 74 past_effective_connection_type_ != effective_connection_type; |
| 57 bool http_rtt_changed = (past_network_quality_.http_rtt() == InvalidRTT()) != | 75 bool http_rtt_changed = MetricChangedMeaningfully( |
| 58 (network_quality.http_rtt() == InvalidRTT()); | 76 past_network_quality_.http_rtt().InMilliseconds(), |
| 59 bool transport_rtt_changed = | 77 network_quality.http_rtt().InMilliseconds()); |
| 60 (past_network_quality_.transport_rtt() == InvalidRTT()) != | 78 |
| 61 (network_quality.transport_rtt() == InvalidRTT()); | 79 bool transport_rtt_changed = MetricChangedMeaningfully( |
| 62 bool kbps_changed = | 80 past_network_quality_.transport_rtt().InMilliseconds(), |
| 63 (past_network_quality_.downstream_throughput_kbps() == | 81 network_quality.transport_rtt().InMilliseconds()); |
| 64 INVALID_RTT_THROUGHPUT) != | 82 bool kbps_changed = MetricChangedMeaningfully( |
| 65 (network_quality.downstream_throughput_kbps() == INVALID_RTT_THROUGHPUT); | 83 past_network_quality_.downstream_throughput_kbps(), |
| 84 network_quality.downstream_throughput_kbps()); | |
| 66 | 85 |
| 67 if (!effective_connection_type_changed && !http_rtt_changed && | 86 if (!effective_connection_type_changed && !http_rtt_changed && |
| 68 !transport_rtt_changed && !kbps_changed) { | 87 !transport_rtt_changed && !kbps_changed) { |
| 69 // Return since none of the metrics changed meaningfully. | 88 // Return since none of the metrics changed meaningfully. |
| 70 return; | 89 return; |
| 71 } | 90 } |
| 72 | 91 |
| 73 past_effective_connection_type_ = effective_connection_type; | 92 past_effective_connection_type_ = effective_connection_type; |
| 74 past_network_quality_ = network_quality; | 93 past_network_quality_ = network_quality; |
| 75 | 94 |
| 76 net_log_.AddEvent( | 95 net_log_.AddEvent( |
| 77 NetLogEventType::NETWORK_QUALITY_CHANGED, | 96 NetLogEventType::NETWORK_QUALITY_CHANGED, |
| 78 base::Bind(&EffectiveConnectionTypeChangedNetLogCallback, | 97 base::Bind(&NetworkQualityChangedNetLogCallback, |
| 79 network_quality.http_rtt(), network_quality.transport_rtt(), | 98 network_quality.http_rtt(), network_quality.transport_rtt(), |
| 80 network_quality.downstream_throughput_kbps(), | 99 network_quality.downstream_throughput_kbps(), |
| 81 effective_connection_type)); | 100 effective_connection_type)); |
| 82 } | 101 } |
| 83 | 102 |
| 84 } // namespace internal | 103 } // namespace internal |
| 85 | 104 |
| 86 } // namespace nqe | 105 } // namespace nqe |
| 87 | 106 |
| 88 } // namespace net | 107 } // namespace net |
| OLD | NEW |