节点http.get模块:是套接字开放事件,更准确地考虑在计算http请求的响应时间时将其视为开始时间

发布于 2025-02-07 00:41:56 字数 779 浏览 6 评论 0原文

我正在使用节点HTTP模块并行测试100个端点。这些端点中的每一个都来自组织内的不同端点(不同的域),我想计算它们的TCP连接时间和总响应时间。 请在下面找到我的代码。由于节点JS正在单个事件循环中处理所有这些端点,因此我知道时间将通过多个请求共享。但是,在我启动计时器的时间(请参阅选项1)与节点正在处理其他回调的连接的实际打开之间是否存在差距。我想知道是否应该在打开插座后开始时间(选项2),因为这是HTTP生命周期中的第一步,端点响应时间可能从那里开始。 请建议。我正在尝试获得每个端点的最准确时间。

// **Option 1:** Start time before http.get (my current code)
    var startTime = new Date();
    var req = http.get(endpoint);
    req.on('socket', socket => {
            // **Option 2:** Start time when socket is open (Is this more accurate?)
            var startTime = new Date()
            socket.on('connect', () => {
                var tcpConnectionAt = new Date();
                console.log("TCP connection time: " + (tcpConnectionAt-startTime));
            });
        });

I am using node http module for testing 100 endpoints in parallel. Each of these endpoints are from different endpoints within my organization (different domains) and I want to calculate tcp connection time and total response time for them.
Please find my code below. Since node JS is processing all these endpoints in a single event loop, I understand that time will be shared by multiple requests. However, Is there a gap between the time at which I start timer (See Option 1) and actual opening of connection where node is processing other callbacks. I am wondering if I should start time once socket is open (Option 2) because that is the first step in HTTP lifecycle and endpoint response time should probably start from there.
Please advice. I am trying to get the most accurate timings of each of the endpoints.

// **Option 1:** Start time before http.get (my current code)
    var startTime = new Date();
    var req = http.get(endpoint);
    req.on('socket', socket => {
            // **Option 2:** Start time when socket is open (Is this more accurate?)
            var startTime = new Date()
            socket.on('connect', () => {
                var tcpConnectionAt = new Date();
                console.log("TCP connection time: " + (tcpConnectionAt-startTime));
            });
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文