1

I create 10000 get requests to example.com, and send them concurrently by using promise.

It take more than 1 minute.

If create 1 get request, it take only 500 ms.

Why do too many requests take so long time regardless of sending them concurrently?

I think 10000 requests should take 500ms as same as 1 request if it's completely concurrent.

It's not the cpu or memory problem because I tried multithread and memory increase, but the time is not changed. Even 10000 different urls, it's not changed. So this is client matter I guess.

Is there any other root cause?

How can I improve the time?

My code here.

const got = require("got");

console.time('total');
const urlList = Array(10000).fill("https://example.com");
const pList = urlList.map(function(url) {
    return got(url, {timeout: {request: 60000, }, retry: {limit: 0}}).catch((e) => {
        console.log(e);
        return null;
    });
});

Promise.all(pList).then((result) => {
    console.timeEnd('total');
}).catch((e) => {
    console.log('error: ' + e);
});

Thank you in advance!

gacchan
  • 21
  • 2
  • you can not achieve parallelism just via `promise.all` instead it uses concurreny, to achieve parallelism you should also use `cluster` module to get the number of cores and distrubte all the requests equally among the total number of cores in your machine. so this will give you different results as all the machines will not have same number of cores. – Vikash_Singh Nov 07 '22 at 10:50
  • please go through https://stackoverflow.com/questions/34855352/how-in-general-does-node-js-handle-10-000-concurrent-requests this will help you understand how nodejs handles concurrent requests. Please go through https://www.geeksforgeeks.org/how-to-run-many-parallel-http-requests-using-node-js/ this will help you to understand parallelism in nodejs. – Vikash_Singh Nov 07 '22 at 10:56
  • sorry, that's my english mistake. I understand difference between parallel and concurrent. And I read the article you share, I knew most of all things in there. There might not be the explanation of why each request takes longer time than when I sent only one request. But I really appreciate your reply. – gacchan Nov 07 '22 at 12:07
  • Also, I tried worker-thread, not cluster, but time is changed. – gacchan Nov 07 '22 at 12:10

0 Answers0