Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/query_diet/logger.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'benchmark'

module QueryDiet
class Logger
DEFAULT_OPTIONS = { :bad_count => 8, :bad_time => 5000 }
Expand All @@ -16,9 +14,9 @@ def log(query)
yield
else
result = nil
time = Benchmark.realtime do
result = yield
end
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = yield
time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
queries << [query, time] if log_query?(query)
result
end
Expand Down
8 changes: 5 additions & 3 deletions spec/query_diet/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
end

def mock_realtime(seconds)
Benchmark.should_receive(:realtime).at_least(:once).and_wrap_original do |original_method, *args, &block|
original_method.call(*args, &block)
seconds
call_count = 0
Process.should_receive(:clock_gettime).with(Process::CLOCK_MONOTONIC).at_least(:twice).and_wrap_original do
result = call_count.even? ? (call_count / 2) * seconds : ((call_count / 2) + 1) * seconds
call_count += 1
result
end
end

Expand Down