最近被hosting vps主机的速度给困扰了,干脆放下手中的活 测试下
test.php放在网站根目录即可
代码如下:
<?php
/*** 最终版服务器性能测试工具* 测试项目:CPU运算性能、内存读写速度、硬盘IO速度、网络下载速度*/// 配置参数(已优化以控制总测试时间在100秒内)
$config = ['cpu_test_duration' => 3, // CPU测试持续秒数'mem_test_size' => 32, // 内存测试大小(MB)'disk_test_size' => 50, // 硬盘测试大小(MB)'disk_block_size' => 4, // 硬盘测试块大小(KB)'net_test_size_mb' => 50, // 网络测试下载大小(MB)'net_test_url' => 'https://speed.hetzner.de/100MB.bin', // 网络测试文件'tmp_file' => __DIR__ . '/benchmark_tempfile.tmp' // 临时文件路径
];// 工具函数:格式化大小显示
function formatSize($bytes) {$units = ['B', 'KB', 'MB', 'GB', 'TB'];$unit = 0;while ($bytes >= 1024 && $unit < 4) {$bytes /= 1024;$unit++;}return number_format($bytes, 2) . ' ' . $units[$unit];
}// 工具函数:格式化时间显示
function formatTime($seconds) {if ($seconds < 1) return number_format($seconds * 1000, 1) . ' ms';return number_format($seconds, 3) . ' s';
}// 工具函数:输出分隔线
function printSeparator() {echo "----------------------------------------\n";
}// 工具函数:确保在浏览器和CLI中都能正确换行
function eol() {return (PHP_SAPI === 'cli') ? "\n" : "<br>\n";
}// 记录总开始时间
$start_time = microtime(true);// 标题显示
echo eol();
printSeparator();
echo " 服务器性能测试结果 " . eol();
printSeparator();
echo "测试时间:" . date('Y-m-d H:i:s') . eol() . eol();// 1. CPU性能测试(计算质数)
echo "[1/4] CPU性能测试中...";
$startTime = microtime(true);
$primeCount = 0;
$currentNumber = 2;while (microtime(true) - $startTime < $config['cpu_test_duration']) {$isPrime = true;for ($i = 2; $i <= sqrt($currentNumber); $i++) {if ($currentNumber % $i == 0) {$isPrime = false;break;}}if ($isPrime) $primeCount++;$currentNumber++;
}$cpuTime = microtime(true) - $startTime;
$cpuResult = ['duration' => $cpuTime,'prime_count' => $primeCount,'per_second' => $primeCount / $cpuTime
];
echo "完成" . eol();// 2. 内存性能测试
echo "[2/4] 内存性能测试中...";
$memSize = $config['mem_test_size'] * 1024 * 1024; // 转换为字节
$blockSize = 4096;
$blocks = $memSize / $blockSize;
$data = [];// 内存写入测试
$startTime = microtime(true);
for ($i = 0; $i < $blocks; $i++) {$data[] = str_repeat(chr(rand(0, 255)), $blockSize);
}
$writeTime = microtime(true) - $startTime;// 内存读取测试
$startTime = microtime(true);
$checksum = 0;
for ($i = 0; $i < $blocks; $i++) {$checksum += crc32($data[$i]);
}
$readTime = microtime(true) - $startTime;// 释放内存
unset($data);$memResult = ['write_speed' => $config['mem_test_size'] / $writeTime,'read_speed' => $config['mem_test_size'] / $readTime
];
echo "完成" . eol();// 3. 硬盘IO测试
echo "[3/4] 硬盘IO测试中...";
$testFile = $config['tmp_file'];
$diskSize = $config['disk_test_size'] * 1024 * 1024;
$blockSize = $config['disk_block_size'] * 1024;
$totalBlocks = $diskSize / $blockSize;
$randomData = random_bytes($blockSize);// 写入测试
$startTime = microtime(true);
$file = fopen($testFile, 'wb');
for ($i = 0; $i < $totalBlocks; $i++) {fwrite($file, $randomData);
}
fclose($file);
$writeTime = microtime(true) - $startTime;// 读取测试
$startTime = microtime(true);
$file = fopen($testFile, 'rb');
for ($i = 0; $i < $totalBlocks; $i++) {fread($file, $blockSize);
}
fclose($file);
$readTime = microtime(true) - $startTime;// 删除测试文件
@unlink($testFile);$diskResult = ['write_speed' => $config['disk_test_size'] / $writeTime,'read_speed' => $config['disk_test_size'] / $readTime,'write_iops' => $totalBlocks / $writeTime,'read_iops' => $totalBlocks / $readTime
];
echo "完成" . eol();// 4. 网络速度测试(下载测试)
echo "[4/4] 网络速度测试中...";
$testUrl = $config['net_test_url'];
$startTime = microtime(true);
$ch = curl_init($testUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOPROGRESS, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 最大超时60秒
$content = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$downloadSize = min(curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD) / 1024 / 1024, $config['net_test_size_mb']); // MB
$downloadTime = microtime(true) - $startTime;
curl_close($ch);$netResult = ['speed' => 0,'size' => $downloadSize,'time' => $downloadTime
];if ($httpCode == 200 && $downloadSize > 0 && $downloadTime > 0) {$netResult['speed'] = $downloadSize / $downloadTime;
}
echo "完成" . eol() . eol();// 最终汇总报告
printSeparator();
echo " 测试结果汇总 " . eol();
printSeparator();
echo "1. CPU性能:" . number_format($cpuResult['per_second'], 1) . " 质数/秒" . eol();
echo " 测试时间:" . formatTime($cpuResult['duration']) . eol() . eol();
echo "2. 内存性能:" . eol();
echo " 写入速度:" . number_format($memResult['write_speed'], 2) . " MB/s" . eol();
echo " 读取速度:" . number_format($memResult['read_speed'], 2) . " MB/s" . eol() . eol();
echo "3. 硬盘IO:" . eol();
echo " 写入速度:" . number_format($diskResult['write_speed'], 2) . " MB/s(IOPS: " . number_format($diskResult['write_iops'], 0) . ")" . eol();
echo " 读取速度:" . number_format($diskResult['read_speed'], 2) . " MB/s(IOPS: " . number_format($diskResult['read_iops'], 0) . ")" . eol() . eol();
echo "4. 网络速度:" . eol();
echo " 下载速度:" . number_format($netResult['speed'], 2) . " MB/s(" . number_format($netResult['speed'] * 8, 2) . " Mbps)" . eol() . eol();
echo "测试总耗时:" . formatTime(microtime(true) - $start_time) . eol();
echo "测试完成时间:" . date('Y-m-d H:i:s') . eol() . eol();
?>
不得不服气阿里云,在国内一直是遥遥领先的存在!