-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_swoole_client.php
More file actions
executable file
·177 lines (141 loc) · 4.73 KB
/
example_swoole_client.php
File metadata and controls
executable file
·177 lines (141 loc) · 4.73 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
<?php
/**
* Swoole WebSocket Client for Opus Audio Server
* Tests the audio streaming server
*/
use Swoole\Coroutine;
use Swoole\Coroutine\Http\Client;
if (!extension_loaded('swoole')) {
die("✗ Swoole extension required\n");
}
if (!extension_loaded('opus')) {
die("✗ Opus extension required\n");
}
echo "=== Opus Audio Client Test ===\n\n";
// Generate test audio (440Hz sine wave)
function generateTestAudio($samples = 960, $frequency = 440, $sample_rate = 48000) {
$pcm = '';
for ($i = 0; $i < $samples; $i++) {
$value = (int)(sin(2 * M_PI * $frequency * $i / $sample_rate) * 16000);
$pcm .= pack('s', $value);
}
return $pcm;
}
Coroutine\run(function() {
$client = new Client('127.0.0.1', 9501);
$client->set(['timeout' => 10]);
// Upgrade to WebSocket
$ret = $client->upgrade('/');
if (!$ret) {
echo "✗ Failed to connect to server\n";
return;
}
echo "✓ Connected to server\n\n";
// Create Opus encoder
$opus = new opusChannel(48000, 1);
echo "✓ Opus encoder created\n\n";
// Test 1: Send ping
echo "[Test 1] Sending ping...\n";
$client->push(json_encode(['command' => 'ping']));
$frame = $client->recv(2.0);
if ($frame) {
$msg = json_decode($frame->data, true);
echo " Response: {$msg['type']}\n\n";
}
// Test 2: Get stats
echo "[Test 2] Getting stats...\n";
$client->push(json_encode(['command' => 'stats']));
$frame = $client->recv(2.0);
if ($frame) {
$msg = json_decode($frame->data, true);
echo " Client ID: {$msg['client_id']}\n";
echo " Sample Rate: {$msg['sample_rate']}\n";
echo " Channels: {$msg['channels']}\n\n";
}
// Test 3: Send audio data
echo "[Test 3] Sending audio data...\n";
$sent_count = 0;
$received_count = 0;
$total_processing_time = 0;
for ($i = 0; $i < 10; $i++) {
// Generate and encode audio
$pcm = generateTestAudio(960, 440 + ($i * 10));
$encoded = $opus->encode($pcm);
// Send to server
$client->push($encoded, WEBSOCKET_OPCODE_BINARY);
$sent_count++;
// Receive response (could be audio or stats)
Coroutine::create(function() use ($client, &$received_count, &$total_processing_time) {
$frame = $client->recv(2.0);
if ($frame) {
if ($frame->opcode === WEBSOCKET_OPCODE_BINARY) {
$received_count++;
echo ".";
} else {
$msg = json_decode($frame->data, true);
if ($msg['type'] === 'stats') {
$total_processing_time += $msg['processing_time_ms'];
}
}
}
});
Coroutine::sleep(0.1); // 100ms between frames
}
echo "\n";
echo " Sent: $sent_count frames\n";
echo " Received: $received_count frames\n";
if ($total_processing_time > 0) {
echo " Avg processing time: " . round($total_processing_time / $sent_count, 2) . "ms\n";
}
echo "\n";
// Test 4: Change configuration
echo "[Test 4] Changing configuration...\n";
$client->push(json_encode([
'command' => 'config',
'sample_rate' => 16000,
'channels' => 1
]));
$frame = $client->recv(2.0);
if ($frame) {
$msg = json_decode($frame->data, true);
echo " Config updated: {$msg['sample_rate']}Hz, {$msg['channels']}ch\n\n";
}
// Test 5: Reset processor
echo "[Test 5] Resetting processor...\n";
$client->push(json_encode(['command' => 'reset']));
$frame = $client->recv(2.0);
if ($frame) {
$msg = json_decode($frame->data, true);
echo " Reset: {$msg['type']}\n\n";
}
// Test 6: Stress test with concurrent sends
echo "[Test 6] Stress test (100 frames)...\n";
$start = microtime(true);
$errors = 0;
for ($i = 0; $i < 100; $i++) {
Coroutine::create(function() use ($client, $opus, &$errors) {
try {
$pcm = generateTestAudio(960);
$encoded = $opus->encode($pcm);
$client->push($encoded, WEBSOCKET_OPCODE_BINARY);
if ($i % 10 == 0) {
echo ".";
}
} catch (Throwable $e) {
$errors++;
}
});
if ($i % 10 == 0) {
Coroutine::sleep(0.01);
}
}
$elapsed = microtime(true) - $start;
echo "\n";
echo " Time: " . round($elapsed, 2) . "s\n";
echo " Throughput: " . round(100 / $elapsed, 2) . " frames/sec\n";
echo " Errors: $errors\n\n";
// Cleanup
$client->close();
$opus->destroy();
echo "✓ All tests completed\n";
});