-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add time tracking to http status agent (#1517)
* Add time tracking to http status agent * Replace time tracking to monotonic * Add tests for time tracker
- Loading branch information
Showing
4 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class TimeTracker | ||
attr_accessor :elapsed_time, :result | ||
|
||
def self.track | ||
start = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
result = yield | ||
new(Process.clock_gettime(Process::CLOCK_MONOTONIC) - start, result) | ||
end | ||
|
||
def initialize(elapsed_time, result) | ||
@elapsed_time = elapsed_time | ||
@result = result | ||
end | ||
|
||
def method_missing(method_sym, *arguments, &block) | ||
if @result.respond_to?(method_sym) | ||
@result.send(method_sym, *arguments, &block) | ||
else | ||
super | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'rails_helper' | ||
|
||
describe TimeTracker do | ||
describe "#track" do | ||
it "tracks execution time" do | ||
tracked_result = TimeTracker.track { sleep(0.01) } | ||
expect(tracked_result.elapsed_time).to satisfy {|v| v > 0.01 && v < 0.1} | ||
end | ||
|
||
it "returns the proc return value" do | ||
tracked_result = TimeTracker.track { 42 } | ||
expect(tracked_result.result).to eq(42) | ||
end | ||
|
||
it "returns an object that behaves like the proc result" do | ||
tracked_result = TimeTracker.track { 42 } | ||
expect(tracked_result.to_i).to eq(42) | ||
expect(tracked_result + 1).to eq(43) | ||
end | ||
end | ||
end |