-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_audio_frame_stress.cpp
More file actions
259 lines (198 loc) · 8.92 KB
/
Copy pathtest_audio_frame_stress.cpp
File metadata and controls
259 lines (198 loc) · 8.92 KB
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
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <livekit/audio_frame.h>
#include <livekit/livekit.h>
#include <atomic>
#include <chrono>
#include <thread>
#include <vector>
namespace livekit::test {
class AudioFrameStressTest : public ::testing::Test {
protected:
void SetUp() override { livekit::initialize(livekit::LogLevel::Info); }
void TearDown() override { livekit::shutdown(); }
};
// Stress test: Rapid creation and destruction of AudioFrames
TEST_F(AudioFrameStressTest, RapidFrameCreation) {
const int num_iterations = 10000;
const int sample_rate = 48000;
const int num_channels = 2;
const int samples_per_channel = 960; // 20ms at 48kHz
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < num_iterations; ++i) {
AudioFrame frame = AudioFrame::create(sample_rate, num_channels, samples_per_channel);
ASSERT_EQ(frame.sampleRate(), sample_rate);
ASSERT_EQ(frame.numChannels(), num_channels);
ASSERT_EQ(frame.samplesPerChannel(), samples_per_channel);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Created " << num_iterations << " AudioFrames in " << duration.count() << "ms"
<< " (" << (num_iterations * 1000.0 / duration.count()) << " frames/sec)" << std::endl;
}
// Stress test: Large buffer allocation
TEST_F(AudioFrameStressTest, LargeBufferAllocation) {
const int sample_rate = 48000;
const int num_channels = 8; // 7.1 surround
const int samples_per_channel = 48000; // 1 second of audio
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 100; ++i) {
AudioFrame frame = AudioFrame::create(sample_rate, num_channels, samples_per_channel);
ASSERT_EQ(frame.totalSamples(), static_cast<size_t>(num_channels * samples_per_channel));
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Created 100 large (1 second, 8-channel) AudioFrames in " << duration.count() << "ms" << std::endl;
}
// Stress test: Concurrent frame creation from multiple threads
TEST_F(AudioFrameStressTest, ConcurrentFrameCreation) {
const int num_threads = 8;
const int frames_per_thread = 1000;
std::atomic<int> total_frames{0};
std::vector<std::thread> threads;
auto start = std::chrono::high_resolution_clock::now();
for (int t = 0; t < num_threads; ++t) {
threads.emplace_back([&total_frames, frames_per_thread]() {
for (int i = 0; i < frames_per_thread; ++i) {
AudioFrame frame = AudioFrame::create(48000, 2, 960);
if (frame.sampleRate() == 48000) {
total_frames.fetch_add(1, std::memory_order_relaxed);
}
}
});
}
for (auto& thread : threads) {
thread.join();
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
EXPECT_EQ(total_frames.load(), num_threads * frames_per_thread);
std::cout << "Created " << total_frames.load() << " AudioFrames across " << num_threads << " threads in "
<< duration.count() << "ms" << std::endl;
}
// Stress test: Memory pressure with many simultaneous frames
TEST_F(AudioFrameStressTest, MemoryPressure) {
const int num_frames = 1000;
std::vector<AudioFrame> frames;
frames.reserve(num_frames);
auto start = std::chrono::high_resolution_clock::now();
// Create many frames and keep them alive
for (int i = 0; i < num_frames; ++i) {
frames.push_back(AudioFrame::create(48000, 2, 960));
}
// Verify all frames are valid
for (const auto& frame : frames) {
ASSERT_EQ(frame.sampleRate(), 48000);
ASSERT_EQ(frame.numChannels(), 2);
ASSERT_EQ(frame.samplesPerChannel(), 960);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Held " << num_frames << " AudioFrames simultaneously in " << duration.count() << "ms" << std::endl;
// Frames are destroyed when vector goes out of scope
}
// Stress test: Data modification under load
TEST_F(AudioFrameStressTest, DataModificationUnderLoad) {
const int num_frames = 100;
const int modifications_per_frame = 100;
auto start = std::chrono::high_resolution_clock::now();
for (int f = 0; f < num_frames; ++f) {
AudioFrame frame = AudioFrame::create(48000, 2, 960);
auto& data = frame.data();
for (int m = 0; m < modifications_per_frame; ++m) {
// Simulate audio processing
for (size_t i = 0; i < data.size(); ++i) {
data[i] = static_cast<int16_t>((data[i] * 2 + m) % 32767);
}
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Modified " << num_frames << " frames " << modifications_per_frame << " times each in "
<< duration.count() << "ms" << std::endl;
}
// Stress test: Copy operations
TEST_F(AudioFrameStressTest, CopyOperationsStress) {
const int num_copies = 1000;
std::vector<int16_t> original_data(1920, 12345);
AudioFrame original(original_data, 48000, 2, 960);
auto start = std::chrono::high_resolution_clock::now();
std::vector<AudioFrame> copies;
copies.reserve(num_copies);
for (int i = 0; i < num_copies; ++i) {
copies.push_back(original);
}
// Verify all copies are independent
for (auto& copy : copies) {
ASSERT_EQ(copy.data()[0], 12345);
copy.data()[0] = 0; // Modify copy
}
// Original should be unchanged
ASSERT_EQ(original.data()[0], 12345);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Performed " << num_copies << " copy operations in " << duration.count() << "ms" << std::endl;
}
// Stress test: Move operations
TEST_F(AudioFrameStressTest, MoveOperationsStress) {
const int num_moves = 10000;
auto start = std::chrono::high_resolution_clock::now();
AudioFrame frame = AudioFrame::create(48000, 2, 960);
for (int i = 0; i < num_moves; ++i) {
AudioFrame moved = std::move(frame);
frame = std::move(moved);
}
ASSERT_EQ(frame.sampleRate(), 48000);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Performed " << num_moves << " move operations in " << duration.count() << "ms" << std::endl;
}
// Stress test: Simulated real-time audio processing
TEST_F(AudioFrameStressTest, SimulatedRealtimeProcessing) {
const int duration_seconds = 1;
const int sample_rate = 48000;
const int frame_size_ms = 10;
const int frames_per_second = 1000 / frame_size_ms;
const int total_frames = duration_seconds * frames_per_second;
const int samples_per_frame = sample_rate * frame_size_ms / 1000;
std::vector<AudioFrame> processed_frames;
processed_frames.reserve(total_frames);
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < total_frames; ++i) {
// Simulate receiving audio
AudioFrame frame = AudioFrame::create(sample_rate, 2, samples_per_frame);
// Simulate processing (apply simple gain)
auto& data = frame.data();
for (size_t j = 0; j < data.size(); ++j) {
data[j] = static_cast<int16_t>(data[j] * 0.8);
}
processed_frames.push_back(std::move(frame));
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
double processing_time_per_frame_us = static_cast<double>(duration.count()) / total_frames;
double available_time_per_frame_us = frame_size_ms * 1000.0;
std::cout << "Processed " << total_frames << " frames (" << duration_seconds << "s of audio)" << std::endl;
std::cout << "Average processing time per frame: " << processing_time_per_frame_us << "us" << std::endl;
std::cout << "Available time per frame: " << available_time_per_frame_us << "us" << std::endl;
std::cout << "Processing overhead: " << (processing_time_per_frame_us / available_time_per_frame_us * 100) << "%"
<< std::endl;
// Processing should be fast enough for real-time
EXPECT_LT(processing_time_per_frame_us, available_time_per_frame_us)
<< "Processing takes longer than real-time allows";
}
} // namespace livekit::test