-
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.
- Loading branch information
Showing
6 changed files
with
222 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Airbrake | ||
# RouteTrace represents a named & timed sequence of events that consists of | ||
# spans. | ||
# | ||
# @api public | ||
# @since v4.3.0 | ||
class RouteTrace | ||
def initialize | ||
@spans = {} | ||
end | ||
|
||
def span(label) | ||
start_span(label) | ||
yield | ||
stop_span(label) | ||
end | ||
|
||
def start_span(label) | ||
return false if @spans.key?(label) | ||
|
||
@spans[label] = Airbrake::Benchmark.new | ||
true | ||
end | ||
|
||
def stop_span(label) | ||
return false unless @spans.key?(label) | ||
|
||
@spans[label].stop | ||
true | ||
end | ||
|
||
def spans | ||
@spans.each_with_object({}) do |(label, benchmark), new_groups| | ||
new_groups[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
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,114 @@ | ||
RSpec.describe Airbrake::RouteTrace do | ||
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 |