File size: 1,837 Bytes
e8a6fe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const express = require('express');
const fs = require('fs');

const app = express();
const PORT = 7860;

app.all('/', async (req, res) => {
  res.send(`<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speed Test</title>
</head>
<body>
    <h1>Speed Test Download</h1>
    <button onclick="startTest()">Mulai Tes</button>
    <p id="result"></p>

    <script>
        async function startTest() {
            const url = "/static/100MB.bin";
            const startTime = performance.now();
            const response = await fetch(url);
            const reader = response.body.getReader();
            let received = 0;

            while (true) {
                const { done, value } = await reader.read();
                if (done) break;
                received += value.length;
            }

            const endTime = performance.now();
            const duration = (endTime - startTime) / 1000;
            const speedBps = (received * 8) / duration;
            let speed, unit;

            if (speedBps >= 1e12) {
                speed = speedBps / 1e12;
                unit = "Tbps";
            } else if (speedBps >= 1e9) {
                speed = speedBps / 1e9;
                unit = "Gbps";
            } else if (speedBps >= 1e6) {
                speed = speedBps / 1e6;
                unit = "Mbps";
            } else {
                speed = speedBps / 1e3;
                unit = "Kbps";
            }

            document.getElementById("result").textContent = \`Kecepatan: \${speed.toFixed(2)} \${unit}\`;
        }
    </script>
</body>
</html>`) });

app.use('/static', express.static('static'));

app.listen(PORT, () => console.log(`Server running at http://localhost:${PORT}/static/100MB.bin`));