summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/test/pc/e2e/stats_based_network_quality_metrics_reporter.cc
blob: 65dca5b518a54b44c01824ca50de37618fabdc8d (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
 *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "test/pc/e2e/stats_based_network_quality_metrics_reporter.h"

#include <cstdint>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "api/scoped_refptr.h"
#include "api/sequence_checker.h"
#include "api/stats/rtc_stats.h"
#include "api/stats/rtcstats_objects.h"
#include "api/test/metrics/metric.h"
#include "api/test/network_emulation/network_emulation_interfaces.h"
#include "api/test/network_emulation_manager.h"
#include "api/units/data_rate.h"
#include "api/units/timestamp.h"
#include "rtc_base/checks.h"
#include "rtc_base/event.h"
#include "rtc_base/ip_address.h"
#include "rtc_base/strings/string_builder.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/system/no_unique_address.h"
#include "system_wrappers/include/field_trial.h"
#include "test/pc/e2e/metric_metadata_keys.h"

namespace webrtc {
namespace webrtc_pc_e2e {
namespace {

using ::webrtc::test::ImprovementDirection;
using ::webrtc::test::Unit;

using NetworkLayerStats =
    StatsBasedNetworkQualityMetricsReporter::NetworkLayerStats;

constexpr TimeDelta kStatsWaitTimeout = TimeDelta::Seconds(1);

// Field trial which controls whether to report standard-compliant bytes
// sent/received per stream.  If enabled, padding and headers are not included
// in bytes sent or received.
constexpr char kUseStandardBytesStats[] = "WebRTC-UseStandardBytesStats";

EmulatedNetworkStats PopulateStats(std::vector<EmulatedEndpoint*> endpoints,
                                   NetworkEmulationManager* network_emulation) {
  rtc::Event stats_loaded;
  EmulatedNetworkStats stats;
  network_emulation->GetStats(endpoints, [&](EmulatedNetworkStats s) {
    stats = std::move(s);
    stats_loaded.Set();
  });
  bool stats_received = stats_loaded.Wait(kStatsWaitTimeout);
  RTC_CHECK(stats_received);
  return stats;
}

std::map<rtc::IPAddress, std::string> PopulateIpToPeer(
    const std::map<std::string, std::vector<EmulatedEndpoint*>>&
        peer_endpoints) {
  std::map<rtc::IPAddress, std::string> out;
  for (const auto& entry : peer_endpoints) {
    for (const EmulatedEndpoint* const endpoint : entry.second) {
      RTC_CHECK(out.find(endpoint->GetPeerLocalAddress()) == out.end())
          << "Two peers can't share the same endpoint";
      out.emplace(endpoint->GetPeerLocalAddress(), entry.first);
    }
  }
  return out;
}

// Accumulates emulated network stats being executed on the network thread.
// When all stats are collected stores it in thread safe variable.
class EmulatedNetworkStatsAccumulator {
 public:
  // `expected_stats_count` - the number of calls to
  // AddEndpointStats/AddUplinkStats/AddDownlinkStats the accumulator is going
  // to wait. If called more than expected, the program will crash.
  explicit EmulatedNetworkStatsAccumulator(size_t expected_stats_count)
      : not_collected_stats_count_(expected_stats_count) {
    RTC_DCHECK_GE(not_collected_stats_count_, 0);
    if (not_collected_stats_count_ == 0) {
      all_stats_collected_.Set();
    }
    sequence_checker_.Detach();
  }

  // Has to be executed on network thread.
  void AddEndpointStats(std::string peer_name, EmulatedNetworkStats stats) {
    RTC_DCHECK_RUN_ON(&sequence_checker_);
    n_stats_[peer_name].endpoints_stats = std::move(stats);
    DecrementNotCollectedStatsCount();
  }

  // Has to be executed on network thread.
  void AddUplinkStats(std::string peer_name, EmulatedNetworkNodeStats stats) {
    RTC_DCHECK_RUN_ON(&sequence_checker_);
    n_stats_[peer_name].uplink_stats = std::move(stats);
    DecrementNotCollectedStatsCount();
  }

  // Has to be executed on network thread.
  void AddDownlinkStats(std::string peer_name, EmulatedNetworkNodeStats stats) {
    RTC_DCHECK_RUN_ON(&sequence_checker_);
    n_stats_[peer_name].downlink_stats = std::move(stats);
    DecrementNotCollectedStatsCount();
  }

  // Can be executed on any thread.
  // Returns true if count down was completed and false if timeout elapsed
  // before.
  bool Wait(TimeDelta timeout) { return all_stats_collected_.Wait(timeout); }

  // Can be called once. Returns all collected stats by moving underlying
  // object.
  std::map<std::string, NetworkLayerStats> ReleaseStats() {
    RTC_DCHECK(!stats_released_);
    stats_released_ = true;
    MutexLock lock(&mutex_);
    return std::move(stats_);
  }

 private:
  void DecrementNotCollectedStatsCount() {
    RTC_DCHECK_RUN_ON(&sequence_checker_);
    RTC_CHECK_GT(not_collected_stats_count_, 0)
        << "All stats are already collected";
    not_collected_stats_count_--;
    if (not_collected_stats_count_ == 0) {
      MutexLock lock(&mutex_);
      stats_ = std::move(n_stats_);
      all_stats_collected_.Set();
    }
  }

  RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
  size_t not_collected_stats_count_ RTC_GUARDED_BY(sequence_checker_);
  // Collected on the network thread. Moved into `stats_` after all stats are
  // collected.
  std::map<std::string, NetworkLayerStats> n_stats_
      RTC_GUARDED_BY(sequence_checker_);

  rtc::Event all_stats_collected_;
  Mutex mutex_;
  std::map<std::string, NetworkLayerStats> stats_ RTC_GUARDED_BY(mutex_);
  bool stats_released_ = false;
};

}  // namespace

StatsBasedNetworkQualityMetricsReporter::
    StatsBasedNetworkQualityMetricsReporter(
        std::map<std::string, std::vector<EmulatedEndpoint*>> peer_endpoints,
        NetworkEmulationManager* network_emulation,
        test::MetricsLogger* metrics_logger)
    : collector_(std::move(peer_endpoints), network_emulation),
      clock_(network_emulation->time_controller()->GetClock()),
      metrics_logger_(metrics_logger) {
  RTC_CHECK(metrics_logger_);
}

StatsBasedNetworkQualityMetricsReporter::NetworkLayerStatsCollector::
    NetworkLayerStatsCollector(
        std::map<std::string, std::vector<EmulatedEndpoint*>> peer_endpoints,
        NetworkEmulationManager* network_emulation)
    : peer_endpoints_(std::move(peer_endpoints)),
      ip_to_peer_(PopulateIpToPeer(peer_endpoints_)),
      network_emulation_(network_emulation) {}

void StatsBasedNetworkQualityMetricsReporter::NetworkLayerStatsCollector::
    Start() {
  MutexLock lock(&mutex_);
  // Check that network stats are clean before test execution.
  for (const auto& entry : peer_endpoints_) {
    EmulatedNetworkStats stats =
        PopulateStats(entry.second, network_emulation_);
    RTC_CHECK_EQ(stats.overall_outgoing_stats.packets_sent, 0);
    RTC_CHECK_EQ(stats.overall_incoming_stats.packets_received, 0);
  }
}

void StatsBasedNetworkQualityMetricsReporter::NetworkLayerStatsCollector::
    AddPeer(absl::string_view peer_name,
            std::vector<EmulatedEndpoint*> endpoints,
            std::vector<EmulatedNetworkNode*> uplink,
            std::vector<EmulatedNetworkNode*> downlink) {
  MutexLock lock(&mutex_);
  // When new peer is added not in the constructor, don't check if it has empty
  // stats, because their endpoint could be used for traffic before.
  peer_endpoints_.emplace(peer_name, std::move(endpoints));
  peer_uplinks_.emplace(peer_name, std::move(uplink));
  peer_downlinks_.emplace(peer_name, std::move(downlink));
  for (const EmulatedEndpoint* const endpoint : endpoints) {
    RTC_CHECK(ip_to_peer_.find(endpoint->GetPeerLocalAddress()) ==
              ip_to_peer_.end())
        << "Two peers can't share the same endpoint";
    ip_to_peer_.emplace(endpoint->GetPeerLocalAddress(), peer_name);
  }
}

std::map<std::string, NetworkLayerStats>
StatsBasedNetworkQualityMetricsReporter::NetworkLayerStatsCollector::
    GetStats() {
  MutexLock lock(&mutex_);
  EmulatedNetworkStatsAccumulator stats_accumulator(
      peer_endpoints_.size() + peer_uplinks_.size() + peer_downlinks_.size());
  for (const auto& entry : peer_endpoints_) {
    network_emulation_->GetStats(
        entry.second, [&stats_accumulator,
                       peer = entry.first](EmulatedNetworkStats s) mutable {
          stats_accumulator.AddEndpointStats(std::move(peer), std::move(s));
        });
  }
  for (const auto& entry : peer_uplinks_) {
    network_emulation_->GetStats(
        entry.second, [&stats_accumulator,
                       peer = entry.first](EmulatedNetworkNodeStats s) mutable {
          stats_accumulator.AddUplinkStats(std::move(peer), std::move(s));
        });
  }
  for (const auto& entry : peer_downlinks_) {
    network_emulation_->GetStats(
        entry.second, [&stats_accumulator,
                       peer = entry.first](EmulatedNetworkNodeStats s) mutable {
          stats_accumulator.AddDownlinkStats(std::move(peer), std::move(s));
        });
  }
  bool stats_collected = stats_accumulator.Wait(kStatsWaitTimeout);
  RTC_CHECK(stats_collected);
  std::map<std::string, NetworkLayerStats> peer_to_stats =
      stats_accumulator.ReleaseStats();
  std::map<std::string, std::vector<std::string>> sender_to_receivers;
  for (const auto& entry : peer_endpoints_) {
    const std::string& peer_name = entry.first;
    const NetworkLayerStats& stats = peer_to_stats[peer_name];
    for (const auto& income_stats_entry :
         stats.endpoints_stats.incoming_stats_per_source) {
      const rtc::IPAddress& source_ip = income_stats_entry.first;
      auto it = ip_to_peer_.find(source_ip);
      if (it == ip_to_peer_.end()) {
        // Source IP is unknown for this collector, so will be skipped.
        continue;
      }
      sender_to_receivers[it->second].push_back(peer_name);
    }
  }
  for (auto& entry : peer_to_stats) {
    const std::vector<std::string>& receivers =
        sender_to_receivers[entry.first];
    entry.second.receivers =
        std::set<std::string>(receivers.begin(), receivers.end());
  }
  return peer_to_stats;
}

void StatsBasedNetworkQualityMetricsReporter::AddPeer(
    absl::string_view peer_name,
    std::vector<EmulatedEndpoint*> endpoints) {
  collector_.AddPeer(peer_name, std::move(endpoints), /*uplink=*/{},
                     /*downlink=*/{});
}

void StatsBasedNetworkQualityMetricsReporter::AddPeer(
    absl::string_view peer_name,
    std::vector<EmulatedEndpoint*> endpoints,
    std::vector<EmulatedNetworkNode*> uplink,
    std::vector<EmulatedNetworkNode*> downlink) {
  collector_.AddPeer(peer_name, std::move(endpoints), std::move(uplink),
                     std::move(downlink));
}

void StatsBasedNetworkQualityMetricsReporter::Start(
    absl::string_view test_case_name,
    const TrackIdStreamInfoMap* reporter_helper) {
  test_case_name_ = std::string(test_case_name);
  collector_.Start();
  start_time_ = clock_->CurrentTime();
}

void StatsBasedNetworkQualityMetricsReporter::OnStatsReports(
    absl::string_view pc_label,
    const rtc::scoped_refptr<const RTCStatsReport>& report) {
  PCStats cur_stats;

  auto inbound_stats = report->GetStatsOfType<RTCInboundRTPStreamStats>();
  for (const auto& stat : inbound_stats) {
    cur_stats.payload_received +=
        DataSize::Bytes(stat->bytes_received.ValueOrDefault(0ul) +
                        stat->header_bytes_received.ValueOrDefault(0ul));
  }

  auto outbound_stats = report->GetStatsOfType<RTCOutboundRTPStreamStats>();
  for (const auto& stat : outbound_stats) {
    cur_stats.payload_sent +=
        DataSize::Bytes(stat->bytes_sent.ValueOrDefault(0ul) +
                        stat->header_bytes_sent.ValueOrDefault(0ul));
  }

  auto candidate_pairs_stats = report->GetStatsOfType<RTCTransportStats>();
  for (const auto& stat : candidate_pairs_stats) {
    cur_stats.total_received +=
        DataSize::Bytes(stat->bytes_received.ValueOrDefault(0ul));
    cur_stats.total_sent +=
        DataSize::Bytes(stat->bytes_sent.ValueOrDefault(0ul));
    cur_stats.packets_received += stat->packets_received.ValueOrDefault(0ul);
    cur_stats.packets_sent += stat->packets_sent.ValueOrDefault(0ul);
  }

  MutexLock lock(&mutex_);
  pc_stats_[std::string(pc_label)] = cur_stats;
}

void StatsBasedNetworkQualityMetricsReporter::StopAndReportResults() {
  Timestamp end_time = clock_->CurrentTime();

  if (!webrtc::field_trial::IsEnabled(kUseStandardBytesStats)) {
    RTC_LOG(LS_ERROR)
        << "Non-standard GetStats; \"payload\" counts include RTP headers";
  }

  std::map<std::string, NetworkLayerStats> stats = collector_.GetStats();
  for (const auto& entry : stats) {
    LogNetworkLayerStats(entry.first, entry.second);
  }
  MutexLock lock(&mutex_);
  for (const auto& pair : pc_stats_) {
    auto it = stats.find(pair.first);
    RTC_CHECK(it != stats.end())
        << "Peer name used for PeerConnection stats collection and peer name "
           "used for endpoints naming doesn't match. No endpoints found for "
           "peer "
        << pair.first;
    const NetworkLayerStats& network_layer_stats = it->second;
    int64_t total_packets_received = 0;
    bool found = false;
    for (const auto& dest_peer : network_layer_stats.receivers) {
      auto pc_stats_it = pc_stats_.find(dest_peer);
      if (pc_stats_it == pc_stats_.end()) {
        continue;
      }
      found = true;
      total_packets_received += pc_stats_it->second.packets_received;
    }
    int64_t packet_loss = -1;
    if (found) {
      packet_loss = pair.second.packets_sent - total_packets_received;
    }
    ReportStats(pair.first, pair.second, network_layer_stats, packet_loss,
                end_time);
  }
}

void StatsBasedNetworkQualityMetricsReporter::ReportStats(
    const std::string& pc_label,
    const PCStats& pc_stats,
    const NetworkLayerStats& network_layer_stats,
    int64_t packet_loss,
    const Timestamp& end_time) {
  // TODO(bugs.webrtc.org/14757): Remove kExperimentalTestNameMetadataKey.
  std::map<std::string, std::string> metric_metadata{
      {MetricMetadataKey::kPeerMetadataKey, pc_label},
      {MetricMetadataKey::kExperimentalTestNameMetadataKey, test_case_name_}};
  metrics_logger_->LogSingleValueMetric(
      "bytes_discarded_no_receiver", GetTestCaseName(pc_label),
      network_layer_stats.endpoints_stats.overall_incoming_stats
          .bytes_discarded_no_receiver.bytes(),
      Unit::kBytes, ImprovementDirection::kNeitherIsBetter, metric_metadata);
  metrics_logger_->LogSingleValueMetric(
      "packets_discarded_no_receiver", GetTestCaseName(pc_label),
      network_layer_stats.endpoints_stats.overall_incoming_stats
          .packets_discarded_no_receiver,
      Unit::kUnitless, ImprovementDirection::kNeitherIsBetter, metric_metadata);

  metrics_logger_->LogSingleValueMetric(
      "payload_bytes_received", GetTestCaseName(pc_label),
      pc_stats.payload_received.bytes(), Unit::kBytes,
      ImprovementDirection::kNeitherIsBetter, metric_metadata);
  metrics_logger_->LogSingleValueMetric(
      "payload_bytes_sent", GetTestCaseName(pc_label),
      pc_stats.payload_sent.bytes(), Unit::kBytes,
      ImprovementDirection::kNeitherIsBetter, metric_metadata);

  metrics_logger_->LogSingleValueMetric(
      "bytes_sent", GetTestCaseName(pc_label), pc_stats.total_sent.bytes(),
      Unit::kBytes, ImprovementDirection::kNeitherIsBetter, metric_metadata);
  metrics_logger_->LogSingleValueMetric(
      "packets_sent", GetTestCaseName(pc_label), pc_stats.packets_sent,
      Unit::kUnitless, ImprovementDirection::kNeitherIsBetter, metric_metadata);
  metrics_logger_->LogSingleValueMetric(
      "average_send_rate", GetTestCaseName(pc_label),
      (pc_stats.total_sent / (end_time - start_time_)).kbps<double>(),
      Unit::kKilobitsPerSecond, ImprovementDirection::kNeitherIsBetter,
      metric_metadata);
  metrics_logger_->LogSingleValueMetric(
      "bytes_received", GetTestCaseName(pc_label),
      pc_stats.total_received.bytes(), Unit::kBytes,
      ImprovementDirection::kNeitherIsBetter, metric_metadata);
  metrics_logger_->LogSingleValueMetric(
      "packets_received", GetTestCaseName(pc_label), pc_stats.packets_received,
      Unit::kUnitless, ImprovementDirection::kNeitherIsBetter, metric_metadata);
  metrics_logger_->LogSingleValueMetric(
      "average_receive_rate", GetTestCaseName(pc_label),
      (pc_stats.total_received / (end_time - start_time_)).kbps<double>(),
      Unit::kKilobitsPerSecond, ImprovementDirection::kNeitherIsBetter,
      metric_metadata);
  metrics_logger_->LogSingleValueMetric(
      "sent_packets_loss", GetTestCaseName(pc_label), packet_loss,
      Unit::kUnitless, ImprovementDirection::kNeitherIsBetter, metric_metadata);
}

std::string StatsBasedNetworkQualityMetricsReporter::GetTestCaseName(
    absl::string_view network_label) const {
  rtc::StringBuilder builder;
  builder << test_case_name_ << "/" << network_label.data();
  return builder.str();
}

void StatsBasedNetworkQualityMetricsReporter::LogNetworkLayerStats(
    const std::string& peer_name,
    const NetworkLayerStats& stats) const {
  DataRate average_send_rate =
      stats.endpoints_stats.overall_outgoing_stats.packets_sent >= 2
          ? stats.endpoints_stats.overall_outgoing_stats.AverageSendRate()
          : DataRate::Zero();
  DataRate average_receive_rate =
      stats.endpoints_stats.overall_incoming_stats.packets_received >= 2
          ? stats.endpoints_stats.overall_incoming_stats.AverageReceiveRate()
          : DataRate::Zero();
  // TODO(bugs.webrtc.org/14757): Remove kExperimentalTestNameMetadataKey.
  std::map<std::string, std::string> metric_metadata{
      {MetricMetadataKey::kPeerMetadataKey, peer_name},
      {MetricMetadataKey::kExperimentalTestNameMetadataKey, test_case_name_}};
  rtc::StringBuilder log;
  log << "Raw network layer statistic for [" << peer_name << "]:\n"
      << "Local IPs:\n";
  for (size_t i = 0; i < stats.endpoints_stats.local_addresses.size(); ++i) {
    log << "  " << stats.endpoints_stats.local_addresses[i].ToString() << "\n";
  }
  if (!stats.endpoints_stats.overall_outgoing_stats.sent_packets_size
           .IsEmpty()) {
    metrics_logger_->LogMetric(
        "sent_packets_size", GetTestCaseName(peer_name),
        stats.endpoints_stats.overall_outgoing_stats.sent_packets_size,
        Unit::kBytes, ImprovementDirection::kNeitherIsBetter, metric_metadata);
  }
  if (!stats.endpoints_stats.overall_incoming_stats.received_packets_size
           .IsEmpty()) {
    metrics_logger_->LogMetric(
        "received_packets_size", GetTestCaseName(peer_name),
        stats.endpoints_stats.overall_incoming_stats.received_packets_size,
        Unit::kBytes, ImprovementDirection::kNeitherIsBetter, metric_metadata);
  }
  if (!stats.endpoints_stats.overall_incoming_stats
           .packets_discarded_no_receiver_size.IsEmpty()) {
    metrics_logger_->LogMetric(
        "packets_discarded_no_receiver_size", GetTestCaseName(peer_name),
        stats.endpoints_stats.overall_incoming_stats
            .packets_discarded_no_receiver_size,
        Unit::kBytes, ImprovementDirection::kNeitherIsBetter, metric_metadata);
  }
  if (!stats.endpoints_stats.sent_packets_queue_wait_time_us.IsEmpty()) {
    metrics_logger_->LogMetric(
        "sent_packets_queue_wait_time_us", GetTestCaseName(peer_name),
        stats.endpoints_stats.sent_packets_queue_wait_time_us, Unit::kUnitless,
        ImprovementDirection::kNeitherIsBetter, metric_metadata);
  }

  log << "Send statistic:\n"
      << "  packets: "
      << stats.endpoints_stats.overall_outgoing_stats.packets_sent << " bytes: "
      << stats.endpoints_stats.overall_outgoing_stats.bytes_sent.bytes()
      << " avg_rate (bytes/sec): " << average_send_rate.bytes_per_sec()
      << " avg_rate (bps): " << average_send_rate.bps() << "\n"
      << "Send statistic per destination:\n";

  for (const auto& entry :
       stats.endpoints_stats.outgoing_stats_per_destination) {
    DataRate source_average_send_rate = entry.second.packets_sent >= 2
                                            ? entry.second.AverageSendRate()
                                            : DataRate::Zero();
    log << "(" << entry.first.ToString() << "):\n"
        << "  packets: " << entry.second.packets_sent
        << " bytes: " << entry.second.bytes_sent.bytes()
        << " avg_rate (bytes/sec): " << source_average_send_rate.bytes_per_sec()
        << " avg_rate (bps): " << source_average_send_rate.bps() << "\n";
    if (!entry.second.sent_packets_size.IsEmpty()) {
      metrics_logger_->LogMetric(
          "sent_packets_size",
          GetTestCaseName(peer_name + "/" + entry.first.ToString()),
          entry.second.sent_packets_size, Unit::kBytes,
          ImprovementDirection::kNeitherIsBetter, metric_metadata);
    }
  }

  if (!stats.uplink_stats.packet_transport_time.IsEmpty()) {
    log << "[Debug stats] packet_transport_time=("
        << stats.uplink_stats.packet_transport_time.GetAverage() << ", "
        << stats.uplink_stats.packet_transport_time.GetStandardDeviation()
        << ")\n";
    metrics_logger_->LogMetric(
        "uplink_packet_transport_time", GetTestCaseName(peer_name),
        stats.uplink_stats.packet_transport_time, Unit::kMilliseconds,
        ImprovementDirection::kNeitherIsBetter, metric_metadata);
  }
  if (!stats.uplink_stats.size_to_packet_transport_time.IsEmpty()) {
    log << "[Debug stats] size_to_packet_transport_time=("
        << stats.uplink_stats.size_to_packet_transport_time.GetAverage() << ", "
        << stats.uplink_stats.size_to_packet_transport_time
               .GetStandardDeviation()
        << ")\n";
    metrics_logger_->LogMetric(
        "uplink_size_to_packet_transport_time", GetTestCaseName(peer_name),
        stats.uplink_stats.size_to_packet_transport_time, Unit::kUnitless,
        ImprovementDirection::kNeitherIsBetter, metric_metadata);
  }

  log << "Receive statistic:\n"
      << "  packets: "
      << stats.endpoints_stats.overall_incoming_stats.packets_received
      << " bytes: "
      << stats.endpoints_stats.overall_incoming_stats.bytes_received.bytes()
      << " avg_rate (bytes/sec): " << average_receive_rate.bytes_per_sec()
      << " avg_rate (bps): " << average_receive_rate.bps() << "\n"
      << "Receive statistic per source:\n";

  for (const auto& entry : stats.endpoints_stats.incoming_stats_per_source) {
    DataRate source_average_receive_rate =
        entry.second.packets_received >= 2 ? entry.second.AverageReceiveRate()
                                           : DataRate::Zero();
    log << "(" << entry.first.ToString() << "):\n"
        << "  packets: " << entry.second.packets_received
        << " bytes: " << entry.second.bytes_received.bytes()
        << " avg_rate (bytes/sec): "
        << source_average_receive_rate.bytes_per_sec()
        << " avg_rate (bps): " << source_average_receive_rate.bps() << "\n";
    if (!entry.second.received_packets_size.IsEmpty()) {
      metrics_logger_->LogMetric(
          "received_packets_size",
          GetTestCaseName(peer_name + "/" + entry.first.ToString()),
          entry.second.received_packets_size, Unit::kBytes,
          ImprovementDirection::kNeitherIsBetter, metric_metadata);
    }
    if (!entry.second.packets_discarded_no_receiver_size.IsEmpty()) {
      metrics_logger_->LogMetric(
          "packets_discarded_no_receiver_size",
          GetTestCaseName(peer_name + "/" + entry.first.ToString()),
          entry.second.packets_discarded_no_receiver_size, Unit::kBytes,
          ImprovementDirection::kNeitherIsBetter, metric_metadata);
    }
  }
  if (!stats.downlink_stats.packet_transport_time.IsEmpty()) {
    log << "[Debug stats] packet_transport_time=("
        << stats.downlink_stats.packet_transport_time.GetAverage() << ", "
        << stats.downlink_stats.packet_transport_time.GetStandardDeviation()
        << ")\n";
    metrics_logger_->LogMetric(
        "downlink_packet_transport_time", GetTestCaseName(peer_name),
        stats.downlink_stats.packet_transport_time, Unit::kMilliseconds,
        ImprovementDirection::kNeitherIsBetter, metric_metadata);
  }
  if (!stats.downlink_stats.size_to_packet_transport_time.IsEmpty()) {
    log << "[Debug stats] size_to_packet_transport_time=("
        << stats.downlink_stats.size_to_packet_transport_time.GetAverage()
        << ", "
        << stats.downlink_stats.size_to_packet_transport_time
               .GetStandardDeviation()
        << ")\n";
    metrics_logger_->LogMetric(
        "downlink_size_to_packet_transport_time", GetTestCaseName(peer_name),
        stats.downlink_stats.size_to_packet_transport_time, Unit::kUnitless,
        ImprovementDirection::kNeitherIsBetter, metric_metadata);
  }

  RTC_LOG(LS_INFO) << log.str();
}

}  // namespace webrtc_pc_e2e
}  // namespace webrtc