-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TimedTrace represents a chunk of code performance of which was measured and stored under a label. The chunk is called a "span". ```ruby timed_trace = TimedTrace.new timed_trace.span('http request') do http.get('example.com') end timed_trace.spans #=> { 'http request' => 0.123 } ```
- Loading branch information
Showing
7 changed files
with
268 additions
and
4 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
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
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,58 @@ | ||
module Airbrake | ||
# TimedTrace represents a chunk of code performance of which was measured and | ||
# stored under a label. The chunk is called a "span". | ||
# | ||
# @example | ||
# timed_trace = TimedTrace.new | ||
# timed_trace.span('http request') do | ||
# http.get('example.com') | ||
# end | ||
# timed_trace.spans #=> { 'http request' => 0.123 } | ||
# | ||
# @api public | ||
# @since v4.3.0 | ||
class TimedTrace | ||
# @param [String] label | ||
# @return [Airbrake::TimedTrace] | ||
def self.span(label, &block) | ||
new.tap { |timed_trace| timed_trace.span(label, &block) } | ||
end | ||
|
||
def initialize | ||
@spans = {} | ||
end | ||
|
||
# @param [String] label | ||
# @return [Boolean] | ||
def span(label) | ||
start_span(label) | ||
yield | ||
stop_span(label) | ||
end | ||
|
||
# @param [String] label | ||
# @return [Boolean] | ||
def start_span(label) | ||
return false if @spans.key?(label) | ||
|
||
@spans[label] = Airbrake::Benchmark.new | ||
true | ||
end | ||
|
||
# @param [String] label | ||
# @return [Boolean] | ||
def stop_span(label) | ||
return false unless @spans.key?(label) | ||
|
||
@spans[label].stop | ||
true | ||
end | ||
|
||
# @return [Hash<String=>Float>] | ||
def spans | ||
@spans.each_with_object({}) do |(label, benchmark), new_spans| | ||
new_spans[label] = benchmark.duration | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,33 @@ | ||
RSpec.describe Airbrake::Benchmark do | ||
describe ".measure" do | ||
it "returns measured performance time" do | ||
expect(subject.measure { '10' * 10 }).to be_kind_of(Numeric) | ||
expect(described_class.measure { '10' * 10 }).to be_kind_of(Numeric) | ||
end | ||
end | ||
|
||
describe "#stop" do | ||
before { subject } | ||
|
||
context "when called one time" do | ||
its(:stop) { is_expected.to eq(true) } | ||
end | ||
|
||
context "when called twice or more" do | ||
before { subject.stop } | ||
|
||
its(:stop) { is_expected.to eq(false) } | ||
end | ||
end | ||
|
||
describe "#duration" do | ||
context "when #stop wasn't called yet" do | ||
its(:duration) { is_expected.to be_zero } | ||
end | ||
|
||
context "when #stop was called" do | ||
before { subject.stop } | ||
|
||
its(:duration) { is_expected.to be > 0 } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
RSpec.describe Airbrake::TimedTrace do | ||
describe ".span" do | ||
it "returns a timed trace" do | ||
expect(described_class.span('operation') {}).to be_a(described_class) | ||
end | ||
|
||
it "returns a timed trace with a stopped span" do | ||
timed_trace = described_class.span('operation') {} | ||
expect(timed_trace.spans).to match('operation' => be > 0) | ||
end | ||
end | ||
|
||
describe "#span" do | ||
it "captures a span" do | ||
subject.span('operation') {} | ||
expect(subject.spans).to match('operation' => be > 0) | ||
end | ||
end | ||
|
||
describe "#start_span" do | ||
context "when called once" do | ||
it "returns true" do | ||
expect(subject.start_span('operation')).to eq(true) | ||
end | ||
end | ||
|
||
context "when called multiple times" do | ||
before { subject.start_span('operation') } | ||
|
||
it "returns false" do | ||
expect(subject.start_span('operation')).to eq(false) | ||
end | ||
end | ||
|
||
context "when another span was started" do | ||
before { subject.start_span('operation') } | ||
|
||
it "returns true" do | ||
expect(subject.start_span('another operation')).to eq(true) | ||
end | ||
end | ||
|
||
context "when #spans was called" do | ||
before { subject.start_span('operation') } | ||
|
||
it "returns spans with zero values" do | ||
expect(subject.spans).to eq('operation' => 0.0) | ||
end | ||
end | ||
end | ||
|
||
describe "#stop_span" do | ||
context "when #start_span wasn't invoked" do | ||
it "returns false" do | ||
expect(subject.stop_span('operation')).to eq(false) | ||
end | ||
end | ||
|
||
context "when #start_span was invoked" do | ||
before { subject.start_span('operation') } | ||
|
||
it "returns true" do | ||
expect(subject.stop_span('operation')).to eq(true) | ||
end | ||
end | ||
|
||
context "when multiple spans were started" do | ||
before do | ||
subject.start_span('operation') | ||
subject.start_span('another operation') | ||
end | ||
|
||
context "and when stopping in LIFO order" do | ||
it "returns true for all spans" do | ||
expect(subject.stop_span('another operation')).to eq(true) | ||
expect(subject.stop_span('operation')).to eq(true) | ||
end | ||
end | ||
|
||
context "and when stopping in FIFO order" do | ||
it "returns true for all spans" do | ||
expect(subject.stop_span('operation')).to eq(true) | ||
expect(subject.stop_span('another operation')).to eq(true) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "#spans" do | ||
context "when no spans were captured" do | ||
it "returns an empty hash" do | ||
expect(subject.spans).to eq({}) | ||
end | ||
end | ||
|
||
context "when a span was captured" do | ||
before do | ||
subject.start_span('operation') | ||
subject.stop_span('operation') | ||
end | ||
|
||
it "returns a Hash with the corresponding span" do | ||
subject.stop_span('operation') | ||
expect(subject.spans).to match('operation' => be > 0) | ||
end | ||
end | ||
|
||
context "when multiple spans were captured" do | ||
before do | ||
subject.start_span('operation') | ||
subject.stop_span('operation') | ||
|
||
subject.start_span('another operation') | ||
subject.stop_span('another operation') | ||
end | ||
|
||
it "returns a Hash with all spans" do | ||
expect(subject.spans).to match( | ||
'operation' => be > 0, | ||
'another operation' => be > 0 | ||
) | ||
end | ||
end | ||
end | ||
end |