Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ruby function to track the time elapsed by the execution of a block and print it to the log

License

Notifications You must be signed in to change notification settings

yegor256/elapsed

Repository files navigation

Measures the Time Elapsed by a Block

DevOps By Rultor.com We recommend RubyMine

rake PDD status Gem Version Test Coverage Yard Docs Hits-of-Code License

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

You can change the level of logging (use Logger::INFO, Logger::DEBUG, or Logger::ERROR), while the default one is DEBUG:

elapsed(Loog::VERBOSE, level: Logger::DEBUG) do
  # The procedure
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.

About

Ruby function to track the time elapsed by the execution of a block and print it to the log

Topics

Resources

License

Stars

Watchers

Forks

Contributors 5

Languages