Skip to content

Instantly share code, notes, and snippets.

@dolzenko
Created January 8, 2014 14:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolzenko/8317682 to your computer and use it in GitHub Desktop.
Save dolzenko/8317682 to your computer and use it in GitHub Desktop.
Automatic benchmarking in IRB (based on irb_callbacks gem code)
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