Shopify / statsd-instrument

A StatsD client for Ruby apps. Provides metaprogramming methods to inject StatsD instrumentation into your code.
http://shopify.github.io/statsd-instrument
MIT License
570 stars 94 forks source link

Benchmark Ruby 3.2 SizedQueue #315

Closed casperisfine closed 2 years ago

casperisfine commented 2 years ago

This is not meant to be merged.

Warming up --------------------------------------
    thread condition     7.005k i/100ms
SizedQueue#pop(count:)
                         9.392k i/100ms
SizedQueue#pop(nonblock)
                         9.609k i/100ms
Calculating -------------------------------------
    thread condition     67.229k (± 8.7%) i/s -    336.240k in   5.041282s
SizedQueue#pop(count:)
                         93.481k (± 2.4%) i/s -    469.600k in   5.026409s
SizedQueue#pop(nonblock)
                         95.093k (±14.4%) i/s -    393.969k in   5.035037s

Comparison:
    thread condition:    67229.3 i/s
SizedQueue#pop(nonblock):    95092.5 i/s - 1.41x  (± 0.00) faster
SizedQueue#pop(count:):    93480.8 i/s - 1.39x  (± 0.00) faster

The buffer size if the default 5k, it means pop(nonblock) is called 5k times, but somehow it's not noticeably slower than a single pop(count: 5000) call, which I must say is surprising.

I profiled both, and SizedQueue#pop accounts for 0.31% and 0.08% of runtime respectively. So pop(count: 5000) is much faster as expected, but it doesn't matter much in this benchmark.

Most of the time is spent building the datagrams and sending the UDP packets

Capture d’écran 2022-08-19 à 17 36 06

So even without pop(count:), the overhead of backgrounding sending the events has been pretty much been eliminated.

We should definitely use SizedQueue for Ruby 3.2+.