Sha256: a7a14917e7994383c9f1ee4884d76e159b4069351d8b36666f629137ddff9e2f

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

require 'sinatra/assetpack'
require 'chartkick'

module Sidekiq
  module Benchmark
    module Web
      def self.registered(app)
        web_dir = File.expand_path("../../../web", __FILE__)
        js_dir = File.join(web_dir, "assets", "javascripts")

        app.helpers Chartkick::Helper
        app.register Sinatra::AssetPack

        app.assets {
          serve '/js', from: js_dir

          js 'chartkick', ['/js/chartkick.js']
        }

        app.get "/benchmarks" do
          @charts = {}

          Sidekiq.redis do |conn|
            @types = conn.smembers "benchmark:types"
            @types.each do |type|
              @charts[type] = { total: [], stats: [] }

              total_keys = conn.hkeys("benchmark:#{type}:total") -
                ['start_time', 'job_time', 'finish_time']

              total_time = conn.hget "benchmark:#{type}:total", :job_time
              total_time = total_time.to_f
              total_keys.each do |key|
                value = conn.hget "benchmark:#{type}:total", key
                @charts[type][:total] << [key, value.to_f.round(2)]
              end

              stats = conn.hgetall "benchmark:#{type}:stats"
              stats.each do |key, value|
                @charts[type][:stats] << [key.to_f, value.to_i]
              end

              @charts[type][:stats].sort! { |a, b| a[0] <=> b[0] }
              @charts[type][:stats].map! { |a| [a[0].to_s, a[1]] }
            end
          end

          view_path = File.join(web_dir, "views", "benchmarks.slim")
          template = File.read view_path
          render :slim, template
        end

        app.post "/benchmarks/remove" do
          Sidekiq.redis do |conn|
            keys = conn.keys "benchmark:*"
            conn.del keys
          end

          redirect "#{root_path}benchmarks"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidekiq-benchmark-0.2.0 lib/sidekiq-benchmark/web.rb
sidekiq-benchmark-0.1.2 lib/sidekiq-benchmark/web.rb