class Ezmetrics::Benchmark

Attributes

durations[R]
intervals[R]
iterations[R]
redis[R]
start[R]
store_each_value[R]

Public Class Methods

new(store_each_value=false) click to toggle source
# File lib/ezmetrics/benchmark.rb, line 5
def initialize(store_each_value=false)
  @store_each_value = store_each_value
  @start            = Time.now.to_i
  @redis            = Redis.new(driver: :hiredis)
  @durations        = []
  @iterations       = 1
  @intervals        = {
    "1.minute" => 60,
    "1.hour  " => 3600,
    "12.hours" => 43200,
    "24.hours" => 86400,
    "48.hours" => 172800
  }
end

Public Instance Methods

measure_aggregation(partition_by=nil) click to toggle source
# File lib/ezmetrics/benchmark.rb, line 20
def measure_aggregation(partition_by=nil)
  write_metrics
  print_header
  intervals.each do |interval, seconds|
    result = measure_aggregation_time(interval, seconds, partition_by)
    print_row(result)
  end
  cleanup_metrics
  print_footer
end

Private Instance Methods

cleanup_metrics() click to toggle source
# File lib/ezmetrics/benchmark.rb, line 69
def cleanup_metrics
  interval_start = Time.now.to_i - intervals.values.max - 100
  interval_keys  = (interval_start..Time.now.to_i).to_a
  redis.del(interval_keys)
end
measure_aggregation_time(interval, seconds, partition_by) click to toggle source
# File lib/ezmetrics/benchmark.rb, line 75
def measure_aggregation_time(interval, seconds, partition_by)
  iterations.times do
    durations << ::Benchmark.measure do
      ezmetrics = Ezmetrics::Storage.new(seconds)
      if store_each_value
        partition_by ? ezmetrics.partition_by(partition_by).show(db: :percentile_90) : ezmetrics.show(db: :percentile_90)
      else
        partition_by ? ezmetrics.partition_by(partition_by).show : ezmetrics.show
      end
    end.real
  end

  return {
    interval: interval.gsub(".", " "),
    duration: (durations.sum.to_f / iterations).round(2)
  }
end
print_header() click to toggle source
print_row(result) click to toggle source
write_metrics() click to toggle source
# File lib/ezmetrics/benchmark.rb, line 35
def write_metrics
  seconds = intervals.values.max
  seconds.times do |i|
    second = start - i
    payload = {
      "second"       => second,
      "duration_sum" => rand(10000),
      "duration_max" => rand(10000),
      "views_sum"    => rand(1000),
      "views_max"    => rand(1000),
      "db_sum"       => rand(8000),
      "db_max"       => rand(8000),
      "queries_sum"  => rand(100),
      "queries_max"  => rand(100),
      "2xx"          => rand(1..10),
      "3xx"          => rand(1..10),
      "4xx"          => rand(1..10),
      "5xx"          => rand(1..10),
      "all"          => rand(1..40)
    }

    if store_each_value
      payload.merge!(
        "duration_values" => Array.new(100) { rand(10..60000) },
        "views_values"    => Array.new(100) { rand(10..60000) },
        "db_values"       => Array.new(100) { rand(10..60000) },
        "queries_values"  => Array.new(10)  { rand(1..60) }
      )
    end
    redis.setex(second, seconds, Oj.dump(payload.values))
  end
  nil
end