实时网速测试工具 (实时网速测试在线)

深圳休闲 05-07 阅读:16 评论:0

本工具可以帮助您实时测试您的互联网速度,包括下载速度、上传速度和 ping 值。

0
0
0
实时网速测试工具 (实时网速测试在线) `script.js`: js const startTestButton = document.getElementById('start-test-button'); const downloadSpeed = document.getElementById('download-speed'); const uploadSpeed = document.getElementById('upload-speed'); const ping = document.getElementById('ping');startTestButton.addEventListener('click', () => {startTest(); });function startTest() {// 初始化测试变量let downloadStartTime;let downloadEndTime;let uploadStartTime;let uploadEndTime;let pingStartTime;let pingEndTime;// 下载速度测试const downloadTestUrl = 'https://speed.hetzner.de/100MB.bin';const downloadTestFileSize = 102400000; // 100MBconst downloadTest = new XMLHttpRequest();downloadTest.open('GET', downloadTestUrl);downloadTest.responseType = 'blob';downloadTest.onload = () => {downloadEndTime = performance.now();const downloadDuration = (downloadEndTime - downloadStartTime) / 1000; // ms to secondsconst downloadSpeedInMB = downloadTestFileSize / downloadDuration /1000000; // MB/sdownloadSpeed.textContent = downloadSpeedInMB.toFixed(2) + ' MB/s';};downloadTest.onerror = () => {downloadSpeed.textContent = '无法测试下载速度';};downloadStartTime = performance.now();downloadTest.send();// 上传速度测试const uploadTestUrl = 'https://speed.hetzner.de/upload';const uploadTestFileSize = 10240000; // 10MBconst uploadTestForm = new FormData();uploadTestForm.append('file', new File([new ArrayBuffer(uploadTestFileSize)], 'test-file.bin', { type: 'application/octet-stream' }));const uploadTest = new XMLHttpRequest();uploadTest.open('POST', uploadTestUrl);uploadTest.onload = () => {uploadEndTime = performance.now();const uploadDuration = (uploadEndTime - uploadStartTime) / 1000; // ms to secondsconst uploadSpeedInMB = uploadTestFileSize / uploadDuration / 1000000; // MB/suploadSpeed.textContent = uploadSpeedInMB.toFixed(2) + ' MB/s';};uploadTest.onerror = () => {uploadSpeed.textContent = '无法测试上传速度';};uploadStartTime = performance.now();uploadTest.send(uploadTestForm);// ping 测试const pingTestUrl = 'https://speed.hetzner.de/ping.php';const pingTest = new XMLHttpRequest();pingTest.open('GET', pingTestUrl);pingTest.onload = () => {pingEndTime = performance.now();const pingDuration = (pingEndTime - pingStartTime) / 1000; // ms to secondsping.textContent = pingDuration.toFixed(2) + ' ms';};pingTest.onerror = () => {ping.textContent = '无法测试 ping 值';};pingStartTime = performance.now();pingTest.send(); }`style.css`: css body{font-family: sans-serif; }h1 {margin-bottom: 1em; }p {margin-bottom: 1em; }test-results {display: flex;justify-content: space-around;align-items: center; }test-results div {width: 100px;text-align: center;font-size: 1.5rem;font-weight: bold;margin: 0 1em; }start-test-button {cursor: pointer;padding: 10px 20px;background-color: 007bff;color: fff;border: none;border-radius: 5px;margin-top: 1em; }
版权声明

本文仅代表作者观点,不代表深圳桑拿立场。
本文系作者授权发表,未经许可,不得转载。

分享:

扫一扫在手机阅读、分享本文

文章排行