From 9f08e3c703795e81d333d568e7e44743022468f1 Mon Sep 17 00:00:00 2001 From: aycabta Date: Sat, 15 Aug 2020 06:36:24 +0900 Subject: [ruby/irb] Add measure command You can use "measure" command to check performance in IRB like below: irb(main):001:0> 3 => 3 irb(main):002:0> measure TIME is added. => nil irb(main):003:0> 3 processing time: 0.000058s => 3 irb(main):004:0> measure :off => nil irb(main):005:0> 3 => 3 You can set "measure :on" by "IRB.conf[:MEASURE] = true" in .irbrc, and, also, set custom performance check method: IRB.conf[:MEASURE_PROC][:CUSTOM] = proc { |context, code, line_no, &block| time = Time.now result = block.() now = Time.now puts 'custom processing time: %fs' % (Time.now - time) if IRB.conf[:MEASURE] result } https://github.com/ruby/irb/commit/3899eaf2e2 --- lib/irb/cmd/measure.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib/irb/cmd/measure.rb (limited to 'lib/irb/cmd/measure.rb') diff --git a/lib/irb/cmd/measure.rb b/lib/irb/cmd/measure.rb new file mode 100644 index 0000000000..6161d15bcb --- /dev/null +++ b/lib/irb/cmd/measure.rb @@ -0,0 +1,34 @@ +require_relative "nop" + +# :stopdoc: +module IRB + module ExtendCommand + class Measure < Nop + def initialize(*args) + super(*args) + end + + def execute(type = nil, arg = nil) + case type + when :off + IRB.conf[:MEASURE] = nil + IRB.unset_measure_callback(arg) + when :list + IRB.conf[:MEASURE_CALLBACKS].each do |type_name, _| + puts "- #{type_name}" + end + when :on + IRB.conf[:MEASURE] = true + added = IRB.set_measure_callback(type) + puts "#{added.first} is added." + else + IRB.conf[:MEASURE] = true + added = IRB.set_measure_callback(type) + puts "#{added.first} is added." + end + nil + end + end + end +end +# :startdoc: -- cgit v1.2.3