r/rust • • Sep 20 '24

Why is Rust slower than Python, Go and Nim regarding SSH?

For my test, I run 'date +\"%T.%N\"' twice on a SSH Server on my local network and calculate the time between the two executions.

Clients run on Windows10/11.

Python (paramiko): ~45ms
Nim (ssh2(libssh2)): ~44ms
Go (golang.org/x/crypto/ssh): ~4ms
Rust (ssh-rs, russh, ssh2(libssh2)): ~115-130ms

I don't know what Go's trick is, but 45ms seems like a goal Rust should easily achieve!?

Rust profile for "cargo build -r":
[profile.release]
lto = true
codegen-units = 1
strip = true
panic = 'abort'

Rust (ssh-rs): https://pastebin.com/3ucPpCzc
Rust (ssh2(libssh2)): https://pastebin.com/cQ7j6zd2
Nim (ssh2(libssh2)): https://pastebin.com/xRYFFLDN
Go (golang.org/x/crypto/ssh): https://pastebin.com/mFzvLLnk

Any idea why this happens and how I can at least get the same performance as the other implementations?

Update:

The previous tests were executed on Windows 10 (Threadripper 3990X with Mellanox card, highly optimized for latency) and Standard Windows 11 Enterprise (work notebook).

I copied the projects to a raspberry pi and retested the Rust ssh-rs, ssh2 implementations and the Go app against the same destination.

ssh-rs: ~45ms
ssh2: ~45ms
go: ~4ms

5 Upvotes

30 comments sorted by

78

u/ralphpotato Sep 20 '24

Ok I tested using your ssh-rs code and your go code, and I briefly looked at the source code in go. Here's my output with a bit of timing around the ssh commands to my home server on the same network:

Rust:

Elapsed time: 54.22ms
03:15:20.209671266

Elapsed time: 53.72ms
03:15:20.263840329

Go:

2024/09/20 03:16:32 Elapsed 24.922917ms
Output: 03:16:32.648934309

2024/09/20 03:16:32 Elapsed 10.858125ms
Output: 03:16:32.659267186

So the rust is definitely slower, but I think the main thing is that the go code must be running concurrently while the rust code is completely synchronous. Basically the rust code sends the command over the session, waits for the output, parses the output, prints the output, and then does the same thing for the next command. The go code ends up running both at the same time, and the total turnaround time also seems to be faster.

I am not super familiar with nim and you didn't add your python code so I can't compare with that. If you ran the rust code concurrently using an async runtime like tokio, you probably would see a date diff close to go.

8

u/psx8888 Sep 20 '24

Thanks for your effort!

I was totally okay with ssh-rs running slower because it's pretty new and not battle testet like ssh2.
It was easy to use, therefore I went with it for a few projects. But it annoyed me that other implementations were faster. Therefore I tried the same with the SSH2 crate and its bindings to libssh2. And this lib takes 45ms in Nim and pure C (havent found my old C project i made some time ago). So I think ssh-rs isn't really compareable 1:1, but libssh2 is. And that's why I dont understand the big difference, it's the same library.

21

u/ralphpotato Sep 20 '24

Ok checking the total execution time of the rust ssh2 and go programs, they're both about the same time for me at about ~450ms-480ms. It makes me wonder if the timing in the go program isn't actually capturing the same amount of what I'm attempting to time in rust.

4

u/psx8888 Sep 20 '24

That's possible. With goroutines I was able to reduce the time for the go program even further. But that wouldn't be a fair test.

I like Rust very much and use it at home and at work since 1 year for mostly everything we need (ssh, grpc, ldap, ...)
After I saw some grpc benchmarks I began to wonder If its even worth to code in Rust when at least some use cases are very hard to optimize to reach standard go performance levels. I mean you dont even have to messure the time, execute the binary on the console and you can see how snappy some go programs execute. Even with the GC baggage. I dont want to leave Rust land, but I really need a solution for ssh, otherwise I have to use both, depending on the specific use case and it's measured performance. Rust isn't slow, I'm far away to accept that, thats why I opened this post. Maybe I'm missing something. And I would be happy if I did.

3

u/ralphpotato Sep 20 '24

Check the comment I made here: https://www.reddit.com/r/rust/comments/1fl7ym9/why_is_rust_slower_than_python_go_and_nim/lo1847h/

I think depending on what you're doing, they're going to end up being the same speed because I think these benchmarks are somehow not measuring things correctly. Ultimately if you do need to use multiple ssh sessions, you will probably need to use an async runtime in rust- synchronously blocking on network requests is definitely going to slow down a bigger project.

2

u/psx8888 Sep 20 '24

Thanks again for your time and effort.

The previous tests were executed on Windows 10 (Threadripper 3990X with Mellanox card, highly optimized for latency) and Standard Windows 11 Enterprise (work notebook).

I copied the projects to a raspberry pi and retested the Rust ssh-rs, ssh2 implementations and the Go app against the same destination.

ssh-rs: ~45ms
ssh2: ~45ms
go: ~4ms

It seems that Windows has minor latency issues regardless of OS version or network card configuration.

Go shines nevertheless, but my faith in Rust is restored^^ At least on linux! (-;

1

u/ralphpotato Sep 20 '24

Interesting it’s much slower on windows. But still I will say the go time reported is that fast because both commands are being executed concurrently, so they reach the destination machine at about the same time. This isn’t the same as saying the entire ssh operation happened within 4ms.

1

u/psx8888 Sep 20 '24

Use sync.WaitGroup in the Go implementation and it will blow your mind. I'm not sure how to interpret this and if this is even correct.
I used Go's ssh implementation for a terminal project. It sadly wasn't stable at all.
I trust Rust's results more because it is similar to libssh2's output in other implementations. But I think there is much more room for performance improvments on the Rust SSH side.

1

u/ralphpotato Sep 20 '24

What do you mean by use sync.WaitGroup?

2

u/psx8888 Sep 20 '24

Sorry, I meant the combination with Goroutines:

https://pastebin.com/8RdEDvFc

→ More replies (0)

31

u/ralphpotato Sep 20 '24

Here's an output from /usr/bin/time for rust:

real 0.46
user 0.05
sys 0.02
             5390336  maximum resident set size
                   0  average shared memory size
                   0  average unshared data size
                   0  average unshared stack size
                3498  page reclaims
                  31  page faults
                   0  swaps
                   0  block input operations
                   0  block output operations
                  14  messages sent
                  32  messages received
                   0  signals received
                   1  voluntary context switches
                 115  involuntary context switches
           446324001  instructions retired
           165570480  cycles elapsed
             2147328  peak memory footprint

And for go:

real 0.45
user 0.19
sys 0.34
            92635136  maximum resident set size
                   0  average shared memory size
                   0  average unshared data size
                   0  average unshared stack size
               26683  page reclaims
                 212  page faults
                   0  swaps
                   0  block input operations
                   0  block output operations
                  15  messages sent
                  33  messages received
                 460  signals received
                 431  voluntary context switches
                6059  involuntary context switches
           914880134  instructions retired
           925112470  cycles elapsed
            17204992  peak memory footprint

The wall clock time for both of them is nearly identical, but interestingly the rust program has much lower user and sys times, while the go program is higher for both. This along with the other information basically tells me that the rust program is effectively doing the same thing as the go program, but to run go's concurrent runtime requires a lot more context switching and operations. The rust program I'm guessing just blocks and waits for the kernel or network card to wake it up (not sure about the technical details of how ssh is implemented with regards to this point) whenever the response from the ssh connection comes back.

Given that the total wall-clock time is the same, and is clearly dominated by the cost of doing the network requests, I think the timings within the rust and go programs are probably not that representative of what's really happening.

1

u/manipulater Sep 20 '24

I wonder how much the sys and user time would increase if tokio was used. Even though the response time is not improved, reduced sys and user times mean that same code written in rust should produce the same throughput with lesser resources than the go one.

4

u/ralphpotato Sep 20 '24

I tried the same thing using the russh crate but the example in the crate reads an ssh private key and more argument parsing which adds some overhead. I didn’t spend the time trimming it down for a fair comparison.

In theory yes rust could potentially scale more but I think the output is just evidence that the go runtime is doing stuff, and I’m not actually sure that go’s runtime overhead will scale poorly with more tasks. What we’re seeing might be more or less just a constant overhead. Both solutions ultimately have to block on a response form the network, and go’s runtime probably just wakes up every few ms to see if it has any other tasks to schedule. I think if you’re running into scaling issues with a go application over ssh, you’re either an evil genius building something crazy or you’re trying to do too much over ssh.

1

u/manipulater Sep 20 '24

I agree this doesn't provide evidence that go's overhead will not remain the same. Comparing this to an tokio app would be much better. Further, GC, I am not sure how it works in go, however I am assuming it has to keep track of each variable if it is alive or not and remove the dead values. In comparison in rust this is handled by borrow checker for stack values for some heap values, it is borrow checker, for some there would be some runtime overhead using Arc/Mutex/Locks. In some applications you can even use Atomics which would reduce this number further.

This would be interesting to measure how much is the cost of GC rt of go vs tokio rust with no GC.

1

u/ralphpotato Sep 20 '24

I tried your ssh2 rust code and the difference in date outputs for me was 54ms, so more in line with what is expected. I'm still surprised how fast the go runs.

9

u/crusoe Sep 20 '24

How are you launching and timing the program?

If you are running with cargo run after compiling you are also measuring the start time of cargo itself.

Try running the binary directly it's under target/release.

3

u/dddd0 Sep 20 '24

Could be that ssh-rs uses unoptimized „pure rust“ crypto implementations.

2

u/psx8888 Sep 20 '24

That can certainly be the case!

I think the comparison between libssh2 in Rust and other programming languages ​​such as C and Nim is more meaningful. And that's exactly the point that concerns me.

16

u/joshuamck ratatui Sep 20 '24

When in doubt about a perf issue, prefer measuring over speculating. Bump up the server log verbosity and look at the difference in call patterns. Maybe the libs have different encryption defaults? Maybe there's an extra roundtrip or two happening somewhere. The logs are where I'd start looking for that info.

7

u/Sloppyjoeman Sep 20 '24

I suspect this is an example of async rust being relatively difficult to write:

  • c is ancient and so has a battle tested, optimised library

  • go’s async features are relatively easy to use, and so a given library is more likely to take advantage of its async capabilities

  • rust is relatively new with relatively complex async primitives, and so is less likely to have these optimisations in

1

u/Individual_Caramel93 Mar 14 '25

Nagle's algorithm strikes again. Golang (smartly?) disables it by default. Enabling tcp_nodelay for your Nim snippet makes the time diff 3ms.

-1

u/eras Sep 20 '24

Did you check CPU usage? If it takes 100% cpu, then did you also use release mode when compiling?

I mean it stands to reason everyone would do benchmarking in release mode, but it also stands to reason that if it were so, one would also mention it, as it is always the first response :).

2

u/psx8888 Sep 20 '24

That's why I wrote this in my post. Or did I misunderstand your comment?

Rust profile for "cargo build -r":
[profile.release]
lto = true
codegen-units = 1
strip = true
panic = 'abort'

4

u/heavymetalpanda Sep 20 '24

Are you doing cargo build -r and then cargo run? How are you running the resulting binary?

4

u/psx8888 Sep 20 '24 edited Sep 20 '24

The tests are build in release mode:

cargo build -r and then ./target/release/xyz.exe
and when i do use "cargo run" I add "-r"

Trust me, it's in release mode. There is also no significant cpu usage. That's not my first project and all tests are stripped down for simplicity, therefore no error handling etc.

[profile.release] is also working, otherwise the binary wouldn't be stripped regarding size.

4

u/eras Sep 20 '24

There is also no significant cpu usage

Then it's not about codegen, but about network behaviour. Perhaps the ssh-rs does more roundtrips than required.

Does the delay decrease "too much" if you test against localhost?

You could also compare wireshark between those different clients. No deep insight required, just the amount of packets.

3

u/crusoe Sep 20 '24

When you do cargo run -r cargo has to start and fork the ssh process. Since the program is built it is not rebuilt.  But you're time the startup time of cargo as well.