I’m trying to do a simple benchmark to know how many octets are read from the cache in each page of my Rails site. I’m talking about the data retrieve from Rails.cache.
I would like to display something like 123Ko/145Ko at the bottom of my pages.
Does a gem exist to perform this task or perhaps is there something included in the ruby standard library?
One option is to subclass the store you’re using and extend the protected
read_entrymethod declared inActiveSupport::Cache::Store, whichFileStoreand the other caches themselves subclass.When starting a page, you can call
start_pageto zero out the octet count. Sinceread_entryis a low-level method used every time the cache tries to perform a read, you can intercept any data read and get its size before returning it. You might have to convertsizeto octets.To set this as your custom cache store, add
config.cache_store = FileStoreWithReadTracking.new('/path/to/file/store')to your environment. I think you can subclass all the stores this way.