Sha256: 78d75b0449582ee8234891cb1ea17f1e14793f39e3fb365d218238d19db10cc4

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

require 'benchmark'

class PerformanceStats < Struct.new(:backend)

  GET_ACCESS_TIMES = 10_000
  SET_ACCESS_TIMES = 10_000
  CREATE_AND_DELETE_ACCESS_TIMES = 10_000

  def average_access
    key = generate_key
    backend.set(key, rand)

    time = Benchmark.measure{ GET_ACCESS_TIMES.times{ backend.get(key) } }
    backend.del(key)

    time.real / GET_ACCESS_TIMES
  end

  def average_write
    key = generate_key
    time = Benchmark.measure{ SET_ACCESS_TIMES.times{ backend.set(key, rand) } }
    backend.del(key)

    time.real / SET_ACCESS_TIMES
  end

  def average_create_and_delete
    key = generate_key
    time = Benchmark.measure do
      CREATE_AND_DELETE_ACCESS_TIMES.times do
        backend.set(key, rand)
        backend.del(key)
      end
    end

    time.real / CREATE_AND_DELETE_ACCESS_TIMES
  end

  def result
    { average_access: format_result_in_ms(average_access),
      average_write: format_result_in_ms(average_write),
      average_create_and_delete: format_result_in_ms(average_create_and_delete)}
  end

  private
  def generate_key
    rand.to_s
  end

  def format_result_in_ms(result)
    "#{format('%.3f', result.to_f * 1000)} ms"
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redis_monitor-0.3.3 lib/engine/app/lib/commands/performance_stats.rb
redis_monitor-0.3.2 lib/engine/app/lib/commands/performance_stats.rb
redis_monitor-0.3 lib/engine/app/lib/commands/performance_stats.rb
redis_monitor-0.2.1 lib/engine/app/lib/commands/performance_stats.rb