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

The server use more cpu after I switch from golang 1.11 net.http to fasthttp #549

Closed
bronze1man opened this issue Mar 2, 2019 · 4 comments

Comments

@bronze1man
Copy link

bronze1man commented Mar 2, 2019

The server use more cpu after I switch from golang 1.11 net.http to fasthttp.

I have a process the act as reverseProxy, it is behind cloudflare cdn.
I want to use fasthttp to reduce cpu usage on my server.
The golang 1.11 net.http use 70% cpu, more then 1GB memory.
The fasthttp use 379% cpu, 401.13MB memory.
It looks like fasthttp use less memory, and use more cpu.

As the fasthttp do not support http/2. And cloudflare support it.
I doubt the http/2 and golang 1.11 net.http use less cpu than fasthttp with http/1.1 under the same load.

May relative to #144

Cpu usage Profile:

  • With fasthttp the first cpu usage come from tls stuff.
  • With golang 1.11 net.http the first cpu usage come from alloc stuff.

Alloc profile (I use debug.allocfreetrace to profile the alloc):

  • With fasthttp first alloc usage come from net.conn and tls stuff.
  • With golang 1.11 net.http first alloc usage come from the header map of http.Request

I use default fasthttp.Server{} setting. I only set handler to it. So the http/1.1 keep alive is enabled.

@kirillDanshin
Copy link
Collaborator

hi @bronze1man,
First of all, what do you use for TLS: RSA or ECDSA? RSA is pretty slow and ineffective, so I suggest checking this first.
Also, usually fasthttp trying to get things done as soon as possible, so it seemed like higher CPU usage when it is basically doing the same but made without waiting for next memory allocation.

net/http usually allocates more and spends more time waiting for allocation, so it spreads the load over time. fasthttp skips that wait time because it tries to reuse as much memory as possible.

@erikdubbelboer
Copy link
Collaborator

How many requests per second? Are you using keep-alive connections?

@bronze1man
Copy link
Author

bronze1man commented Mar 5, 2019

How many requests per second?

I do not measure that.

Are you using keep-alive connections?

Yes, in both cases(fasthttp and net/http).

Also, usually fasthttp trying to get things done as soon as possible, so it seemed like higher CPU usage when it is basically doing the same but made without waiting for next memory allocation.

I do not measure the requests per second when I measure the cpu usage. Maybe fasthttp process more requests than net/http. Thanks, that is a good idea to try.

@erikdubbelboer
Copy link
Collaborator

If with fasthttp most CPU usage goes into TLS I'm suspecting that keep-alive connections aren't being used. Setting up a TLS connections uses a lot of CPU, but once it's setup it's very fast. Can you somehow verify that it's really using keep-alive?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants