Sha256: 1899191b6f60aa5a4361b0d20f969e36418db47d8924d756dcd63154a5cac2fe

Contents?: true

Size: 754 Bytes

Versions: 1

Compression:

Stored size: 754 Bytes

Contents

module Servel::Instrumentation
  def instrument(*method_names)
    return unless ENV["DEBUG"] == "1"

    Array(method_names).each do |method_name|
      original_method_name = "#{method_name}_without_instrumentation"

      alias_method original_method_name, method_name

      cumulative_time_spent = 0

      define_method(method_name) do |*args|
        start = Time.now

        return_value = __send__(original_method_name, *args)

        finish = Time.now
        time_spent = ((finish - start) * 1000.0)
        cumulative_time_spent += time_spent
        puts "Running #{self.class.name}<#{object_id}>##{method_name}: #{time_spent.round(3)}ms (cumulative: #{cumulative_time_spent.round(3)}ms)"

        return_value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
servel-0.14.0 lib/servel/instrumentation.rb