Skip to content

Why node.js server takes longer for each new set of requests #1179

@ORESoftware

Description

@ORESoftware

This regards Node.js versions 9+

I have a simple Node.js Express server running, I issued this command history:

round 1:

$ node server.js

$ node /Users/alexamil/perf.test.js
passed in these millis: 3405
$ node /Users/alexamil/perf.test.js
passed in these millis: 4010
$ node /Users/alexamil/perf.test.js
passed in these millis: 5290
$ node /Users/alexamil/perf.test.js
passed in these millis: 21170

round 2:

$ node server.js

$ node /Users/alexamil/perf.test.js
passed in these millis: 4407
$ node /Users/alexamil/perf.test.js
passed in these millis: 4865
$ node /Users/alexamil/perf.test.js
passed in these millis: 4874
$ node /Users/alexamil/perf.test.js
passed in these millis: 20808

I am just curious what the explanation is for the server taking more time, even after finishing all requests. For example, I run perf.test.js, it finishes all requests. Then I manually run the same command again, about 1 or 2 seconds after the previous finished. Yet the response time still increases. We also see a big increase upon the fourth command.

The express server is like so:

'use strict';

import {ErrorRequestHandler} from "express";
import * as express from 'express';
import haven from 'domain-haven';

const app = express();

app.use(function (req, res, next) {
  req.havenData = JSON.parse(req.query.haven || '{}');
  next();
});

if (process.env.use_haven === 'yes') {
  console.log('using haven');
  app.use(haven());
}


app.use(function(req,res,next){
  res.json({success: true});
});

app.use(function (err, req, res, next) {
  err && console.error(err.message || err);
  if (!res.headersSent) {
    setTimeout(function () {
      if (!res.headersSent) {
        res.json({error: 'hit final error middleware', value: err && err.stack || err || null})
      }
    }, 10);
  }
  
});

app.listen(6969, function () {
  console.log('app is listening.');
});

the test code is like so:

import http = require('http');
import async = require('async');
import request = require('request');
import {HavenData} from "./simple-server";
import assert = require('assert');
import util = require('util');

const tasks = Array.apply(null, Array(5000)).map(function () {
  return function (cb: Function) {
    
    const opts = {
      json: true,
      qs: {haven: null as any}
    };
    
    request.get('http://localhost:6969', opts, function (err, resp, v) {
      
      if (err) {
        return cb(err);
      }
      
      cb(null);
    });
    
  }
});

const now = Date.now();

async.parallelLimit(tasks, 15, function (err) {
  if (err) throw err;
  console.log('passed in these millis:', Date.now() - now);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions