• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2018 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 #include "logging/rtc_event_log/rtc_event_processor.h"
12 
13 #include <stddef.h>
14 
15 #include <cstdint>
16 #include <initializer_list>
17 #include <numeric>
18 
19 #include "absl/memory/memory.h"
20 #include "logging/rtc_event_log/rtc_event_log_parser.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/random.h"
23 #include "test/gtest.h"
24 
25 namespace webrtc {
26 
27 namespace {
CreateEventList(std::initializer_list<int64_t> timestamp_list)28 std::vector<LoggedStartEvent> CreateEventList(
29     std::initializer_list<int64_t> timestamp_list) {
30   std::vector<LoggedStartEvent> v;
31   for (int64_t timestamp_ms : timestamp_list) {
32     v.emplace_back(timestamp_ms * 1000);  // Convert ms to us.
33   }
34   return v;
35 }
36 
37 std::vector<std::vector<LoggedStartEvent>>
CreateRandomEventLists(size_t num_lists,size_t num_elements,uint64_t seed)38 CreateRandomEventLists(size_t num_lists, size_t num_elements, uint64_t seed) {
39   Random prng(seed);
40   std::vector<std::vector<LoggedStartEvent>> lists(num_lists);
41   for (size_t elem = 0; elem < num_elements; elem++) {
42     uint32_t i = prng.Rand(0u, num_lists - 1);
43     int64_t timestamp_ms = elem;
44     lists[i].emplace_back(timestamp_ms * 1000);
45   }
46   return lists;
47 }
48 }  // namespace
49 
TEST(RtcEventProcessor,NoList)50 TEST(RtcEventProcessor, NoList) {
51   RtcEventProcessor processor;
52   processor.ProcessEventsInOrder();  // Don't crash but do nothing.
53 }
54 
TEST(RtcEventProcessor,EmptyList)55 TEST(RtcEventProcessor, EmptyList) {
56   auto not_called = [](LoggedStartEvent /*elem*/) { EXPECT_TRUE(false); };
57   std::vector<LoggedStartEvent> events;
58   RtcEventProcessor processor;
59 
60   processor.AddEvents(events, not_called);
61   processor.ProcessEventsInOrder();  // Don't crash but do nothing.
62 }
63 
TEST(RtcEventProcessor,OneList)64 TEST(RtcEventProcessor, OneList) {
65   std::vector<LoggedStartEvent> result;
66   auto f = [&result](LoggedStartEvent elem) { result.push_back(elem); };
67 
68   std::vector<LoggedStartEvent> events(CreateEventList({1, 2, 3, 4}));
69   RtcEventProcessor processor;
70   processor.AddEvents(events, f);
71   processor.ProcessEventsInOrder();
72 
73   std::vector<int64_t> expected_results{1, 2, 3, 4};
74   ASSERT_EQ(result.size(), expected_results.size());
75   for (size_t i = 0; i < expected_results.size(); i++) {
76     EXPECT_EQ(result[i].log_time_ms(), expected_results[i]);
77   }
78 }
79 
TEST(RtcEventProcessor,MergeTwoLists)80 TEST(RtcEventProcessor, MergeTwoLists) {
81   std::vector<LoggedStartEvent> result;
82   auto f = [&result](LoggedStartEvent elem) { result.push_back(elem); };
83 
84   std::vector<LoggedStartEvent> events1(CreateEventList({1, 2, 4, 7, 8, 9}));
85   std::vector<LoggedStartEvent> events2(CreateEventList({3, 5, 6, 10}));
86   RtcEventProcessor processor;
87   processor.AddEvents(events1, f);
88   processor.AddEvents(events2, f);
89   processor.ProcessEventsInOrder();
90 
91   std::vector<int64_t> expected_results{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
92   ASSERT_EQ(result.size(), expected_results.size());
93   for (size_t i = 0; i < expected_results.size(); i++) {
94     EXPECT_EQ(result[i].log_time_ms(), expected_results[i]);
95   }
96 }
97 
TEST(RtcEventProcessor,MergeTwoListsWithDuplicatedElements)98 TEST(RtcEventProcessor, MergeTwoListsWithDuplicatedElements) {
99   std::vector<LoggedStartEvent> result;
100   auto f = [&result](LoggedStartEvent elem) { result.push_back(elem); };
101 
102   std::vector<LoggedStartEvent> events1(CreateEventList({1, 2, 2, 3, 5, 5}));
103   std::vector<LoggedStartEvent> events2(CreateEventList({1, 3, 4, 4}));
104   RtcEventProcessor processor;
105   processor.AddEvents(events1, f);
106   processor.AddEvents(events2, f);
107   processor.ProcessEventsInOrder();
108 
109   std::vector<int64_t> expected_results{1, 1, 2, 2, 3, 3, 4, 4, 5, 5};
110   ASSERT_EQ(result.size(), expected_results.size());
111   for (size_t i = 0; i < expected_results.size(); i++) {
112     EXPECT_EQ(result[i].log_time_ms(), expected_results[i]);
113   }
114 }
115 
TEST(RtcEventProcessor,MergeManyLists)116 TEST(RtcEventProcessor, MergeManyLists) {
117   std::vector<LoggedStartEvent> result;
118   auto f = [&result](LoggedStartEvent elem) { result.push_back(elem); };
119 
120   constexpr size_t kNumLists = 5;
121   constexpr size_t kNumElems = 30;
122   constexpr uint64_t kSeed = 0xF3C6B91F;
123   std::vector<std::vector<LoggedStartEvent>> lists(
124       CreateRandomEventLists(kNumLists, kNumElems, kSeed));
125   RTC_DCHECK_EQ(lists.size(), kNumLists);
126   RtcEventProcessor processor;
127   for (const auto& list : lists) {
128     processor.AddEvents(list, f);
129   }
130   processor.ProcessEventsInOrder();
131 
132   std::vector<int64_t> expected_results(kNumElems);
133   std::iota(expected_results.begin(), expected_results.end(), 0);
134   ASSERT_EQ(result.size(), expected_results.size());
135   for (size_t i = 0; i < expected_results.size(); i++) {
136     EXPECT_EQ(result[i].log_time_ms(), expected_results[i]);
137   }
138 }
139 
TEST(RtcEventProcessor,DifferentTypes)140 TEST(RtcEventProcessor, DifferentTypes) {
141   std::vector<int64_t> result;
142   auto f1 = [&result](LoggedStartEvent elem) {
143     result.push_back(elem.log_time_ms());
144   };
145   auto f2 = [&result](LoggedStopEvent elem) {
146     result.push_back(elem.log_time_ms());
147   };
148 
149   std::vector<LoggedStartEvent> events1{LoggedStartEvent(2000)};
150   std::vector<LoggedStopEvent> events2{LoggedStopEvent(1000)};
151   RtcEventProcessor processor;
152   processor.AddEvents(events1, f1);
153   processor.AddEvents(events2, f2);
154   processor.ProcessEventsInOrder();
155 
156   std::vector<int64_t> expected_results{1, 2};
157   ASSERT_EQ(result.size(), expected_results.size());
158   for (size_t i = 0; i < expected_results.size(); i++) {
159     EXPECT_EQ(result[i], expected_results[i]);
160   }
161 }
162 
163 }  // namespace webrtc
164