【问题标题】:How to get job execution time from sidekiq?如何从 sidekiq 获取作业执行时间?
【发布时间】:2017-07-07 16:47:02
【问题描述】:

我想知道每次运行作业时的执行时间。似乎有一个 gem sidekiq-statistic 可以做我想要的,甚至有 GUI,但不幸的是它不适用于 sidekiq 5。我可以从 perform 方法中以某种方式获取此类信息吗?我宁愿避免手动编写基准测试代码,将我的方法包装到 Benchmark 块中

【问题讨论】:

    标签: ruby-on-rails sidekiq rails-activejob


    【解决方案1】:

    你可以从日志输出中抓取它。

    【讨论】:

      【解决方案2】:

      您可以在 perform 方法中进行计算,然后将这些计算记录到您的 Rails 日志中。

      def preform(task,*params)
         ...start metric times...
         ... do your computations ...
         ...end metric times...
         Rails.logger.info "Your Metrics..."
      end
      

      我在 heroku 上使用 Paper-trail 取得了成功,只需查看工作人员的工作开始和完成时间。

      【讨论】:

        【解决方案3】:

        作为 Mike says,它已经在日志中。

        快速查看工人昨天的平均完成时间:

        rg MyWorker sidekiq.log.1 |rg done| awk '{ total += $8; count++ } END { print total/count }'
        

        【讨论】:

        • rg 对我们 n00bs 来说是什么?
        • rgripgrep
        【解决方案4】:

        我有问题要找出哪个任务花费的时间最长,所以我写了一个小任务来分析日志 好消息:我找到了,所以我分享我的代码 :-)

        希望它没有任何错误

          desc "sidekiq run statistics"
          task sidekiq_run_statistics: :environment do
            # 2021-02-19T06:04:23.088Z 20240 TID-ov75i75aw Importer::PriceJob JID-95245ccf3933fe1f5897d52b INFO: done: 193.952 sec
            file = File.open( Rails.root.join("log","sidekiq.log"))
            job_stats = {}
            file.each_line do |line|
              job_stat = line.split(" ")
              if job_stat[8] == "sec"
                time = job_stat[7].to_f
                job_stats[ job_stat[3] ] ||= {longest_run_time: 0, total_run_time:0, total_runs: 0 }
                job_stats[ job_stat[3] ][:longest_run_time] = time if  job_stats[ job_stat[3] ][:longest_run_time] < time 
                job_stats[ job_stat[3] ][:total_run_time]+=time 
                job_stats[ job_stat[3] ][:total_runs]+=1 
              end 
            end
            file.close
            puts %w(total_runs total_run_time longest_run_time job ).collect{|i| i.rjust(16) }.join(" ")
            puts "="*80
               
            job_stats.keys.each do |key| 
              puts( [job_stats[key][:total_runs],job_stats[key][:total_run_time].round(3), job_stats[key][:longest_run_time].round(3), key ].collect{|i| i.to_s.rjust(16) }.join(" "))
            end 
          end 
        

        样本输出

        total_runs       total_run_time   longest_run_time job             
        ==================================================================
        56               68.909           7.641            Job1
        56               98.9             11.599           Job2
        47               193.909          13.795           Job3
        234              1806.76          20.644           Job4
        

        【讨论】:

          【解决方案5】:

          你可以试试这个Scout,这是一个很棒的工具,可以提供详细的方法指标。它也适用于 Heroku,他们在那里有附加组件。

          【讨论】:

            猜你喜欢
            • 2016-07-13
            • 1970-01-01
            • 2013-10-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-07-25
            • 2015-04-27
            相关资源
            最近更新 更多