module CapybaraTestHelpers::BenchmarkHelpers
Public: Keeps track of the running time for user-defined helpers, useful as a way to keep track of the executed methods, and to easily spot slow operations.
Protected Instance Methods
benchmark_method(method_name, args, kwargs) { || ... }
click to toggle source
Internal: Helper to benchmark an operation, outputs the method name, its arguments, and the ellapsed time in milliseconds.
# File lib/capybara_test_helpers/benchmark_helpers.rb, line 23 def benchmark_method(method_name, args, kwargs) @@indented_logs.push(log = +'') # Push it in order, set the content later. @@indentation_level += 1 before = Time.now yield ensure diff_in_millis = (Time.now - before).in_milliseconds.round @@indentation_level -= 1 # Set the queued message with the method call and the ellapsed time. log.sub!('', _benchmark_str(method_name: method_name, args: args, kwargs: kwargs, time: diff_in_millis)) # Print the messages once we outdent all, and clear the queue. @@indented_logs.each { |inner_log| Kernel.puts(inner_log) }.clear if @@indentation_level.zero? end
Private Instance Methods
_benchmark_str(method_name:, args:, kwargs:, time:)
click to toggle source
Internal: Indents nested method calls, and adds color to make it readable.
# File lib/capybara_test_helpers/benchmark_helpers.rb, line 42 def _benchmark_str(method_name:, args:, kwargs:, time:) args += [kwargs] unless kwargs.empty? args_str = args.map(&:inspect) [ ' ' * @@indentation_level, Rainbow(self.class.name.chomp('TestHelper') + '#').slategray.rjust(40), Rainbow(method_name.to_s).cyan, Rainbow("(#{ args_str.join(', ') })").slategray, ' ', Rainbow("#{ time } ms").send(time > 1000 && :red || time > 100 && :yellow || :green), ].join('') end