class Dbtap::PerformsWithin

Tests if a query performs within a certain period of time.

Each query is run ten times and

This is useful for benchmarking, and will verify that a query ran neither too slow nor too fast.

Attributes

expected_time[RW]

Public Class Methods

new(query, expected_time, delta, name = nil) click to toggle source

query - The query to benchmark expected_time - The average expected time to run the query once (in milliseconds) delta - The amount of time + or - that the query is allowed to deviate (in milliseconds) name - (optional) the name of the test

# File lib/dbtap/testers/performs_within.rb, line 17
def initialize(query, expected_time, delta, name = nil)
  @name = name
  @query = query
  @expected_time = expected_time
  @delta = delta
end

Public Instance Methods

errors() click to toggle source
# File lib/dbtap/testers/performs_within.rb, line 28
def errors
  output = []
  if time_diff < 0
    output << "too slow by factor of #{time_factor}"
  else
    output << "too fast by factor of #{time_factor}"
  end
  output << "average runtime: #{elapsed_time} ms"
  output << "desired average: #{expected_time} +/- #{delta} ms"
end
ok?() click to toggle source
# File lib/dbtap/testers/performs_within.rb, line 24
def ok?
  time_diff.abs.to_f <= delta
end

Private Instance Methods

bench_it() click to toggle source
# File lib/dbtap/testers/performs_within.rb, line 44
def bench_it
  Benchmark.realtime do
    repetitions.times do
      query.count
    end
  end
end
elapsed_time() click to toggle source
# File lib/dbtap/testers/performs_within.rb, line 40
def elapsed_time
  @elapsed_time ||= bench_it / repetitions.to_f
end
repetitions() click to toggle source
# File lib/dbtap/testers/performs_within.rb, line 52
def repetitions
  10
end
time_diff() click to toggle source
# File lib/dbtap/testers/performs_within.rb, line 56
def time_diff
  expected_time - elapsed_time
end
time_factor() click to toggle source
# File lib/dbtap/testers/performs_within.rb, line 64
def time_factor
  return time_proportion if time_proportion > 1
  return (1 / time_proportion)
end
time_proportion() click to toggle source
# File lib/dbtap/testers/performs_within.rb, line 60
def time_proportion
  elapsed_time.to_f / expected_time
end