chuckremes / ffi-rzmq

FFI bindings for ZeroMQ so the library can be used under JRuby and other FFI-compliant ruby runtimes
242 stars 62 forks source link

Performance #89

Closed mattconnolly closed 11 years ago

mattconnolly commented 11 years ago

I'm going through the Code Connected book, writing out the examples in ruby using this gem.

The Weather Update publisher is considerably slower than the C example, by a lot more than I would have expected. The C example takes roughly 4 seconds consistently. The ruby code takes over 2 minutes / about 30x slower.

Is this to be expected?

chuckremes commented 11 years ago

Put a copy of the code into a gist so I can run it. Slower by 30x seems a bit high.

Sent from my iPhone

On Apr 24, 2013, at 8:29 AM, Matt Connolly notifications@github.com wrote:

I'm going through the Code Connected book, writing out the examples in ruby using this gem.

The Weather Update publisher is considerably slower than the C example, by a lot more than I would have expected. The C example takes roughly 4 seconds consistently. The ruby code takes over 2 minutes / about 30x slower.

Is this to be expected?

— Reply to this email directly or view it on GitHub.

mattconnolly commented 11 years ago

I've used the example code from the "zguide" repo:

https://github.com/imatix/zguide/blob/master/examples/Ruby/wuserver.rb

I've commented out the "puts" line in case that made a difference. Still takes about 2 minutes. Same time for both the wuclient.c and wuclient.rb files. So the limiting factor has to be the wuserver code.

I added some benchmarking to the wuserver.rb and equivalent wuserver.c programs to see:

C:

Time to send 10000 updates: 0.003 Time to send 10000 updates: 0.004 Time to send 10000 updates: 0.004 Time to send 10000 updates: 0.004 Time to send 10000 updates: 0.004 Time to send 10000 updates: 0.004 Time to send 10000 updates: 0.003 Time to send 10000 updates: 0.004 Time to send 10000 updates: 0.004

Ruby:

Time to send 10000 updates: 0.121969 Time to send 10000 updates: 0.122294 Time to send 10000 updates: 0.127774 Time to send 10000 updates: 0.12535 Time to send 10000 updates: 0.121435 Time to send 10000 updates: 0.120299 Time to send 10000 updates: 0.128274 Time to send 10000 updates: 0.123823

Commenting out the send line, I get:

Time to send 10000 updates: 0.022921 Time to send 10000 updates: 0.023188 Time to send 10000 updates: 0.022353 Time to send 10000 updates: 0.021565

So it seems that the send part is taking roughly 0.1 of the 0.12 seconds to send 10000 messages on the socket.

mostlyobvious commented 11 years ago

Regarding performance of zmq on ruby, see Martin's comment: http://www.youtube.com/watch?feature=player_detailpage&v=RcfT3b79UYM#t=1355s

What ruby version are you using? I'm curious how disabling GC would improve that time.

mostlyobvious commented 11 years ago

Also it'd be interesting to see how https://github.com/methodmissing/rbczmq compares.

mattconnolly commented 11 years ago

I'm using ruby-2.0.0-p0.

I'll try check out the rbczmq later... it doesn't build due a compatibility issue with homebrew on my mac.

mattconnolly commented 11 years ago

I found a fork with a fairly recent branch that works with Homebrew.

Time to send 10000 updates: 0.040435 Time to send 10000 updates: 0.043302 Time to send 10000 updates: 0.042057 Time to send 10000 updates: 0.044484 Time to send 10000 updates: 0.041047 Time to send 10000 updates: 0.044977

It's about 3x faster than this gem, taking about 35-39 seconds for the wuserver.rb and wuclient.rb examples. (Only 10x slower than the C examples). That library is also quite a bit more ruby-like in its interface.

chuckremes commented 11 years ago

Yes ffi-rzmq is a very thin Ruby layer over a C API so it isn't very ruby-like. The intention is for someone to write a wrapper library around it to provide all of the nice Ruby idioms that we enjoy.