summaryrefslogtreecommitdiff
path: root/lib/irb.rb
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2020-08-15 06:36:24 +0900
committeraycabta <aycabta@gmail.com>2020-12-20 16:23:59 +0900
commit9f08e3c703795e81d333d568e7e44743022468f1 (patch)
treefe9a2ae21291561130f584ef6c2bb9f5700b53d1 /lib/irb.rb
parent8b6aaeaddf2d13833c0540490a84741035a3a808 (diff)
[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
Diffstat (limited to 'lib/irb.rb')
-rw-r--r--lib/irb.rb18
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/irb.rb b/lib/irb.rb
index 579fd67c67..26c5d2ebe9 100644
--- a/lib/irb.rb
+++ b/lib/irb.rb
@@ -538,7 +538,23 @@ module IRB
signal_status(:IN_EVAL) do
begin
line.untaint if RUBY_VERSION < '2.7'
- @context.evaluate(line, line_no, exception: exc)
+ if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty?
+ IRB.set_measure_callback
+ end
+ if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
+ result = nil
+ last_proc = proc{ result = @context.evaluate(line, line_no, exception: exc) }
+ IRB.conf[:MEASURE_CALLBACKS].map{ |s| s.last }.inject(last_proc) { |chain, item|
+ proc {
+ item.(@context, line, line_no, exception: exc) do
+ chain.call
+ end
+ }
+ }.call
+ @context.set_last_value(result)
+ else
+ @context.evaluate(line, line_no, exception: exc)
+ end
if @context.echo?
if assignment_expression?(line)
if @context.echo_on_assignment?