xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_bandwidth.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2012 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 
5 #include "quiche/quic/core/quic_bandwidth.h"
6 
7 #include <cinttypes>
8 #include <string>
9 
10 #include "absl/strings/str_format.h"
11 #include "absl/strings/string_view.h"
12 
13 namespace quic {
14 
ToDebuggingValue() const15 std::string QuicBandwidth::ToDebuggingValue() const {
16   if (bits_per_second_ < 80000) {
17     return absl::StrFormat("%d bits/s (%d bytes/s)", bits_per_second_,
18                            bits_per_second_ / 8);
19   }
20 
21   double divisor;
22   char unit;
23   if (bits_per_second_ < 8 * 1000 * 1000) {
24     divisor = 1e3;
25     unit = 'k';
26   } else if (bits_per_second_ < INT64_C(8) * 1000 * 1000 * 1000) {
27     divisor = 1e6;
28     unit = 'M';
29   } else {
30     divisor = 1e9;
31     unit = 'G';
32   }
33 
34   double bits_per_second_with_unit = bits_per_second_ / divisor;
35   double bytes_per_second_with_unit = bits_per_second_with_unit / 8;
36   return absl::StrFormat("%.2f %cbits/s (%.2f %cbytes/s)",
37                          bits_per_second_with_unit, unit,
38                          bytes_per_second_with_unit, unit);
39 }
40 
41 }  // namespace quic
42