Sha256: eaf962a4d1c5f934d03a1ff58027ef40e86e52b1f3b7a64ada6464b6f74c7b78

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  #
  # Class MethodProfiler provides method level profiling
  #
  # @author Mikael Henriksson <mikael@zoolutions.se>
  #
  class Profiler
    def self.patch(klass, methods, name) # rubocop:disable Metrics/MethodLength
      patches = methods.map do |method_name|
        <<~RUBY
          unless defined?(#{method_name}__mp_unpatched)
            alias_method :#{method_name}__mp_unpatched, :#{method_name}
            def #{method_name}(*args, &blk)
              unless prof = Thread.current[:_method_profiler]
                return #{method_name}__mp_unpatched(*args, &blk)
              end
              begin
                start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
                #{method_name}__mp_unpatched(*args, &blk)
              ensure
                data = (prof[:#{name}] ||= {duration: 0.0, calls: 0})
                data[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
                data[:calls] += 1
              end
            end
          end
        RUBY
      end.join("\n")

      klass.class_eval patches
    end

    def self.start
      Thread.current[:_method_profiler] = {
        __start: Process.clock_gettime(Process::CLOCK_MONOTONIC),
      }
    end

    def self.stop
      finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      return unless (data = Thread.current[:_method_profiler])

      Thread.current[:_method_profiler] = nil
      start = data.delete(:__start)
      data[:total_duration] = finish - start
      data
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-7.0.0.beta8 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta7 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta6 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta5 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta4 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta3 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta2 lib/sidekiq_unique_jobs/profiler.rb