Created
January 8, 2014 14:34
-
-
Save dolzenko/8317682 to your computer and use it in GitHub Desktop.
Automatic benchmarking in IRB (based on irb_callbacks gem code)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
module IRB | |
def self.before_eval | |
end | |
def self.after_eval | |
end | |
def self.around_eval(&block) | |
@timing = Benchmark.measure do | |
block.call | |
end | |
end | |
def self.before_output | |
end | |
def self.after_output | |
puts "=> #{@timing.inspect}" | |
end | |
def self.around_output | |
yield | |
end | |
end | |
module IRB | |
class Irb | |
alias original_output_value output_value | |
# Add before_output, after_output, and around_output callbacks | |
# without damaging original behavior. | |
def output_value(*args, &block) | |
IRB::before_output | |
value = nil | |
IRB::around_output do | |
value = original_output_value(*args, &block) | |
end | |
IRB::after_output | |
value | |
end | |
end | |
class Context | |
alias original_evaluate evaluate | |
# Add before_eval, after_eval, and around_eval callbacks | |
# without damaging original behavior. | |
def evaluate(*args, &block) | |
IRB::before_eval | |
value = nil | |
IRB::around_eval do | |
value = original_evaluate(*args, &block) | |
end | |
IRB::after_eval | |
value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment