0

This is a bit odd and I am not sure what is really going on, but http requests have varying delays that sometimes become huge. I time it as,

  process.time = new Date().getTime();
    console.log('voice ' + voice)

    var req = http.get(config.host_ip + ":" + config.host_port + "/sendSpeech/" + voice, function (response) {
      response.on('data', function (d) {
            console.log("TOTAL TIME " + (new Date().getTime() - process.time))
        });

    }).on('error', function (e) {
        // Call callback function with the error object which comes from the request

    });
    req.end();

Sometimes, total time is sub second and other times it is upwards of 6 seconds. I timed the process that actually runs the function it is sending it to and it operates at less than 100 millisecond speeds.

If I do the call via the browser from the server it stands up, the call is often sub second. Significantly faster than calling it from a separate node application.

Does anyone know what is going on? The most connections that it handles is about 6-10 so I don't think the workload is especially heavy, although it runs on a raspberry pi.

Edit: I am using Node 5.4.1 .

Edit 2: I just tried switching to websockets via socket.io and the speed is about the same. Meaning I think the issue is network related.

Mitchell Ingram
  • 686
  • 2
  • 11
  • 23
  • Probably not enough information. You need to narrow down which component is causing the issue. If it's never slow from a browser on the same machine, then it sounds like the requesting node or the network might be the culprit. – AlexMA Jan 18 '16 at 19:50
  • Yeah, I switched to web sockets and it was just as slow, so I think it is a networking issue. – Mitchell Ingram Jan 18 '16 at 19:51

1 Answers1

0

In node 0.10 and earlier, the HTTP Agent will only open 5 simultaneous connections to a single host by default. You can change this easily: (assuming you've required the HTTP module as http)

You can increase number of sockets by setting maxSockets

http.globalAgent.maxSockets = 20; // or whatever

Read more at https://stackoverflow.com/a/12061013/789377

Side note

It may not effect the performance but you are using http.get like http.request . Http.get returns callback with response data. Use it like

process.time = new Date().getTime();
console.log('voice ' + voice)

var url = config.host_ip + ":" + config.host_port + "/sendSpeech/" + voice;
http.get(url, function(res) {
  console.log("TOTAL TIME " + (new Date().getTime() - process.time))
}).on('error', function(e) {
    // Call callback function with the error object which comes from the request
});

// you don't need req.end since http.get automatically calls req.end
Community
  • 1
  • 1
Maxali
  • 1,934
  • 2
  • 16
  • 25