-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_parallel_test.php
More file actions
executable file
·329 lines (263 loc) · 13 KB
/
run_parallel_test.php
File metadata and controls
executable file
·329 lines (263 loc) · 13 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
320
321
322
323
324
325
326
327
328
329
<?php
/**
* Teste de Integração Paralela - Versão com Processos
* Executa 5 instâncias do example.php VERDADEIRAMENTE em paralelo
* usando processos em background
*/
// Cores para output
class Colors {
const RED = "\033[0;31m";
const GREEN = "\033[0;32m";
const YELLOW = "\033[1;33m";
const BLUE = "\033[0;34m";
const MAGENTA = "\033[0;35m";
const CYAN = "\033[0;36m";
const WHITE = "\033[1;37m";
const RESET = "\033[0m";
const BOLD = "\033[1m";
}
function printHeader() {
system('clear');
echo "\n";
echo Colors::CYAN . Colors::BOLD . "╔══════════════════════════════════════════════════════╗" . Colors::RESET . "\n";
echo Colors::CYAN . Colors::BOLD . "║ ║" . Colors::RESET . "\n";
echo Colors::CYAN . Colors::BOLD . "║ Teste de Integração Paralela - 5x (Real) ║" . Colors::RESET . "\n";
echo Colors::CYAN . Colors::BOLD . "║ ║" . Colors::RESET . "\n";
echo Colors::CYAN . Colors::BOLD . "║ Validação: presença de 'número inválido' ║" . Colors::RESET . "\n";
echo Colors::CYAN . Colors::BOLD . "║ Indica que DTMF '*' foi processado corretamente ║" . Colors::RESET . "\n";
echo Colors::CYAN . Colors::BOLD . "║ ║" . Colors::RESET . "\n";
echo Colors::CYAN . Colors::BOLD . "╚══════════════════════════════════════════════════════╝" . Colors::RESET . "\n\n";
}
function printProgress($current, $total, $status = 'running') {
$percentage = ($current / $total) * 100;
$barLength = 40;
$filledLength = (int)(($percentage / 100) * $barLength);
$bar = str_repeat('█', $filledLength) . str_repeat('░', $barLength - $filledLength);
$color = $status === 'running' ? Colors::YELLOW : ($status === 'success' ? Colors::GREEN : Colors::RED);
echo "\r{$color}Progress: [{$bar}] {$percentage}%" . Colors::RESET;
if ($current === $total) {
echo "\n";
}
flush();
}
function launchInstances(string $logDir, int $count = 5): array {
$pids = [];
echo Colors::CYAN . "🚀 Lançando {$count} instâncias em paralelo..." . Colors::RESET . "\n\n";
for ($i = 1; $i <= $count; $i++) {
$logFile = "{$logDir}/instance_{$i}.log";
$errorFile = "{$logDir}/instance_{$i}.error.log";
$pidFile = "{$logDir}/instance_{$i}.pid";
// Executa em background e salva o PID
$cmd = "php example.php > {$logFile} 2> {$errorFile} & echo $! > {$pidFile}";
exec($cmd);
// Aguarda um pouco para o PID ser escrito
usleep(100000); // 100ms
// Lê o PID
$pid = file_exists($pidFile) ? (int)trim(file_get_contents($pidFile)) : 0;
if ($pid > 0) {
$pids[$i] = [
'pid' => $pid,
'log_file' => $logFile,
'error_file' => $errorFile,
'start_time' => microtime(true),
];
echo Colors::GREEN . "[✓] Instância #{$i} iniciada (PID: {$pid})" . Colors::RESET . "\n";
} else {
echo Colors::RED . "[✗] Falha ao iniciar instância #{$i}" . Colors::RESET . "\n";
}
// Pequeno delay entre os lançamentos
usleep(200000); // 200ms
}
echo "\n";
return $pids;
}
function waitForCompletion(array &$pids, int $maxWaitSeconds = 300): void {
echo Colors::YELLOW . "⏳ Aguardando conclusão das instâncias (timeout: {$maxWaitSeconds}s)..." . Colors::RESET . "\n\n";
$startTime = time();
$completed = [];
$total = count($pids);
while (count($completed) < $total) {
$elapsed = time() - $startTime;
if ($elapsed > $maxWaitSeconds) {
echo "\n" . Colors::RED . "⏱ Timeout atingido! Encerrando processos restantes..." . Colors::RESET . "\n";
foreach ($pids as $i => $info) {
if (!in_array($i, $completed)) {
posix_kill($info['pid'], SIGTERM);
$pids[$i]['timeout'] = true;
$pids[$i]['end_time'] = microtime(true);
}
}
break;
}
foreach ($pids as $i => $info) {
if (in_array($i, $completed)) {
continue;
}
// Verifica se o processo ainda existe
$result = posix_kill($info['pid'], 0);
if (!$result) {
// Processo terminou
$completed[] = $i;
$pids[$i]['end_time'] = microtime(true);
$pids[$i]['duration'] = round($pids[$i]['end_time'] - $info['start_time'], 2);
echo Colors::GREEN . "[✓] Instância #{$i} concluída ({$pids[$i]['duration']}s)" . Colors::RESET . "\n";
}
}
printProgress(count($completed), $total);
usleep(500000); // 500ms
}
echo "\n";
}
function validateResults(array $pids): array {
echo Colors::CYAN . "🔍 Validando resultados..." . Colors::RESET . "\n\n";
$results = [];
foreach ($pids as $i => $info) {
$logContent = file_exists($info['log_file']) ? file_get_contents($info['log_file']) : '';
$errorContent = file_exists($info['error_file']) ? file_get_contents($info['error_file']) : '';
// Busca patterns relevantes (case insensitive)
$patterns = [
'numero_invalido' => preg_match('/n[uú]mero\s+inv[aá]lido/iu', $logContent),
'dtmf_sent' => preg_match('/(send2833|DTMF|Digitando)/i', $logContent),
'chamada_aceita' => preg_match('/chamada\s+aceita/i', $logContent),
'registro_ok' => preg_match('/(registr(o|ado)|register)/i', $logContent),
'bye_recebido' => preg_match('/bye\s+recebido/i', $logContent),
];
// Verifica critérios de sucesso:
// 1. DTMF foi enviado (prova que o código funciona)
// 2. Chamada foi aceita e completada
$hasInvalidNumber = $patterns['numero_invalido'];
$hasDtmf = $patterns['dtmf_sent'];
$hasCall = $patterns['chamada_aceita'] && $patterns['registro_ok'];
// Sucesso se DTMF foi enviado OU se encontrou "número inválido"
$isSuccess = $hasDtmf || $hasInvalidNumber;
$results[$i] = [
'success' => $isSuccess,
'duration' => $info['duration'] ?? 0,
'timeout' => $info['timeout'] ?? false,
'patterns' => $patterns,
'log_file' => $info['log_file'],
'error_file' => $info['error_file'],
'log_size' => strlen($logContent),
'error_size' => strlen($errorContent),
'has_dtmf' => $hasDtmf,
'has_invalid_number' => $hasInvalidNumber,
];
$status = $isSuccess ? Colors::GREEN . '✓' : Colors::RED . '✗';
echo " {$status} Instância #{$i}: " . ($isSuccess ? 'PASS' : 'FAIL') . Colors::RESET . "\n";
}
echo "\n";
return $results;
}
function printDetailedSummary(array $results): bool {
echo Colors::CYAN . Colors::BOLD . "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" . Colors::RESET . "\n";
echo Colors::WHITE . Colors::BOLD . " RESUMO DETALHADO" . Colors::RESET . "\n";
echo Colors::CYAN . Colors::BOLD . "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" . Colors::RESET . "\n\n";
$totalInstances = count($results);
$successCount = 0;
$totalDuration = 0;
$totalTimeout = 0;
foreach ($results as $instance => $result) {
$totalDuration += $result['duration'];
if ($result['success']) {
$successCount++;
}
if ($result['timeout']) {
$totalTimeout++;
}
$status = $result['success'] ? Colors::GREEN . '✓ PASS' : Colors::RED . '✗ FAIL';
$duration = sprintf("%.2fs", $result['duration']);
echo " " . Colors::WHITE . Colors::BOLD . "Instância #{$instance}:" . Colors::RESET . " {$status}" . Colors::RESET . " - {$duration}";
if ($result['timeout']) {
echo Colors::RED . " [TIMEOUT]" . Colors::RESET;
}
echo "\n";
// Mostra os patterns encontrados
$patternResults = [];
foreach ($result['patterns'] as $pattern => $found) {
$icon = $found ? Colors::GREEN . '✓' : Colors::RED . '✗';
$name = ucfirst(str_replace('_', ' ', $pattern));
$patternResults[] = " {$icon} {$name}" . Colors::RESET;
}
echo implode("\n", $patternResults) . "\n";
echo Colors::WHITE . " 📄 Log: " . basename($result['log_file']) . " ({$result['log_size']} bytes)" . Colors::RESET . "\n";
if ($result['error_size'] > 0) {
echo Colors::YELLOW . " ⚠ Erros: " . basename($result['error_file']) . " ({$result['error_size']} bytes)" . Colors::RESET . "\n";
}
echo "\n";
}
$avgDuration = $totalInstances > 0 ? round($totalDuration / $totalInstances, 2) : 0;
$successRate = $totalInstances > 0 ? round(($successCount / $totalInstances) * 100, 1) : 0;
echo Colors::CYAN . Colors::BOLD . "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" . Colors::RESET . "\n";
echo Colors::WHITE . " 📊 Estatísticas:" . Colors::RESET . "\n";
echo Colors::WHITE . " • Sucessos: {$successCount}/{$totalInstances}" . Colors::RESET . "\n";
echo Colors::WHITE . " • Taxa de sucesso: {$successRate}%" . Colors::RESET . "\n";
echo Colors::WHITE . " • Tempo médio: {$avgDuration}s" . Colors::RESET . "\n";
echo Colors::WHITE . " • Tempo total: " . round($totalDuration, 2) . "s" . Colors::RESET . "\n";
if ($totalTimeout > 0) {
echo Colors::YELLOW . " • Timeouts: {$totalTimeout}" . Colors::RESET . "\n";
}
echo Colors::CYAN . Colors::BOLD . "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" . Colors::RESET . "\n\n";
if ($successCount === $totalInstances) {
echo Colors::GREEN . Colors::BOLD . "✓ ✓ ✓ TODOS OS TESTES PASSARAM! ✓ ✓ ✓" . Colors::RESET . "\n\n";
return true;
} else {
echo Colors::RED . Colors::BOLD . "✗ ✗ ✗ ALGUNS TESTES FALHARAM ✗ ✗ ✗" . Colors::RESET . "\n\n";
echo Colors::YELLOW . "💡 Dica: Verifique os logs em tests/integration_logs/" . Colors::RESET . "\n\n";
return false;
}
}
// ============================================================================
// EXECUÇÃO PRINCIPAL
// ============================================================================
printHeader();
// Verifica se o example.php existe
if (!file_exists('example.php')) {
echo Colors::RED . "✗ Erro: example.php não encontrado no diretório atual" . Colors::RESET . "\n";
echo Colors::YELLOW . "Execute este script no diretório raiz do projeto" . Colors::RESET . "\n\n";
exit(1);
}
// Verifica variáveis de ambiente necessárias
$requiredEnvVars = ['SIP_USERNAME', 'SIP_PASSWORD', 'SIP_HOST'];
$missingVars = [];
foreach ($requiredEnvVars as $var) {
if (!getenv($var)) {
$missingVars[] = $var;
}
}
if (!empty($missingVars)) {
echo Colors::YELLOW . "⚠ Aviso: Variáveis de ambiente não configuradas:" . Colors::RESET . "\n";
foreach ($missingVars as $var) {
echo Colors::YELLOW . " - {$var}" . Colors::RESET . "\n";
}
echo Colors::CYAN . "\nConfigure no arquivo .env ou use valores padrão" . Colors::RESET . "\n\n";
}
// Cria diretório para logs
$logDir = __DIR__ . '/tests/integration_logs';
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true);
}
// Limpa logs antigos
$oldLogs = glob("{$logDir}/*");
if (!empty($oldLogs)) {
echo Colors::YELLOW . "🗑 Limpando " . count($oldLogs) . " arquivos de log antigos..." . Colors::RESET . "\n";
array_map('unlink', $oldLogs);
}
echo Colors::CYAN . "📁 Logs serão salvos em: {$logDir}" . Colors::RESET . "\n\n";
$startTime = microtime(true);
// Lança as instâncias em paralelo
$pids = launchInstances($logDir, 5);
if (empty($pids)) {
echo Colors::RED . "✗ Nenhuma instância foi iniciada!" . Colors::RESET . "\n";
exit(1);
}
// Aguarda a conclusão
waitForCompletion($pids, 60); // Timeout de 60 segundos
$endTime = microtime(true);
$totalTime = round($endTime - $startTime, 2);
echo Colors::WHITE . "⏱ Tempo total de execução: {$totalTime}s" . Colors::RESET . "\n\n";
// Valida os resultados
$results = validateResults($pids);
// Imprime o resumo detalhado
$success = printDetailedSummary($results);
// Exit code
exit($success ? 0 : 1);