spec/ddtelemetry/summary_spec.rb in ddtelemetry-1.0.0a1 vs spec/ddtelemetry/summary_spec.rb in ddtelemetry-1.0.0a2
- old
+ new
@@ -1,68 +1,74 @@
# frozen_string_literal: true
describe DDTelemetry::Summary do
subject(:summary) { described_class.new }
- context 'no observations' do
- it 'errors on #min' do
- expect { subject.min }
- .to raise_error(DDTelemetry::Summary::EmptySummaryError)
- end
+ describe '#get' do
+ subject { summary.get(:erb) }
- it 'errors on #max' do
- expect { subject.max }
- .to raise_error(DDTelemetry::Summary::EmptySummaryError)
+ before do
+ summary.observe(2.1, :erb)
+ summary.observe(4.1, :erb)
+ summary.observe(5.3, :haml)
end
- it 'errors on #avg' do
- expect { subject.avg }
- .to raise_error(DDTelemetry::Summary::EmptySummaryError)
- end
+ it { is_expected.to be_a(DDTelemetry::Stats) }
- it 'errors on #sum' do
- expect { subject.sum }
- .to raise_error(DDTelemetry::Summary::EmptySummaryError)
+ its(:sum) { is_expected.to eq(2.1 + 4.1) }
+ its(:count) { is_expected.to eq(2) }
+ end
+
+ describe '#labels' do
+ subject { summary.labels }
+
+ before do
+ summary.observe(2.1, :erb)
+ summary.observe(4.1, :erb)
+ summary.observe(5.3, :haml)
end
- its(:count) { is_expected.to eq(0) }
+ it { is_expected.to contain_exactly(:haml, :erb) }
end
- context 'one observation' do
- before { subject.observe(2.1) }
+ describe '#each' do
+ subject do
+ {}.tap do |res|
+ summary.each { |label, stats| res[label] = stats.avg.round(2) }
+ end
+ end
- its(:count) { is_expected.to eq(1) }
- its(:sum) { is_expected.to eq(2.1) }
- its(:avg) { is_expected.to eq(2.1) }
- its(:min) { is_expected.to eq(2.1) }
- its(:max) { is_expected.to eq(2.1) }
+ before do
+ summary.observe(2.1, :erb)
+ summary.observe(4.1, :erb)
+ summary.observe(5.3, :haml)
+ end
- it 'has proper quantiles' do
- expect(subject.quantile(0.00)).to eq(2.1)
- expect(subject.quantile(0.25)).to eq(2.1)
- expect(subject.quantile(0.50)).to eq(2.1)
- expect(subject.quantile(0.90)).to eq(2.1)
- expect(subject.quantile(0.99)).to eq(2.1)
+ it { is_expected.to eq(haml: 5.3, erb: 3.1) }
+
+ it 'is enumerable' do
+ expect(summary.map { |_label, stats| stats.sum }.sort)
+ .to eq([5.3, 2.1 + 4.1])
end
end
- context 'two observations' do
+ describe '#to_s' do
+ subject { summary.to_s }
+
before do
- subject.observe(2.1)
- subject.observe(4.1)
+ summary.observe(2.1, :erb)
+ summary.observe(4.1, :erb)
+ summary.observe(5.3, :haml)
end
- its(:count) { is_expected.to be_within(0.000001).of(2) }
- its(:sum) { is_expected.to be_within(0.000001).of(6.2) }
- its(:avg) { is_expected.to be_within(0.000001).of(3.1) }
- its(:min) { is_expected.to be_within(0.000001).of(2.1) }
- its(:max) { is_expected.to be_within(0.000001).of(4.1) }
+ it 'returns table' do
+ expected = <<~TABLE
+ │ count min .50 .90 .95 max tot
+ ─────┼────────────────────────────────────────────────
+ erb │ 2 2.10 3.10 3.90 4.00 4.10 6.20
+ haml │ 1 5.30 5.30 5.30 5.30 5.30 5.30
+ TABLE
- it 'has proper quantiles' do
- expect(subject.quantile(0.00)).to be_within(0.000001).of(2.1)
- expect(subject.quantile(0.25)).to be_within(0.000001).of(2.6)
- expect(subject.quantile(0.50)).to be_within(0.000001).of(3.1)
- expect(subject.quantile(0.90)).to be_within(0.000001).of(3.9)
- expect(subject.quantile(0.99)).to be_within(0.000001).of(4.08)
+ expect(subject.strip).to eq(expected.strip)
end
end
end