node.js 中的网速计算

问题描述 投票:0回答:2

如何计算服务器端node.js中的互联网速度,从客户端,我在查询字符串中发出http请求时发送当前时间戳。

客户端代码

  var image = document.createElement("img");
      image.width = 1;
      image.height = 1;
      image.src = url+"?"+new Date().getTime();

网址如下所示

  http://localhost:8080/server?1369654461767

互联网速度计算可以使用请求的开始时间、结束时间和可下载文件大小来完成。

但是,我的上述请求的问题是1x1像素,因此无法确定图像大小,并且我不希望单独的服务器调用来计算净速度。

使用客户端的开始时间以及请求和响应中存在的数据来计算互联网速度的任何解决方案。

任何想法都会有帮助。

javascript node.js express analytics web-analytics
2个回答
5
投票

您将无法计算服务器上的互联网速度。 一个关键问题是启动时间基于客户端时钟,而服务器时钟永远不会足够同步以用于计算速度。

要计算速度,您需要一个完整的往返,您可以从同一台机器(都在客户端上)准确捕获开始和结束时间。 您还需要发送比 1px 图像更重要的内容。 有效负载越小,计算越不准确。

通常速度是通过请求几个已知大小的大响应来计算的。


0
投票

要使用客户端在查询字符串中提供的时间戳来计算 Node.js 中服务器端的互联网速度,可以使用以下方法:

步骤:

  1. 在查询字符串中接收客户端的时间戳
  2. 收到请求时捕获服务器的时间戳
  3. 测量响应有效负载的大小(即使它是 1x1 像素图像或小数据)。
  4. 计算客户端启动时间与服务器响应时间之间的时间差
  5. 根据数据大小和时间差确定速度

这是一个示例实现:

客户端代码

var image = document.createElement("img");
image.width = 1;
image.height = 1;
image.src = "http://localhost:8080/server?start=" + new Date().getTime();

服务器端代码(Node.js)

const http = require("http");

const server = http.createServer((req, res) => {
    // Extract the start time from the query string
    const urlParams = new URL(req.url, `http://${req.headers.host}`);
    const clientStartTime = parseInt(urlParams.searchParams.get("start"), 10);

    // Check if the start time is provided
    if (!clientStartTime) {
        res.writeHead(400, { "Content-Type": "text/plain" });
        return res.end("Missing 'start' query parameter.");
    }

    // Get the server's current time (response start time)
    const serverStartTime = Date.now();

    // Create a small response payload (e.g., a 1x1 transparent pixel)
    const pixelData = Buffer.from([
        0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00,
        0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
        0xff, 0xff, 0xff, 0x21, 0xf9, 0x04, 0x01, 0x00,
        0x00, 0x01, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
        0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44,
        0x01, 0x00, 0x3b
    ]);

    // Measure the payload size
    const payloadSize = pixelData.length; // in bytes

    // Respond with the image
    res.writeHead(200, { "Content-Type": "image/gif" });
    res.end(pixelData, () => {
        // Calculate the total round-trip time
        const serverEndTime = Date.now();
        const totalTime = serverEndTime - clientStartTime; // in milliseconds

        // Calculate internet speed in Mbps
        const speedMbps = (payloadSize * 8) / (totalTime / 1000) / 1_000_000;

        console.log(`Payload Size: ${payloadSize} bytes`);
        console.log(`Total Time: ${totalTime} ms`);
        console.log(`Internet Speed: ${speedMbps.toFixed(2)} Mbps`);
    });
});

// Start the server
server.listen(8080, () => {
    console.log("Server running on http://localhost:8080/");
});

说明:

  1. 客户端:

    • 时间戳会附加到查询字符串 (
      ?start=<timestamp>
      )。
    • 这可确保请求是唯一的且不会被缓存。
  2. 服务器端:

    • start
      查询参数用于获取客户端的开始时间。
    • 服务器计算响应时间(
      serverEndTime - clientStartTime
      )。
    • 有效负载大小是固定的(例如,1x1 像素图像的大小)。
    • 使用速度公式:
      [ ext{速度(Mbps)} = rac{ ext{有效负载大小(位)}}{ ext{时间(秒)}} \div 1,000,000 ]
  3. 优点

    • 速度计算不需要额外的服务器调用。
    • 有效负载大小是预定义且已知的,即使对于 1x1 像素等小数据也是如此。
  4. 限制

    • 对于极小的有效负载,精度可能会有所不同。
    • 计算中包含网络延迟,因此速度反映的是往返时间。
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.