stability.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <link rel="shortcut icon" href="favicon.ico" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no" />
  6. <meta charset="UTF-8" />
  7. <title>LibreSpeed - Stability Test</title>
  8. <style type="text/css">
  9. html,
  10. body {
  11. border: none;
  12. padding: 0;
  13. margin: 0;
  14. background: #ffffff;
  15. color: #202020;
  16. }
  17. body {
  18. text-align: center;
  19. font-family: "Roboto", sans-serif;
  20. }
  21. h1 {
  22. color: #404040;
  23. }
  24. nav {
  25. margin-bottom: 1em;
  26. }
  27. nav a {
  28. color: #6060aa;
  29. text-decoration: none;
  30. font-size: 0.95em;
  31. }
  32. nav a:hover {
  33. text-decoration: underline;
  34. }
  35. #controls {
  36. margin: 1em auto;
  37. }
  38. #durationSelect,
  39. #targetSelect {
  40. font-size: 1em;
  41. padding: 0.3em 0.5em;
  42. margin-right: 1em;
  43. }
  44. #startBtn {
  45. display: inline-block;
  46. margin: 0 auto;
  47. color: #6060aa;
  48. background-color: rgba(0, 0, 0, 0);
  49. border: 0.15em solid #6060ff;
  50. border-radius: 0.3em;
  51. transition: all 0.3s;
  52. box-sizing: border-box;
  53. width: 8em;
  54. height: 3em;
  55. line-height: 2.7em;
  56. cursor: pointer;
  57. box-shadow:
  58. 0 0 0 rgba(0, 0, 0, 0.1),
  59. inset 0 0 0 rgba(0, 0, 0, 0.1);
  60. }
  61. #startBtn:hover {
  62. box-shadow:
  63. 0 0 2em rgba(0, 0, 0, 0.1),
  64. inset 0 0 1em rgba(0, 0, 0, 0.1);
  65. }
  66. #startBtn:before {
  67. content: "Start";
  68. }
  69. #startBtn.running {
  70. background-color: #ff3030;
  71. border-color: #ff6060;
  72. color: #ffffff;
  73. }
  74. #startBtn.running:before {
  75. content: "Abort";
  76. }
  77. #resetBtn {
  78. display: inline-block;
  79. color: #808080;
  80. background-color: rgba(0, 0, 0, 0);
  81. border: 0.15em solid #a0a0a0;
  82. border-radius: 0.3em;
  83. transition: all 0.3s;
  84. box-sizing: border-box;
  85. width: 6em;
  86. height: 3em;
  87. line-height: 2.7em;
  88. cursor: pointer;
  89. margin-left: 0.5em;
  90. box-shadow:
  91. 0 0 0 rgba(0, 0, 0, 0.1),
  92. inset 0 0 0 rgba(0, 0, 0, 0.1);
  93. }
  94. #resetBtn:hover {
  95. box-shadow:
  96. 0 0 2em rgba(0, 0, 0, 0.1),
  97. inset 0 0 1em rgba(0, 0, 0, 0.1);
  98. }
  99. #serverArea {
  100. margin: 0.8em 0;
  101. }
  102. #server {
  103. font-size: 1em;
  104. padding: 0.2em;
  105. }
  106. #test {
  107. margin-top: 2em;
  108. margin-bottom: 2em;
  109. }
  110. div.testGroup {
  111. display: block;
  112. margin: 0 auto;
  113. }
  114. div.testArea2 {
  115. display: inline-block;
  116. width: 10em;
  117. height: 7em;
  118. position: relative;
  119. box-sizing: border-box;
  120. text-align: center;
  121. }
  122. div.testArea2 div.testName {
  123. display: block;
  124. text-align: center;
  125. font-size: 1.4em;
  126. }
  127. div.testArea2 div.meterText {
  128. display: inline-block;
  129. font-size: 2.5em;
  130. }
  131. div.meterText:empty:before {
  132. content: "0.00";
  133. }
  134. div.testArea2 div.unit {
  135. display: inline-block;
  136. }
  137. #rating {
  138. font-size: 1.3em;
  139. font-weight: bold;
  140. margin: 0.5em 0;
  141. }
  142. #rating.great {
  143. color: #2ecc71;
  144. }
  145. #rating.good {
  146. color: #27ae60;
  147. }
  148. #rating.poor {
  149. color: #e67e22;
  150. }
  151. #rating.bad {
  152. color: #e74c3c;
  153. }
  154. #rating.none {
  155. color: #808080;
  156. }
  157. #chartContainer {
  158. margin: 1em auto;
  159. width: 95%;
  160. max-width: 60em;
  161. position: relative;
  162. }
  163. #pingChart {
  164. width: 100%;
  165. height: 280px;
  166. border: 1px solid #e0e0e0;
  167. border-radius: 4px;
  168. background: #fafafa;
  169. }
  170. #optionalControls {
  171. margin: 1.5em auto;
  172. max-width: 40em;
  173. }
  174. #optionalControls label {
  175. display: block;
  176. margin: 0.5em 0;
  177. }
  178. #alertThreshold {
  179. vertical-align: middle;
  180. width: 200px;
  181. }
  182. #downloadCsvBtn {
  183. margin-top: 0.8em;
  184. font-size: 0.9em;
  185. padding: 0.4em 1em;
  186. cursor: pointer;
  187. border: 1px solid #6060aa;
  188. background: none;
  189. color: #6060aa;
  190. border-radius: 0.3em;
  191. }
  192. #downloadCsvBtn:hover {
  193. background: #6060aa;
  194. color: #ffffff;
  195. }
  196. .footer {
  197. margin: 2em 0 1em;
  198. }
  199. .footer a {
  200. color: #808080;
  201. font-size: 0.85em;
  202. }
  203. @media all and (max-width: 40em) {
  204. body {
  205. font-size: 0.8em;
  206. }
  207. #pingChart {
  208. height: 200px;
  209. }
  210. }
  211. @media all and (prefers-color-scheme: dark) {
  212. html,
  213. body {
  214. background: #202020;
  215. color: #f4f4f4;
  216. color-scheme: dark;
  217. }
  218. h1 {
  219. color: #e0e0e0;
  220. }
  221. nav a {
  222. color: #9090ff;
  223. }
  224. #pingChart {
  225. border-color: #404040;
  226. background: #1a1a1a;
  227. }
  228. a {
  229. color: #9090ff;
  230. }
  231. #startBtn {
  232. color: #9090ff;
  233. border-color: #7070ff;
  234. }
  235. #startBtn.running {
  236. background-color: #ff3030;
  237. border-color: #ff6060;
  238. color: #ffffff;
  239. }
  240. #resetBtn {
  241. color: #a0a0a0;
  242. border-color: #606060;
  243. }
  244. #downloadCsvBtn {
  245. color: #9090ff;
  246. border-color: #9090ff;
  247. }
  248. #downloadCsvBtn:hover {
  249. background: #9090ff;
  250. color: #202020;
  251. }
  252. .footer a {
  253. color: #a0a0a0;
  254. }
  255. }
  256. </style>
  257. </head>
  258. <body>
  259. <h1>LibreSpeed - Stability Test</h1>
  260. <nav><a href="./">&larr; Speed Test</a></nav>
  261. <div id="controls">
  262. <select id="durationSelect">
  263. <option value="60">60 Seconds</option>
  264. <option value="90">90 Seconds</option>
  265. <option value="120">2 Minutes</option>
  266. <option value="180">3 Minutes</option>
  267. <option value="300">5 Minutes</option>
  268. </select>
  269. <select id="targetSelect">
  270. <option value="">Local Server</option>
  271. <option value="https://www.google.com/generate_204">Google</option>
  272. <option value="https://www.cloudflare.com/cdn-cgi/trace">Cloudflare</option>
  273. <option value="https://www.apple.com/library/test/success.html">Apple</option>
  274. </select>
  275. <div id="startBtn" onclick="startStop()"></div>
  276. <div id="resetBtn" onclick="resetTest()">Reset</div>
  277. </div>
  278. <div id="serverArea" style="display: none">
  279. Server:
  280. <select id="server" onchange="onServerChange(this.value)"></select>
  281. </div>
  282. <div id="rating" class="none">--</div>
  283. <div id="test">
  284. <div class="testGroup">
  285. <div class="testArea2">
  286. <div class="testName">Current</div>
  287. <div id="statCurrent" class="meterText" style="color: #aa6060"></div>
  288. <div class="unit">ms</div>
  289. </div>
  290. <div class="testArea2">
  291. <div class="testName">Average</div>
  292. <div id="statAvg" class="meterText" style="color: #aa6060"></div>
  293. <div class="unit">ms</div>
  294. </div>
  295. <div class="testArea2">
  296. <div class="testName">Jitter</div>
  297. <div id="statJitter" class="meterText" style="color: #aa6060"></div>
  298. <div class="unit">ms</div>
  299. </div>
  300. </div>
  301. <div class="testGroup">
  302. <div class="testArea2">
  303. <div class="testName">Min</div>
  304. <div id="statMin" class="meterText"></div>
  305. <div class="unit">ms</div>
  306. </div>
  307. <div class="testArea2">
  308. <div class="testName">Max</div>
  309. <div id="statMax" class="meterText"></div>
  310. <div class="unit">ms</div>
  311. </div>
  312. <div class="testArea2">
  313. <div class="testName">Failed Requests</div>
  314. <div id="statLoss" class="meterText"></div>
  315. <div class="unit">%</div>
  316. </div>
  317. <div class="testArea2">
  318. <div class="testName">Elapsed</div>
  319. <div id="statElapsed" class="meterText">00:00</div>
  320. <div class="unit"></div>
  321. </div>
  322. </div>
  323. </div>
  324. <div id="chartContainer">
  325. <canvas id="pingChart"></canvas>
  326. </div>
  327. <div id="optionalControls">
  328. <label>
  329. Alert threshold:
  330. <input
  331. type="range"
  332. id="alertThreshold"
  333. min="0"
  334. max="500"
  335. step="10"
  336. value="0"
  337. oninput="updateThreshold(this.value)"
  338. />
  339. <span id="thresholdValue">Off</span>
  340. </label>
  341. <button id="downloadCsvBtn" onclick="downloadCsv()">Download CSV</button>
  342. </div>
  343. <div class="footer">
  344. <a href="https://github.com/librespeed/speedtest">Source code</a>
  345. </div>
  346. <script type="text/javascript">
  347. function I(i) {
  348. return document.getElementById(i);
  349. }
  350. // Server configuration (same pattern as index.html)
  351. var SPEEDTEST_SERVERS = [];
  352. // State
  353. var worker = null;
  354. var updater = null;
  355. var running = false;
  356. var allPingData = []; // full dataset for chart and CSV
  357. var latestData = null;
  358. var alertThresholdMs = 0;
  359. var lastBeepTime = 0;
  360. var audioCtx = null;
  361. var selectedServer = null;
  362. // Dark mode detection for canvas colors
  363. var isDark = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
  364. try {
  365. window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", function (e) {
  366. isDark = e.matches;
  367. });
  368. } catch (e) {}
  369. // Chart constants
  370. var VISIBLE_SECONDS = 60;
  371. var CHART_PADDING_LEFT = 50;
  372. var CHART_PADDING_RIGHT = 15;
  373. var CHART_PADDING_TOP = 15;
  374. var CHART_PADDING_BOTTOM = 30;
  375. // Load server list dynamically (for Docker frontend/dual modes where servers.json exists)
  376. function loadServers(callback) {
  377. var xhr = new XMLHttpRequest();
  378. xhr.onload = function () {
  379. try {
  380. var servers = JSON.parse(xhr.responseText);
  381. if (Array.isArray(servers) && servers.length > 0) {
  382. SPEEDTEST_SERVERS = servers;
  383. }
  384. } catch (e) {}
  385. callback();
  386. };
  387. xhr.onerror = function () {
  388. callback();
  389. };
  390. xhr.open("GET", "servers.json?r=" + Math.random());
  391. try {
  392. xhr.timeout = 2000;
  393. xhr.ontimeout = xhr.onerror;
  394. } catch (e) {}
  395. xhr.send();
  396. }
  397. // Server initialization
  398. function initServers() {
  399. if (SPEEDTEST_SERVERS.length === 0) {
  400. I("serverArea").style.display = "none";
  401. } else {
  402. I("serverArea").style.display = "";
  403. // ping servers to find best one
  404. var completed = 0;
  405. var best = null;
  406. for (var i = 0; i < SPEEDTEST_SERVERS.length; i++) {
  407. (function (idx) {
  408. pingServer(SPEEDTEST_SERVERS[idx], function (server, rtt) {
  409. SPEEDTEST_SERVERS[idx].pingT = rtt;
  410. if (rtt > 0 && (best === null || rtt < best.pingT)) best = SPEEDTEST_SERVERS[idx];
  411. completed++;
  412. if (completed === SPEEDTEST_SERVERS.length) {
  413. // populate dropdown
  414. for (var j = 0; j < SPEEDTEST_SERVERS.length; j++) {
  415. if (SPEEDTEST_SERVERS[j].pingT <= 0) continue;
  416. var opt = document.createElement("option");
  417. opt.value = j;
  418. opt.textContent = SPEEDTEST_SERVERS[j].name;
  419. if (SPEEDTEST_SERVERS[j] === best) opt.selected = true;
  420. I("server").appendChild(opt);
  421. }
  422. selectedServer = best;
  423. }
  424. });
  425. })(i);
  426. }
  427. }
  428. }
  429. function pingServer(server, callback) {
  430. var url =
  431. server.server + server.pingURL + (server.pingURL.match(/\?/) ? "&" : "?") + "cors=true&r=" + Math.random();
  432. var xhr = new XMLHttpRequest();
  433. var t = new Date().getTime();
  434. xhr.onload = function () {
  435. callback(server, new Date().getTime() - t);
  436. };
  437. xhr.onerror = function () {
  438. callback(server, -1);
  439. };
  440. xhr.open("GET", url);
  441. try {
  442. xhr.timeout = 2000;
  443. xhr.ontimeout = xhr.onerror;
  444. } catch (e) {}
  445. xhr.send();
  446. }
  447. function onServerChange(idx) {
  448. selectedServer = SPEEDTEST_SERVERS[idx];
  449. }
  450. // Start/Stop
  451. function startStop() {
  452. if (running) {
  453. // abort
  454. if (worker) worker.postMessage("abort");
  455. stopTest();
  456. } else {
  457. startTest();
  458. }
  459. }
  460. function startTest() {
  461. allPingData = [];
  462. latestData = null;
  463. resetUI();
  464. running = true;
  465. I("startBtn").className = "running";
  466. I("durationSelect").disabled = true;
  467. I("targetSelect").disabled = true;
  468. I("server").disabled = true;
  469. var externalTarget = I("targetSelect").value;
  470. var workerSettings = {
  471. duration: parseInt(I("durationSelect").value),
  472. ping_allowPerformanceApi: true,
  473. url_ping_external: externalTarget
  474. };
  475. if (!externalTarget && selectedServer) {
  476. workerSettings.url_ping = selectedServer.server + selectedServer.pingURL;
  477. workerSettings.mpot = true;
  478. }
  479. worker = new Worker("stability_worker.js?r=" + Math.random());
  480. worker.onmessage = function (e) {
  481. var data = JSON.parse(e.data);
  482. latestData = data;
  483. // accumulate ping data
  484. if (data.pingData && data.pingData.length > 0) {
  485. for (var i = 0; i < data.pingData.length; i++) {
  486. allPingData.push(data.pingData[i]);
  487. }
  488. }
  489. // check alert threshold
  490. if (alertThresholdMs > 0 && data.currentPing > alertThresholdMs) {
  491. playBeep();
  492. }
  493. if (data.testState >= 4) {
  494. stopTest();
  495. }
  496. };
  497. updater = setInterval(function () {
  498. if (worker) worker.postMessage("status");
  499. }, 200);
  500. worker.postMessage("start " + JSON.stringify(workerSettings));
  501. }
  502. function stopTest() {
  503. running = false;
  504. I("startBtn").className = "";
  505. I("durationSelect").disabled = false;
  506. I("targetSelect").disabled = false;
  507. I("server").disabled = false;
  508. if (updater) {
  509. clearInterval(updater);
  510. updater = null;
  511. }
  512. // request final status
  513. if (worker) {
  514. worker.postMessage("status");
  515. setTimeout(function () {
  516. if (worker) {
  517. try {
  518. worker.terminate();
  519. } catch (e) {}
  520. worker = null;
  521. }
  522. }, 500);
  523. }
  524. }
  525. function resetTest() {
  526. if (running) {
  527. if (worker) worker.postMessage("abort");
  528. stopTest();
  529. }
  530. allPingData = [];
  531. latestData = null;
  532. resetUI();
  533. drawChart();
  534. }
  535. function resetUI() {
  536. I("statCurrent").textContent = "";
  537. I("statAvg").textContent = "";
  538. I("statMin").textContent = "";
  539. I("statMax").textContent = "";
  540. I("statJitter").textContent = "";
  541. I("statLoss").textContent = "";
  542. I("statElapsed").textContent = "";
  543. I("rating").textContent = "--";
  544. I("rating").className = "none";
  545. }
  546. // Format helpers
  547. function fmt(v) {
  548. v = Number(v);
  549. if (isNaN(v) || v <= 0) return "--";
  550. if (v < 10) return v.toFixed(2);
  551. if (v < 100) return v.toFixed(1);
  552. return v.toFixed(0);
  553. }
  554. function fmtTime(sec) {
  555. var m = Math.floor(sec / 60);
  556. var s = Math.floor(sec % 60);
  557. return (m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s;
  558. }
  559. // Rating calculation
  560. function getRating(avg, jit, loss) {
  561. if (avg <= 0) return { text: "--", cls: "none" };
  562. if (avg < 30 && jit < 5 && loss < 0.5) return { text: "Great", cls: "great" };
  563. if (avg < 60 && jit < 15 && loss < 2) return { text: "Good", cls: "good" };
  564. if (avg < 100 && jit < 30 && loss < 5) return { text: "Poor", cls: "poor" };
  565. return { text: "Bad", cls: "bad" };
  566. }
  567. // UI update loop
  568. function updateUI() {
  569. if (!latestData) return;
  570. var d = latestData;
  571. I("statCurrent").textContent = fmt(d.currentPing);
  572. I("statAvg").textContent = fmt(d.avgPing);
  573. I("statMin").textContent = d.minPing > 0 ? fmt(d.minPing) : "--";
  574. I("statMax").textContent = fmt(d.maxPing);
  575. I("statJitter").textContent = fmt(d.jitter);
  576. I("statLoss").textContent = d.totalSamples > 0 ? d.packetLoss.toFixed(1) : "--";
  577. I("statElapsed").textContent = fmtTime(d.elapsed);
  578. var r = getRating(d.avgPing, d.jitter, d.packetLoss);
  579. I("rating").textContent = r.text;
  580. I("rating").className = r.cls;
  581. }
  582. // Canvas chart rendering
  583. function drawChart() {
  584. var canvas = I("pingChart");
  585. var ctx = canvas.getContext("2d");
  586. var dp = window.devicePixelRatio || 1;
  587. var cw = canvas.clientWidth * dp;
  588. var ch = canvas.clientHeight * dp;
  589. if (canvas.width !== cw || canvas.height !== ch) {
  590. canvas.width = cw;
  591. canvas.height = ch;
  592. }
  593. ctx.clearRect(0, 0, cw, ch);
  594. var padL = CHART_PADDING_LEFT * dp;
  595. var padR = CHART_PADDING_RIGHT * dp;
  596. var padT = CHART_PADDING_TOP * dp;
  597. var padB = CHART_PADDING_BOTTOM * dp;
  598. var plotW = cw - padL - padR;
  599. var plotH = ch - padT - padB;
  600. // colors based on theme
  601. var lineColor = isDark ? "#9090FF" : "#6060AA";
  602. var fillColor = isDark ? "rgba(144,144,255,0.15)" : "rgba(96,96,170,0.15)";
  603. var gridColor = isDark ? "#404040" : "#E0E0E0";
  604. var textColor = isDark ? "#A0A0A0" : "#808080";
  605. var lostColor = "#e74c3c";
  606. var thresholdColor = "rgba(231,76,60,0.6)";
  607. // determine visible time window
  608. var data = allPingData;
  609. var maxTime = 0;
  610. if (data.length > 0) maxTime = data[data.length - 1].t;
  611. var timeStart, timeEnd;
  612. if (maxTime <= VISIBLE_SECONDS) {
  613. timeStart = 0;
  614. timeEnd = Math.max(VISIBLE_SECONDS, maxTime);
  615. } else {
  616. timeEnd = maxTime;
  617. timeStart = maxTime - VISIBLE_SECONDS;
  618. }
  619. // binary search to find first visible data point
  620. var visStart = 0;
  621. var lo = 0,
  622. hi = data.length - 1;
  623. while (lo <= hi) {
  624. var mid = (lo + hi) >> 1;
  625. if (data[mid].t < timeStart) lo = mid + 1;
  626. else hi = mid - 1;
  627. }
  628. visStart = lo;
  629. // determine Y-axis scale from visible data only
  630. var yMax = 50; // minimum 50ms scale
  631. for (var i = visStart; i < data.length && data[i].t <= timeEnd; i++) {
  632. if (!data[i].lost && data[i].ping > yMax) yMax = data[i].ping;
  633. }
  634. yMax = Math.ceil((yMax * 1.2) / 10) * 10; // round up to nearest 10 with 20% headroom
  635. var fontSize = 11 * dp;
  636. ctx.font = fontSize + "px sans-serif";
  637. ctx.textBaseline = "middle";
  638. // grid lines - horizontal
  639. var ySteps = getYSteps(yMax);
  640. ctx.strokeStyle = gridColor;
  641. ctx.lineWidth = dp;
  642. ctx.fillStyle = textColor;
  643. ctx.textAlign = "right";
  644. for (var s = 0; s < ySteps.length; s++) {
  645. var yVal = ySteps[s];
  646. if (yVal > yMax) continue;
  647. var y = padT + plotH - (yVal / yMax) * plotH;
  648. ctx.beginPath();
  649. ctx.moveTo(padL, y);
  650. ctx.lineTo(padL + plotW, y);
  651. ctx.stroke();
  652. ctx.fillText(yVal + "", padL - 5 * dp, y);
  653. }
  654. // grid lines - vertical (time ticks)
  655. ctx.textAlign = "center";
  656. ctx.textBaseline = "top";
  657. var timeSpan = timeEnd - timeStart;
  658. var tickInterval = timeSpan <= 60 ? 10 : timeSpan <= 120 ? 15 : 30;
  659. var firstTick = Math.ceil(timeStart / tickInterval) * tickInterval;
  660. for (var t = firstTick; t <= timeEnd; t += tickInterval) {
  661. var x = padL + ((t - timeStart) / timeSpan) * plotW;
  662. ctx.beginPath();
  663. ctx.strokeStyle = gridColor;
  664. ctx.moveTo(x, padT);
  665. ctx.lineTo(x, padT + plotH);
  666. ctx.stroke();
  667. ctx.fillStyle = textColor;
  668. ctx.fillText(fmtTime(t), x, padT + plotH + 4 * dp);
  669. }
  670. // axis labels
  671. ctx.fillStyle = textColor;
  672. ctx.textAlign = "right";
  673. ctx.textBaseline = "top";
  674. ctx.fillText("ms", padL - 5 * dp, padT - 2 * dp);
  675. // plot border
  676. ctx.strokeStyle = gridColor;
  677. ctx.lineWidth = dp;
  678. ctx.strokeRect(padL, padT, plotW, plotH);
  679. // alert threshold line
  680. if (alertThresholdMs > 0 && alertThresholdMs <= yMax) {
  681. var threshY = padT + plotH - (alertThresholdMs / yMax) * plotH;
  682. ctx.save();
  683. ctx.setLineDash([6 * dp, 4 * dp]);
  684. ctx.strokeStyle = thresholdColor;
  685. ctx.lineWidth = 1.5 * dp;
  686. ctx.beginPath();
  687. ctx.moveTo(padL, threshY);
  688. ctx.lineTo(padL + plotW, threshY);
  689. ctx.stroke();
  690. ctx.restore();
  691. }
  692. if (data.length < 2) return;
  693. // filter visible data (using binary search start index)
  694. var visible = [];
  695. for (var i = visStart; i < data.length && data[i].t <= timeEnd; i++) {
  696. visible.push(data[i]);
  697. }
  698. if (visible.length < 1) return;
  699. // draw filled area
  700. ctx.beginPath();
  701. var started = false;
  702. for (var i = 0; i < visible.length; i++) {
  703. if (visible[i].lost) continue;
  704. var px = padL + ((visible[i].t - timeStart) / timeSpan) * plotW;
  705. var py = padT + plotH - (visible[i].ping / yMax) * plotH;
  706. if (!started) {
  707. ctx.moveTo(px, padT + plotH);
  708. ctx.lineTo(px, py);
  709. started = true;
  710. } else {
  711. ctx.lineTo(px, py);
  712. }
  713. }
  714. // close the fill path
  715. if (started) {
  716. // find last non-lost point
  717. for (var i = visible.length - 1; i >= 0; i--) {
  718. if (!visible[i].lost) {
  719. var px = padL + ((visible[i].t - timeStart) / timeSpan) * plotW;
  720. ctx.lineTo(px, padT + plotH);
  721. break;
  722. }
  723. }
  724. ctx.closePath();
  725. ctx.fillStyle = fillColor;
  726. ctx.fill();
  727. }
  728. // draw line
  729. ctx.beginPath();
  730. started = false;
  731. for (var i = 0; i < visible.length; i++) {
  732. if (visible[i].lost) {
  733. started = false;
  734. continue;
  735. }
  736. var px = padL + ((visible[i].t - timeStart) / timeSpan) * plotW;
  737. var py = padT + plotH - (visible[i].ping / yMax) * plotH;
  738. if (!started) {
  739. ctx.moveTo(px, py);
  740. started = true;
  741. } else {
  742. ctx.lineTo(px, py);
  743. }
  744. }
  745. ctx.strokeStyle = lineColor;
  746. ctx.lineWidth = 2 * dp;
  747. ctx.lineJoin = "round";
  748. ctx.stroke();
  749. // draw failed request markers
  750. for (var i = 0; i < visible.length; i++) {
  751. if (!visible[i].lost) continue;
  752. var px = padL + ((visible[i].t - timeStart) / timeSpan) * plotW;
  753. ctx.beginPath();
  754. ctx.arc(px, padT + plotH - 10 * dp, 3 * dp, 0, Math.PI * 2);
  755. ctx.fillStyle = lostColor;
  756. ctx.fill();
  757. }
  758. }
  759. function getYSteps(yMax) {
  760. // generate sensible Y-axis grid steps
  761. if (yMax <= 50) return [10, 20, 30, 40, 50];
  762. if (yMax <= 100) return [20, 40, 60, 80, 100];
  763. if (yMax <= 200) return [50, 100, 150, 200];
  764. if (yMax <= 500) return [100, 200, 300, 400, 500];
  765. // for very high values
  766. var step = Math.ceil(yMax / 5 / 100) * 100;
  767. var steps = [];
  768. for (var v = step; v <= yMax; v += step) steps.push(v);
  769. return steps;
  770. }
  771. // Animation frame loop
  772. window.requestAnimationFrame =
  773. window.requestAnimationFrame ||
  774. window.webkitRequestAnimationFrame ||
  775. window.mozRequestAnimationFrame ||
  776. window.msRequestAnimationFrame ||
  777. function (callback) {
  778. setTimeout(callback, 1000 / 60);
  779. };
  780. function frame() {
  781. requestAnimationFrame(frame);
  782. updateUI();
  783. drawChart();
  784. }
  785. frame();
  786. // Alert threshold
  787. function updateThreshold(val) {
  788. alertThresholdMs = parseInt(val);
  789. I("thresholdValue").textContent = alertThresholdMs > 0 ? alertThresholdMs + " ms" : "Off";
  790. }
  791. function playBeep() {
  792. var now = new Date().getTime();
  793. if (now - lastBeepTime < 1000) return; // debounce 1s
  794. lastBeepTime = now;
  795. try {
  796. if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  797. var osc = audioCtx.createOscillator();
  798. var gain = audioCtx.createGain();
  799. osc.frequency.value = 800;
  800. gain.gain.value = 0.3;
  801. osc.connect(gain);
  802. gain.connect(audioCtx.destination);
  803. osc.start();
  804. osc.stop(audioCtx.currentTime + 0.1);
  805. } catch (e) {}
  806. }
  807. // CSV download
  808. function downloadCsv() {
  809. if (allPingData.length === 0) return;
  810. var csv = "elapsed_s,ping_ms,failed\n";
  811. for (var i = 0; i < allPingData.length; i++) {
  812. var d = allPingData[i];
  813. csv += d.t.toFixed(3) + "," + d.ping.toFixed(2) + "," + (d.lost ? "1" : "0") + "\n";
  814. }
  815. var blob = new Blob([csv], { type: "text/csv" });
  816. var url = URL.createObjectURL(blob);
  817. var a = document.createElement("a");
  818. a.href = url;
  819. a.download = "stability_test_" + new Date().toISOString().slice(0, 19).replace(/:/g, "-") + ".csv";
  820. document.body.appendChild(a);
  821. a.click();
  822. document.body.removeChild(a);
  823. URL.revokeObjectURL(url);
  824. }
  825. // Init — load server list first (async), then initialize UI
  826. loadServers(function () {
  827. initServers();
  828. drawChart();
  829. });
  830. </script>
  831. </body>
  832. </html>