anycable / graphql-anycable

A drop-in replacement for GraphQL ActionCable subscriptions. Works with AnyCable.
MIT License
112 stars 18 forks source link

feat: add ability to fetch subscription stats #37

Closed prog-supdex closed 1 year ago

prog-supdex commented 1 year ago

This PR adds an ability to call GraphQL::AnyCable.stats, which will return the subscription statistics.

For calculating specific existing keys in the Redis store, we are using scan_each with the argument count

The default count value in scan_each is 10. But this value is not suitable. So, I ran a benchmark and saw that value 1000 would be more suitable for it

scan_each COUNT 10     13.623727   3.093805  16.717532 ( 20.424414)
scan_each COUNT 100     4.232060   0.300462   4.532522 (  6.501772)
scan_each COUNT 1000    5.194357   0.058570   5.252927 (  6.935496)
scan_each COUNT 10000   8.821205   0.023022   8.844227 ( 10.486925)

The Redis store, before the benchmark, was filled with 500_000 records

Also, if we use the default value 10, it will make a lot of calls to Redis

For calculating subscribes by subscriptions, I decided to each by 100 elements in the group_subscription_stats method

prog-supdex commented 1 year ago

I ran a benchmark for whole statistics (with mode include_subscriptions: true) with these numbers {"total":{"subscription":49000,"fingerprints":7002,"subscriptions":49002,"channel":49000} the module Stats had the next results

                                                                           user     system       total              real
only total amount                                        2.239396   0.087184   2.326580 (  2.945300)
total amount with grouped subscriptions    8.423945   1.860734  10.284679 ( 12.442578)

Here we see that full Stats took 12 seconds. Only total numbers (without grouping by subscriptions) took 3 seconds

Also, I changed the count argument for group_subscription_stats because the 1000 count showed a better result (but it was not a significant difference)

                                                                         user       system      total              real
COUNT 10 only grouped                           6.492952   1.814657   8.307609 (  9.801935)
COUNT 100 only grouped                         5.703477   1.721931   7.425408 (  8.784324)
COUNT 1000 only grouped                       5.324353   1.691193   7.015546 (  8.279561)
COUNT 10000 only grouped                     5.684741   1.895940   7.580681 (  8.973217)