Sha256: 57dfae2daf7792305692725304e3a1d211d661ee1e49994c065224413d5a02f5

Contents?: true

Size: 1.58 KB

Versions: 17

Compression:

Stored size: 1.58 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: current_timestamp,
      }
    end

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

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

    def self.current_timestamp
      Process.clock_gettime(Process::CLOCK_MONOTONIC)
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-7.0.0.beta25 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta24 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta23 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta22 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta21 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta20 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta19 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta18 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta17 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta16 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta15 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta14 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta13 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta12 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta11 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta10 lib/sidekiq_unique_jobs/profiler.rb
sidekiq-unique-jobs-7.0.0.beta9 lib/sidekiq_unique_jobs/profiler.rb