Module: Benchmeth::ClassMethods

Defined in:
lib/benchmeth.rb

Instance Method Summary collapse

Instance Method Details

#benchmark(*method_names) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/benchmeth.rb', line 28

def benchmark(*method_names)
  method_names.each do |method_name|
    method_name = method_name.to_sym
    send :alias_method, :"#{method_name}_without_benchmark", method_name
    send :define_method, method_name do |*args, &block|
      method_prefix =
        case self
        when $benchmeth_main
          ""
        when Class
          "#{name}."
        else
          "#{self.class.name}#"
        end

      if Benchmeth.use_notifications
        payload = {
          name: "#{method_prefix}#{method_name}"
        }
        ActiveSupport::Notifications.instrument "benchmark.benchmeth", payload do
          send(:"#{method_name}_without_benchmark", *args, &block)
        end
      else
        start_time = Benchmeth.monotonic_time
        result = send(:"#{method_name}_without_benchmark", *args, &block)
        realtime = Benchmeth.monotonic_time - start_time
        Benchmeth.on_benchmark.call("#{method_prefix}#{method_name}", realtime)
        result
      end
    end
  end
end