File: event_creator.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (112 lines) | stat: -rw-r--r-- 3,774 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/nqe/event_creator.h"

#include <stdlib.h>
#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "net/log/net_log_capture_mode.h"
#include "net/log/net_log_with_source.h"

namespace net::nqe::internal {

namespace {

base::Value::Dict NetworkQualityChangedNetLogParams(
    base::TimeDelta http_rtt,
    base::TimeDelta transport_rtt,
    int32_t downstream_throughput_kbps,
    EffectiveConnectionType effective_connection_type) {
  base::Value::Dict value;
  value.Set("http_rtt_ms", static_cast<int>(http_rtt.InMilliseconds()));
  value.Set("transport_rtt_ms",
            static_cast<int>(transport_rtt.InMilliseconds()));
  value.Set("downstream_throughput_kbps", downstream_throughput_kbps);
  value.Set("effective_connection_type",
            GetNameForEffectiveConnectionType(effective_connection_type));
  return value;
}

bool MetricChangedMeaningfully(int32_t past_value, int32_t current_value) {
  if ((past_value == INVALID_RTT_THROUGHPUT) !=
      (current_value == INVALID_RTT_THROUGHPUT)) {
    return true;
  }

  if (past_value == INVALID_RTT_THROUGHPUT &&
      current_value == INVALID_RTT_THROUGHPUT) {
    return false;
  }

  // Create a new entry only if (i) the difference between the two values exceed
  // the threshold; and, (ii) the ratio of the values also exceeds the
  // threshold.
  static const int kMinDifferenceInMetrics = 100;
  static const float kMinRatio = 1.2f;

  if (std::abs(past_value - current_value) < kMinDifferenceInMetrics) {
    // The absolute change in the value is not sufficient enough.
    return false;
  }

  if (past_value < (kMinRatio * current_value) &&
      current_value < (kMinRatio * past_value)) {
    // The relative change in the value is not sufficient enough.
    return false;
  }

  return true;
}

}  // namespace

EventCreator::EventCreator(NetLogWithSource net_log) : net_log_(net_log) {}

EventCreator::~EventCreator() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void EventCreator::MaybeAddNetworkQualityChangedEventToNetLog(
    EffectiveConnectionType effective_connection_type,
    const NetworkQuality& network_quality) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Check if any of the network quality metrics changed meaningfully.
  bool effective_connection_type_changed =
      past_effective_connection_type_ != effective_connection_type;
  bool http_rtt_changed = MetricChangedMeaningfully(
      past_network_quality_.http_rtt().InMilliseconds(),
      network_quality.http_rtt().InMilliseconds());

  bool transport_rtt_changed = MetricChangedMeaningfully(
      past_network_quality_.transport_rtt().InMilliseconds(),
      network_quality.transport_rtt().InMilliseconds());
  bool kbps_changed = MetricChangedMeaningfully(
      past_network_quality_.downstream_throughput_kbps(),
      network_quality.downstream_throughput_kbps());

  if (!effective_connection_type_changed && !http_rtt_changed &&
      !transport_rtt_changed && !kbps_changed) {
    // Return since none of the metrics changed meaningfully.
    return;
  }

  past_effective_connection_type_ = effective_connection_type;
  past_network_quality_ = network_quality;

  net_log_.AddEvent(NetLogEventType::NETWORK_QUALITY_CHANGED, [&] {
    return NetworkQualityChangedNetLogParams(
        network_quality.http_rtt(), network_quality.transport_rtt(),
        network_quality.downstream_throughput_kbps(),
        effective_connection_type);
  });
}

}  // namespace net::nqe::internal