2

Connection to localhost (web app based on express/node.js) during unit testing (mocha/supertest) on my laptop is slow compared to another equivalent machine.

  • Same hardware
  • Windows 7 x64
  • Similar network configuration (results from ipconfig, netsh interface ip show config, netsh interface ip show dns, ipconfig /displaydns...)
  • Same node.js/npm configuration (node.js v8.9.1 and npm 5.5.1)

I have already tried a few things:

  • Disable IPv6 (it is disable on both machines)
  • Add localhost into the hosts file
  • Flush DNS history (ipconfig /flushdns)
  • Disconnecting my ethernet cable (I have no wifi either. Tests are passing but are still slow)
  • Change from 'localhost' to '127.0.0.1' in the test
  • Delete all my npm dependencies and re-install them (in project folder, in C:\Users\myUser\AppData\Roaming\npm and C:\Users\myUser\AppData\Roaming\npm-cache, in C:\Users\myUser\AppData\Local\Microsoft\Typescript and C:\Users\myUser\AppData\Local\Temp
  • Reinstall node.js/npm/vs code/typescript
  • Update npm and node.js to the latest version

It is also worth mentioning that:

  • the delay is always about 3s
  • the delay is between the time I send the request and the time I receive it (cf. logs below)
  • the problem only happens with localhost. Connection to remote APIs is as fast as on the other laptop
  • the loading time of the node-modules is fast
  • no event loop latency (checked with event-loop-lag)
  • there is no delay if I start the application and try to access the api with a browser
  • there is no delay if I start the application and run the test with url specified (not using 'app'). I added the following code in the app: if(!module.parent) { app.listen(3000); }
  • if I send 2 tests to localhost/127.0.0.1 and port 3000, the 2nd test is fast (cf. below)

Some related posts I have read without success:
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

I have created a dummy project to reproduce the problem.
app.js file:

'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 file:

'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');
  });

});

The result is as follows:

> 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)

Thank you very much for your help

D3nsk
  • 131
  • 2
  • 13

0 Answers0