xref: /aosp_15_r20/external/webrtc/test/pc/e2e/stats_based_network_quality_metrics_reporter.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef TEST_PC_E2E_STATS_BASED_NETWORK_QUALITY_METRICS_REPORTER_H_
12 #define TEST_PC_E2E_STATS_BASED_NETWORK_QUALITY_METRICS_REPORTER_H_
13 
14 #include <cstdint>
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "absl/strings/string_view.h"
23 #include "api/numerics/samples_stats_counter.h"
24 #include "api/test/metrics/metrics_logger.h"
25 #include "api/test/network_emulation/network_emulation_interfaces.h"
26 #include "api/test/network_emulation_manager.h"
27 #include "api/test/peerconnection_quality_test_fixture.h"
28 #include "api/units/data_size.h"
29 #include "api/units/timestamp.h"
30 #include "rtc_base/ip_address.h"
31 #include "rtc_base/synchronization/mutex.h"
32 
33 namespace webrtc {
34 namespace webrtc_pc_e2e {
35 
36 // TODO(titovartem): make this class testable and add tests.
37 class StatsBasedNetworkQualityMetricsReporter
38     : public PeerConnectionE2EQualityTestFixture::QualityMetricsReporter {
39  public:
40   // Emulated network layer stats for single peer.
41   struct NetworkLayerStats {
42     EmulatedNetworkStats endpoints_stats;
43     EmulatedNetworkNodeStats uplink_stats;
44     EmulatedNetworkNodeStats downlink_stats;
45     std::set<std::string> receivers;
46   };
47 
48   // `networks` map peer name to network to report network layer stability stats
49   // and to log network layer metrics.
50   StatsBasedNetworkQualityMetricsReporter(
51       std::map<std::string, std::vector<EmulatedEndpoint*>> peer_endpoints,
52       NetworkEmulationManager* network_emulation,
53       test::MetricsLogger* metrics_logger);
54   ~StatsBasedNetworkQualityMetricsReporter() override = default;
55 
56   void AddPeer(absl::string_view peer_name,
57                std::vector<EmulatedEndpoint*> endpoints);
58   void AddPeer(absl::string_view peer_name,
59                std::vector<EmulatedEndpoint*> endpoints,
60                std::vector<EmulatedNetworkNode*> uplink,
61                std::vector<EmulatedNetworkNode*> downlink);
62 
63   // Network stats must be empty when this method will be invoked.
64   void Start(absl::string_view test_case_name,
65              const TrackIdStreamInfoMap* reporter_helper) override;
66   void OnStatsReports(
67       absl::string_view pc_label,
68       const rtc::scoped_refptr<const RTCStatsReport>& report) override;
69   void StopAndReportResults() override;
70 
71  private:
72   struct PCStats {
73     // TODO(bugs.webrtc.org/10525): Separate audio and video counters. Depends
74     // on standard stat counters, enabled by field trial
75     // "WebRTC-UseStandardBytesStats".
76     DataSize payload_received = DataSize::Zero();
77     DataSize payload_sent = DataSize::Zero();
78 
79     // Total bytes/packets sent/received in all RTCTransport's.
80     DataSize total_received = DataSize::Zero();
81     DataSize total_sent = DataSize::Zero();
82     int64_t packets_received = 0;
83     int64_t packets_sent = 0;
84   };
85 
86   class NetworkLayerStatsCollector {
87    public:
88     NetworkLayerStatsCollector(
89         std::map<std::string, std::vector<EmulatedEndpoint*>> peer_endpoints,
90         NetworkEmulationManager* network_emulation);
91 
92     void Start();
93 
94     void AddPeer(absl::string_view peer_name,
95                  std::vector<EmulatedEndpoint*> endpoints,
96                  std::vector<EmulatedNetworkNode*> uplink,
97                  std::vector<EmulatedNetworkNode*> downlink);
98 
99     std::map<std::string, NetworkLayerStats> GetStats();
100 
101    private:
102     Mutex mutex_;
103     std::map<std::string, std::vector<EmulatedEndpoint*>> peer_endpoints_
104         RTC_GUARDED_BY(mutex_);
105     std::map<std::string, std::vector<EmulatedNetworkNode*>> peer_uplinks_
106         RTC_GUARDED_BY(mutex_);
107     std::map<std::string, std::vector<EmulatedNetworkNode*>> peer_downlinks_
108         RTC_GUARDED_BY(mutex_);
109     std::map<rtc::IPAddress, std::string> ip_to_peer_ RTC_GUARDED_BY(mutex_);
110     NetworkEmulationManager* const network_emulation_;
111   };
112 
113   void ReportStats(const std::string& pc_label,
114                    const PCStats& pc_stats,
115                    const NetworkLayerStats& network_layer_stats,
116                    int64_t packet_loss,
117                    const Timestamp& end_time);
118   std::string GetTestCaseName(absl::string_view network_label) const;
119   void LogNetworkLayerStats(const std::string& peer_name,
120                             const NetworkLayerStats& stats) const;
121 
122   NetworkLayerStatsCollector collector_;
123   Clock* const clock_;
124   test::MetricsLogger* const metrics_logger_;
125 
126   std::string test_case_name_;
127   Timestamp start_time_ = Timestamp::MinusInfinity();
128 
129   Mutex mutex_;
130   std::map<std::string, PCStats> pc_stats_ RTC_GUARDED_BY(mutex_);
131 };
132 
133 }  // namespace webrtc_pc_e2e
134 }  // namespace webrtc
135 
136 #endif  // TEST_PC_E2E_STATS_BASED_NETWORK_QUALITY_METRICS_REPORTER_H_
137