84 lines
3.2 KiB
PHP
84 lines
3.2 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
|
|
use App\Core\Env;
|
|
use App\Core\Cache;
|
|
use App\Core\Database;
|
|
use App\Models\WhatsAppSession;
|
|
|
|
Env::load(__DIR__ . '/.env');
|
|
|
|
echo "====================================================\n";
|
|
echo "🧪 NABEH ROUND-ROBIN & REDIS CACHING TEST SUITE\n";
|
|
echo "====================================================\n\n";
|
|
|
|
// 1. Check Redis Connection
|
|
echo "1️⃣ Checking Redis Connection...\n";
|
|
$redisEnabled = Env::get('REDIS_ENABLED', false);
|
|
echo " - REDIS_ENABLED setting: " . ($redisEnabled ? "ENABLED (true)" : "DISABLED (false)") . "\n";
|
|
|
|
$testKey = 'test_rr_' . time();
|
|
$setOk = Cache::set($testKey, ['test' => 'data', 'timestamp' => time()], 10);
|
|
$getVal = Cache::get($testKey);
|
|
Cache::delete($testKey);
|
|
|
|
if ($setOk && is_array($getVal) && isset($getVal['test'])) {
|
|
echo " - Redis Server Status: ✅ CONNECTED & OPERATIONAL\n\n";
|
|
} else {
|
|
echo " - Redis Server Status: ⚠️ DISCONNECTED (Using Database Fallback)\n\n";
|
|
}
|
|
|
|
// 2. Fetch Companies with Sessions
|
|
echo "2️⃣ Fetching WhatsApp Sessions from Database...\n";
|
|
$companies = Database::select("SELECT DISTINCT company_id FROM whatsapp_sessions ORDER BY company_id ASC");
|
|
|
|
if (empty($companies)) {
|
|
echo " - ⚠️ No WhatsApp sessions found in database.\n";
|
|
exit;
|
|
}
|
|
|
|
foreach ($companies as $comp) {
|
|
$companyId = (int)$comp['company_id'];
|
|
echo " ----------------------------------------------------\n";
|
|
echo " 🏬 Testing Company ID: {$companyId}\n";
|
|
|
|
// Clear cache first to test cold cache read
|
|
WhatsAppSession::clearSessionsCache($companyId);
|
|
|
|
$allSessions = WhatsAppSession::findAllByCompany($companyId);
|
|
$connectedSessions = WhatsAppSession::getConnectedSessionsForCompany($companyId);
|
|
|
|
echo " - Total Configured Sessions: " . count($allSessions) . "\n";
|
|
echo " - Total Connected Sessions: " . count($connectedSessions) . "\n";
|
|
|
|
foreach ($allSessions as $s) {
|
|
$statusIcon = ($s['status'] === 'connected') ? '🟢' : '🔴';
|
|
echo " [ID {$s['id']}] {$statusIcon} {$s['name']} ({$s['session_key']}) | Status: {$s['status']} | Phone: " . ($s['phone'] ?: 'N/A') . "\n";
|
|
}
|
|
|
|
echo "\n 3️⃣ Testing Round-Robin Selection (10 Requests Simulation)...\n";
|
|
$history = [];
|
|
for ($req = 1; $req <= 10; $req++) {
|
|
$selected = WhatsAppSession::getRoundRobinSession($companyId);
|
|
$name = $selected['name'] ?? 'N/A';
|
|
$key = $selected['session_key'] ?? 'N/A';
|
|
$status = $selected['status'] ?? 'N/A';
|
|
$history[] = $key;
|
|
echo " Request #{$req} ➔ Selected Session: {$name} ({$key}) [Status: {$status}]\n";
|
|
}
|
|
|
|
// Verify Distribution Metrics
|
|
$counts = array_count_values($history);
|
|
echo "\n 📊 Distribution Summary:\n";
|
|
foreach ($counts as $sessionKey => $count) {
|
|
$pct = round(($count / 10) * 100, 1);
|
|
echo " - Session {$sessionKey}: {$count} times ({$pct}%)\n";
|
|
}
|
|
echo " ----------------------------------------------------\n\n";
|
|
}
|
|
|
|
echo "====================================================\n";
|
|
echo "✅ TEST COMPLETED SUCCESSFULLY!\n";
|
|
echo "====================================================\n";
|