与本地(Windows 7)上运行的Node.js应用程序的连接缓慢

时间:2018-08-06 04:05:56

标签: node.js windows performance connection localhost

在笔记本电脑上进行单元测试(mocha / supertest)期间,与本地主机(基于express / node.js的Web应用程序)的连接速度比另一台等效计算机慢。

  • 相同的硬件
  • Windows 7 x64
  • 类似的网络配置(来自ipconfignetsh interface ip show confignetsh interface ip show dnsipconfig /displaydns ...的结果)
  • 相同的node.js / npm配置(node.js v8.9.1和npm 5.5.1)

我已经尝试了一些方法:

  • 禁用IPv6(在两台计算机上均禁用)
  • 将localhost添加到hosts文件中
  • 刷新DNS历史记录(ipconfig /flushdns
  • 断开我的以太网电缆的连接(我也没有wifi。测试通过了,但仍然很慢)
  • 在测试中从“ localhost”更改为“ 127.0.0.1”
  • 删除所有我的npm依赖项,然后重新安装它们(在项目文件夹中,C:\Users\myUser\AppData\Roaming\npmC:\Users\myUser\AppData\Roaming\npm-cache中,C:\Users\myUser\AppData\Local\Microsoft\TypescriptC:\Users\myUser\AppData\Local\Temp
  • 中)
  • 重新安装node.js / npm / vs code / typescript
  • 将npm和node.js更新到最新版本

还值得一提的是:

  • 延迟总是大约3s
  • 延迟是在我发送请求到我收到请求之间(下面的 cf。日志)
  • 该问题仅发生在localhost上。连接远程API的速度与另一台笔记本电脑一样快
  • 节点模块的加载时间很快
  • 没有事件循环延迟(已通过event-loop-lag检查)
  • 如果我启动应用程序并尝试使用浏览器访问api,则没有延迟
  • 如果我启动应用程序并使用指定的url(不使用“ app”)运行测试,则没有延迟。我在应用程序中添加了以下代码:if(!module.parent) { app.listen(3000); }
  • 如果我将2个测试发送到localhost / 127.0.0.1和端口3000,则第二个测试很快(下面的 cf。

我读过的一些相关文章没有成功:
Firefox and Chrome slow on localhost; known fix doesn't work on Windows 7
How to figure out why my local host site takes so long to load?
Running sites on "localhost" is extremely slow
https://technet.microsoft.com/en-us/library/bb727023.aspx

我创建了一个虚拟项目来重现该问题。
app.js个文件:

'use strict';

const express = require('express');
const router = express.Router();


const app = express();

router.get('/api/v1/home', (req, res, _next) => {
  console.log('home api - request received - ', new Date().toUTCString());
  res.sendStatus(200);
});
app.use(router);

app.use(function (req, res) {
  res.status(404).send();
});

app.use((err, req, res, next) => {
  res.status(err.status || 500);
  console.log(err.message);
});

app.listen(3000);


module.exports = app;

app.test.js文件:

'use strict';

const assert = require('assert');
const rp = require('request-promise');
const app = require('./app');
const supertest = require('supertest');


describe('', function () {

  it('1st mocha test run fast', async function () {
    const res = await new Promise((resolve, _reject) => {
      setTimeout(() => {
        resolve('ok');
      }, 100);
    });
    assert.equal(res, 'ok');
  });

  it('calling app api with supertest', async function () {
    console.log('home api supertest - send request - ', new Date().toUTCString());
    await supertest(app).get('/api/v1/home').expect(200);
  });

  it('calling app api with request-promise (127.0.0.1)', async function () {
    console.log('home api request-promise - send request - ', new Date().toUTCString());
    const res = await rp('http://127.0.0.1:3000/api/v1/home')
      .then(function (_htmlString) {
          return 'ok'; 
      })
      .catch(function (err) {
          console.log(err);
          return 'nok';
      });
    assert.equal(res, 'ok');
  });

  it('calling app api with request-promise (localhost)', async function () {
    console.log('home api request-promise - send request - ', new Date().toUTCString());
    const res = await rp('http://localhost:3000/api/v1/home')
      .then(function (_htmlString) {
          return 'ok'; 
      })
      .catch(function (err) {
          console.log(err);
          return 'nok';
      });
    assert.equal(res, 'ok');
  });

});

结果如下:

> mocha ./api.test.js --exit --timeout 5000

    √ 1st mocha test run fast (101ms)
home api supertest - send request -  Mon, 06 Aug 2018 02:47:23 GMT
home api - request received -  Mon, 06 Aug 2018 02:47:26 GMT
    √ calling app api with supertest (3020ms)
home api request-promise - send request -  Mon, 06 Aug 2018 02:47:26 GMT
home api - request received -  Mon, 06 Aug 2018 02:47:29 GMT
    √ calling app api with request-promise (127.0.0.1) (3011ms)
home api request-promise - send request -  Mon, 06 Aug 2018 02:47:29 GMT
home api - request received -  Mon, 06 Aug 2018 02:47:29 GMT
    √ calling app api with request-promise (localhost)

  4 passing (6s)

非常感谢您的帮助

0 个答案:

没有答案