Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

axios vs undici with ProxyAgent #3915

Open
yerlantemir opened this issue Dec 2, 2024 · 7 comments
Open

axios vs undici with ProxyAgent #3915

yerlantemir opened this issue Dec 2, 2024 · 7 comments
Labels
enhancement New feature or request Performance features or fixes related to performance

Comments

@yerlantemir
Copy link

yerlantemir commented Dec 2, 2024

Hi team,

First of all, thank you for amazing work than you have done so far! I hope this bug is happening because of my misuse and would love to contribute if there are some problems with the library.

Bug Description

When using undici with ProxyAgent, request performance is significantly slower compared to axios with https-proxy-agent for parallel requests through a proxy server.

Reproducible By

  1. Setup proxy server
  2. Send 1000 requests to the 3rd party service parallelly using axios and undici with ProxyAgent
  3. Compare the results

Expected Behavior

Replacing axios with undici should maintain or improve performance.

Logs & Screenshots

-------------------
`Results for axios: {
  Concurrency: 10,
  'Avg Duration': '465ms',
  'Total Time': '111s'
}
-------------------
Results for undici: {
  Concurrency: 10,
  'Avg Duration': '604ms',
  'Total Time': '145s'
}

You can find the test script here: https://jumpshare.com/v/0ol3h5KZePayCSY46GY1?b=DJylk9UBBfNgJiz93jg4

Environment

MacOs Sonoma 14.3
node v20.13.0

Additional context

We are running a service that sends ~1000s of requests each minute to the third party service through proxy server. Currently, we are using axios with https-proxy-agent and are exploring undici for potential performance improvements and the ability to add retry logic. Could you please help identify what might be causing this issue? Is there a specific way undici should be configured in this case, or could this be related to a bottleneck in the library?

@yerlantemir yerlantemir added the bug Something isn't working label Dec 2, 2024
@mcollina
Copy link
Member

mcollina commented Dec 2, 2024

I don't think the proxy agent was every optimized, thanks for the call. Can you help assembling a benchmark everyone can reproduce? Otherwise it would be really hard to optimize.


Note that your undici call has a significant memory leak. You are not consuming the body, preventing the sockets to be reused. You should try:

    const proxyAgent = getProxyAgentFromUndiciPool();
    const response = await request(URL, {
      dispatcher: proxyAgent,
      method: 'POST',
      blocking: true,
      upgrade: false,
      reset: false,
      body: JSON.stringify(BODY),
      headers: HEADERS,
      throwOnError: true
    });
    await response.body.dump() // or response.dump().catch(() => {})
    const { statusCode } = response;

@mcollina mcollina added enhancement New feature or request Performance features or fixes related to performance and removed bug Something isn't working labels Dec 2, 2024
@yerlantemir
Copy link
Author

@mcollina thanks for quick response! I created a reproducible environment for this issue, you can find a code here: yerlantemir@9820786.

With the following setup, I am constantly getting worse results with undici
CleanShot 2024-12-02 at 15 41 25@2x

Would love to hear your thoughts on this

@PandaWorker
Copy link

PandaWorker commented Dec 2, 2024

you have an error with axios in the last example, because of this, it sends requests through the default agent, and not the one specified

you also have a restriction on agent connections, but requests are still sent one at a time and not immediately to all possible connections

const makeAxiosRequest = async () => {
  await axios.get(serverUrl + '/hello?foo=bar', {
    httpsAgent: axiosHttpsProxyAgent,
    httpAgent: axiosHttpsProxyAgent // +++
  });
}
Results for 10000 requests
axios 2461 ms
undici 2456 ms

@PandaWorker
Copy link

If you use all 100 agent connections, then undici will be 2 times faster

@PandaWorker
Copy link

Try this

https://gist.github.com/PandaWorker/8b95c10f156f4bbc997555d0a1505cab

@mcollina
Copy link
Member

mcollina commented Dec 2, 2024

Thanks @PandaWorker!

@yerlantemir
Copy link
Author

@PandaWorker Thanks for your help. I couldn't make 2x faster, but at least it's not losing to axios on performance.

On the production environment, axios is still a little bit faster(10-20%). Currently I am trying to replicate this environment and find out the issues, will be back if I find something useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Performance features or fixes related to performance
Projects
None yet
Development

No branches or pull requests

3 participants