-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
net: TCPConn.Read Higher CPU usage than comparable operation in python3/c#/nodejs #53939
Comments
It sounds like an apples to oranges comparison. Although the programs do the same thing, they do not do it in the same way. For Python and synchronous C# , I think there is no multiplexing in your test (i.e. no event loop), just a thread waiting on a unique system call to read the socket. Go and Node.js probably generate more system calls, because they automatically multiplex the I/Os (with epoll). Since you measure with a single connection, it is a worst case for I/O multiplexing. Also the default configuration of the socket may be different from one language implementation to the other (Nagle algorithm, socket buffers, etc ...). You could count the number of system calls using strace -c and compare the results. |
Ok. I don't disagree with any of your remarks. I did run some trace results but it was casual and not for the exact same amount of time so the number of calls isn't directly comparable though the proportions (% time) should be fine. You're correct that nodejs and go appear to be using epoll and python3 and .net6 don't appear to be (from the strace results) for this scenario. I'm fine if you want to close the issue, I just thought the results might be worth further investigation as the cpu discrepancy was repeatable and somewhat large but if that's expected then that's that. Nodejs did use less CPU than go in my testing but that may very well be down to deliberate design choices. Go
python3
nodejs
net6
|
Duplicate of #52341 |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I was debugging higher than expected CPU usage for async socket operations in c#/net6 on linux and ran a series of socket tests in nodejs, python3, c, and go as a references. I noticed that go uses about twice as much cpu as python3/c/.net6 and somewhat more than nodejs in my synchronous read test case. It's not a bug but I thought I'd share the findings in case somebody is interested at looking at it. I suspect there is some optimization that can be done to bring the go results in line with the others. I'm not a go developer but the code and are test are very simple so I don't believe it's naivety on my part - though I apologize if it is. These tests are done with the default settings, I'm not adjusting any socket settings, OS settings, etc. Just plain socket reads into an oversized buffer.
From another host on the network I run netcat to saturate the network (in my case the receiving server is 192.168.1.93 and my tests were with a gigabit network)
cat /dev/zero | nc 192.168.1.93 9989
Then I run a receiving application such as the go application below to read the data and print out some details when I kill the netcat. I monitored the cpu usage with top and confirmed the network usage with iftop in addition to the application output below.
using netcat as a listener
nc -l 9989 >/dev/null
uses about 8% cpu
using golang with a goroutine per connection receiving into a buffer
./GoServer (attached zip, but it's super simple and has the code)
uses about 14% cpu
using nodejs and the socket.on data callback
nodejs jsserver.js
uses about 10% cpu (not re-using a receive buffer so it's less efficient)
using python3 with a synchronous tcp socket receiving into a buffer
python3 pyserver.py
uses about 8% cpu
using .net6 with a synchronous tcp socket receiving into a buffer
dotnet run --configuration Release PerfTestTCPListener
uses about 6-8% cpu
Code for python3, nodejs, c#/net6
OtherSamples.zip
Code for Go
GoServer.zip
What did you expect to see?
Similar cpu usage to python/.net6/nodejs
What did you see instead?
~14% cpu usage vs ~8% cpu usage for python3/net6 and ~10% cpu usage for nodejs
The text was updated successfully, but these errors were encountered: