Skip to content

Commit

Permalink
[ruby/irb] Support arg for measure command
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Dec 22, 2020
1 parent 47b2679 commit 4131cd0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
5 changes: 3 additions & 2 deletions lib/irb.rb
Expand Up @@ -544,9 +544,10 @@ def eval_input
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|
IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) { |chain, item|
_name, callback, arg = item
proc {
item.(@context, line, line_no, exception: exc) do
callback.(@context, line, line_no, arg, exception: exc) do
chain.call
end
}
Expand Down
12 changes: 6 additions & 6 deletions lib/irb/cmd/measure.rb
Expand Up @@ -14,17 +14,17 @@ def execute(type = nil, arg = nil)
IRB.conf[:MEASURE] = nil
IRB.unset_measure_callback(arg)
when :list
IRB.conf[:MEASURE_CALLBACKS].each do |type_name, _|
puts "- #{type_name}"
IRB.conf[:MEASURE_CALLBACKS].each do |type_name, _, arg|
puts "- #{type_name}" + (arg ? "(#{arg.inspect})" : '')
end
when :on
IRB.conf[:MEASURE] = true
added = IRB.set_measure_callback(type)
puts "#{added.first} is added."
added = IRB.set_measure_callback(type, arg)
puts "#{added[0]} is added."
else
IRB.conf[:MEASURE] = true
added = IRB.set_measure_callback(type)
puts "#{added.first} is added."
added = IRB.set_measure_callback(type, arg)
puts "#{added[0]} is added."
end
nil
end
Expand Down
14 changes: 7 additions & 7 deletions lib/irb/init.rb
Expand Up @@ -120,7 +120,7 @@ def IRB.init_config(ap_path)
puts 'processing time: %fs' % (now - time) if IRB.conf[:MEASURE]
result
}
@CONF[:MEASURE_PROC][:STACKPROF] = proc { |context, code, line_no, &block|
@CONF[:MEASURE_PROC][:STACKPROF] = proc { |context, code, line_no, arg, &block|
success = false
begin
require 'stackprof'
Expand All @@ -130,7 +130,7 @@ def IRB.init_config(ap_path)
end
if success
result = nil
stackprof_result = StackProf.run(mode: :cpu) do
stackprof_result = StackProf.run(mode: arg ? arg : :cpu) do
result = block.()
end
StackProf::Report.new(stackprof_result).print_text if IRB.conf[:MEASURE]
Expand All @@ -146,17 +146,17 @@ def IRB.init_config(ap_path)
@CONF[:AT_EXIT] = []
end

def IRB.set_measure_callback(type = nil)
def IRB.set_measure_callback(type = nil, arg = nil)
added = nil
if type
type_sym = type.upcase.to_sym
if IRB.conf[:MEASURE_PROC][type_sym]
added = [type_sym, IRB.conf[:MEASURE_PROC][type_sym]]
added = [type_sym, IRB.conf[:MEASURE_PROC][type_sym], arg]
end
elsif IRB.conf[:MEASURE_PROC][:CUSTOM]
added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM]]
added = [:CUSTOM, IRB.conf[:MEASURE_PROC][:CUSTOM], arg]
else
added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME]]
added = [:TIME, IRB.conf[:MEASURE_PROC][:TIME], arg]
end
IRB.conf[:MEASURE_CALLBACKS] << added if added
added
Expand All @@ -167,7 +167,7 @@ def IRB.unset_measure_callback(type = nil)
IRB.conf[:MEASURE_CALLBACKS].clear
else
type_sym = type.upcase.to_sym
IRB.conf[:MEASURE_CALLBACKS].reject!{ |t, c| t == type_sym }
IRB.conf[:MEASURE_CALLBACKS].reject!{ |t, | t == type_sym }
end
end

Expand Down

0 comments on commit 4131cd0

Please sign in to comment.