-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_swoole_simple.php
More file actions
executable file
·319 lines (278 loc) · 8.82 KB
/
test_swoole_simple.php
File metadata and controls
executable file
·319 lines (278 loc) · 8.82 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
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
<?php
/**
* Simplified Swoole Tests for Opus Extension
* Tests real concurrent operations using Swoole coroutines
*/
if (!extension_loaded('swoole')) {
die("✗ Swoole extension not found. Install with: pecl install swoole\n");
}
if (!extension_loaded('opus')) {
die("✗ Opus extension not found.\n");
}
echo "=== Swoole + Opus Tests ===\n\n";
echo "Swoole version: " . swoole_version() . "\n\n";
use Swoole\Coroutine;
$passed = 0;
$failed = 0;
// Test 1: Basic concurrent encoding
echo "Test 1: 50 concurrent encode operations... ";
$errors = [];
Coroutine\run(function() use (&$errors) {
for ($i = 0; $i < 50; $i++) {
Coroutine::create(function() use ($i, &$errors) {
try {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', 1000), 960);
$encoded = $opus->encode($pcm);
if (strlen($encoded) === 0) {
$errors[] = "Empty encode result #$i";
}
$opus->destroy();
} catch (Throwable $e) {
$errors[] = "#$i: " . $e->getMessage();
}
});
}
});
if (empty($errors)) {
echo "✓ PASS\n";
$passed++;
} else {
echo "✗ FAIL: " . count($errors) . " errors\n";
$failed++;
}
// Test 2: Concurrent encode/decode
echo "Test 2: 30 concurrent encode/decode pipelines... ";
$errors = [];
Coroutine\run(function() use (&$errors) {
for ($i = 0; $i < 30; $i++) {
Coroutine::create(function() use ($i, &$errors) {
try {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', 2000), 960);
$encoded = $opus->encode($pcm);
Coroutine::sleep(0.001);
$decoded = $opus->decode($encoded);
if (strlen($decoded) === 0) {
$errors[] = "Empty decode #$i";
}
$opus->destroy();
} catch (Throwable $e) {
$errors[] = "#$i: " . $e->getMessage();
}
});
}
});
if (empty($errors)) {
echo "✓ PASS\n";
$passed++;
} else {
echo "✗ FAIL: " . count($errors) . " errors\n";
$failed++;
}
// Test 3: Mixed sample rates
echo "Test 3: Concurrent mixed sample rates... ";
$errors = [];
Coroutine\run(function() use (&$errors) {
$rates = [8000, 16000, 24000, 48000];
foreach ($rates as $rate) {
for ($i = 0; $i < 5; $i++) {
Coroutine::create(function() use ($rate, $i, &$errors) {
try {
$opus = new opusChannel($rate, 1);
$frame_size = $rate / 50; // 20ms
$pcm = str_repeat(pack('s', 3000), $frame_size);
$encoded = $opus->encode($pcm);
$decoded = $opus->decode($encoded);
if (strlen($decoded) === 0) {
$errors[] = "Empty at $rate Hz #$i";
}
$opus->destroy();
} catch (Throwable $e) {
$errors[] = "$rate Hz #$i: " . $e->getMessage();
}
});
}
}
});
if (empty($errors)) {
echo "✓ PASS\n";
$passed++;
} else {
echo "✗ FAIL: " . count($errors) . " errors\n";
foreach (array_slice($errors, 0, 3) as $err) {
echo " - $err\n";
}
$failed++;
}
// Test 4: Concurrent voice enhancement
echo "Test 4: 50 concurrent voice enhancements... ";
$errors = [];
Coroutine\run(function() use (&$errors) {
for ($i = 0; $i < 50; $i++) {
Coroutine::create(function() use ($i, &$errors) {
try {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', rand(-16000, 16000)), 1000);
$intensity = ($i % 10) / 10.0;
$enhanced = $opus->enhanceVoiceClarity($pcm, $intensity);
if (strlen($enhanced) !== strlen($pcm)) {
$errors[] = "Size mismatch #$i";
}
$opus->destroy();
} catch (Throwable $e) {
$errors[] = "#$i: " . $e->getMessage();
}
});
}
});
if (empty($errors)) {
echo "✓ PASS\n";
$passed++;
} else {
echo "✗ FAIL: " . count($errors) . " errors\n";
$failed++;
}
// Test 5: Concurrent resampling
echo "Test 5: 30 concurrent resample operations... ";
$errors = [];
Coroutine\run(function() use (&$errors) {
for ($i = 0; $i < 30; $i++) {
Coroutine::create(function() use ($i, &$errors) {
try {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', 4000), 4800);
$resampled = $opus->resample($pcm, 48000, 8000);
if (strlen($resampled) === 0) {
$errors[] = "Empty resample #$i";
}
$opus->destroy();
} catch (Throwable $e) {
$errors[] = "#$i: " . $e->getMessage();
}
});
}
});
if (empty($errors)) {
echo "✓ PASS\n";
$passed++;
} else {
echo "✗ FAIL: " . count($errors) . " errors\n";
$failed++;
}
// Test 6: Full pipeline stress
echo "Test 6: 20 full pipeline operations... ";
$errors = [];
Coroutine\run(function() use (&$errors) {
for ($i = 0; $i < 20; $i++) {
Coroutine::create(function() use ($i, &$errors) {
try {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', 5000), 960);
$encoded = $opus->encode($pcm);
Coroutine::sleep(0.001);
$decoded = $opus->decode($encoded);
Coroutine::sleep(0.001);
$enhanced = $opus->enhanceVoiceClarity($decoded, 1.0);
Coroutine::sleep(0.001);
$spatial = $opus->spatialStereoEnhance($enhanced, 1.0, 0.5);
if (strlen($spatial) === 0) {
$errors[] = "Pipeline failed #$i";
}
$opus->destroy();
} catch (Throwable $e) {
$errors[] = "#$i: " . $e->getMessage();
}
});
}
});
if (empty($errors)) {
echo "✓ PASS\n";
$passed++;
} else {
echo "✗ FAIL: " . count($errors) . " errors\n";
$failed++;
}
// Test 7: State isolation
echo "Test 7: State isolation between coroutines... ";
$errors = [];
Coroutine\run(function() use (&$errors) {
for ($pair = 0; $pair < 10; $pair++) {
// Low intensity
Coroutine::create(function() use ($pair, &$errors) {
try {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', 6000), 1000);
for ($i = 0; $i < 5; $i++) {
$opus->enhanceVoiceClarity($pcm, 0.1);
Coroutine::sleep(0.001);
}
$opus->destroy();
} catch (Throwable $e) {
$errors[] = "Pair $pair A: " . $e->getMessage();
}
});
// High intensity
Coroutine::create(function() use ($pair, &$errors) {
try {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', 6000), 1000);
for ($i = 0; $i < 5; $i++) {
$opus->enhanceVoiceClarity($pcm, 1.9);
Coroutine::sleep(0.001);
}
$opus->destroy();
} catch (Throwable $e) {
$errors[] = "Pair $pair B: " . $e->getMessage();
}
});
}
});
if (empty($errors)) {
echo "✓ PASS\n";
$passed++;
} else {
echo "✗ FAIL: " . count($errors) . " errors\n";
$failed++;
}
// Test 8: Throughput benchmark
echo "Test 8: Throughput (200 operations)... ";
$start = microtime(true);
$errors = [];
Coroutine\run(function() use (&$errors) {
for ($i = 0; $i < 200; $i++) {
Coroutine::create(function() use ($i, &$errors) {
try {
$opus = new opusChannel(48000, 1);
$pcm = str_repeat(pack('s', 1000), 960);
$encoded = $opus->encode($pcm);
$decoded = $opus->decode($encoded);
$opus->destroy();
} catch (Throwable $e) {
$errors[] = "#$i: " . $e->getMessage();
}
});
}
});
$elapsed = microtime(true) - $start;
$throughput = 200 / $elapsed;
if (empty($errors)) {
echo sprintf("✓ PASS (%.0f ops/sec)\n", $throughput);
$passed++;
} else {
echo "✗ FAIL: " . count($errors) . " errors\n";
$failed++;
}
// Summary
echo "\n=== Swoole Test Summary ===\n";
echo "Passed: $passed\n";
echo "Failed: $failed\n";
echo "Total: " . ($passed + $failed) . "\n";
if ($failed === 0) {
echo "\n✓ All Swoole tests passed!\n";
echo "Extension is safe for Swoole applications.\n";
exit(0);
} else {
echo "\n✗ Some tests failed.\n";
exit(1);
}