Measures the Time Elapsed by a Block
Here is how you can measure and log the time of a block:
require 'elapsed'
elapsed do
run_something_slow
end
You can also send the log message to a log, for example to the Loog:
require 'elapsed'
require 'loog'
elapsed(Loog::VERBOSE) do
# any code
end
You can also make the message custom:
elapsed(good: 'File saved') do
File.save(f, 'Hello, world!')
end
Or, you can make the message even more custom:
elapsed do
File.save(f, 'Hello, world!')
throw :"Successfully saved #{File.size(f)} bytes"
end
You can also filter out small durations using the over:
parameter:
elapsed(Loog::VERBOSE, over: 0.5) do
# This message will only be logged if the block takes more than 0.5 seconds
run_something_that_might_be_fast
end
That's it.
How to contribute
Read these guidelines. Make sure your build is green before you contribute your pull request. You will need to have Ruby 3.0+ and Bundler installed. Then:
bundle update
bundle exec rake
If it's clean and you don't see any error messages, submit your pull request.