1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
# frozen_string_literal: true
require "spec_helper"
describe Hitimes::TimedValueMetric do
before(:each) do
@tm = Hitimes::TimedValueMetric.new("test-timed-value-metric")
end
it "knows if it is running or not" do
_(@tm.running?).must_equal false
@tm.start
_(@tm.running?).must_equal true
@tm.stop(1)
_(@tm.running?).must_equal false
end
it "#split returns the last duration and the timer is still running" do
@tm.start
d = @tm.split(1)
_(@tm.running?).must_equal true
_(d).must_be :>, 0
_(@tm.value_stats.count).must_equal 1
_(@tm.timed_stats.count).must_equal 1
_(@tm.duration).must_equal d
end
it "#stop returns false if called more than once in a row" do
@tm.start
_(@tm.stop(1)).must_be :>, 0
_(@tm.stop(1)).must_equal false
end
it "does not count a currently running interval as an interval in calculations" do
@tm.start
_(@tm.value_stats.count).must_equal 0
_(@tm.timed_stats.count).must_equal 0
@tm.split(1)
_(@tm.value_stats.count).must_equal 1
_(@tm.timed_stats.count).must_equal 1
end
it "#split called on a stopped timer does nothing" do
@tm.start
@tm.stop(1)
_(@tm.split(1)).must_equal false
end
it "calculates the mean of the durations" do
skip
3.times do |x|
@tm.start
@tm.stop(x)
end
_(@tm.timed_stats.mean).must_be :>, 0
_(@tm.value_stats.mean).must_equal 1.00
end
it "calculates the rate of the counts " do
skip
5.times do |x|
@tm.start
@tm.stop(x)
end
_(@tm.rate).must_be :>, 0
end
it "calculates the stddev of the durations" do
skip
3.times do |x|
@tm.start
@tm.stop(x)
end
_(@tm.timed_stats.stddev).must_be :>, 0
_(@tm.value_stats.stddev).must_equal 1.0
end
it "returns 0.0 for stddev if there is no data" do
_(@tm.timed_stats.stddev).must_equal 0.0
_(@tm.value_stats.stddev).must_equal 0.0
end
it "keeps track of the min value" do
skip
3.times do |x|
@tm.start
@tm.stop(x)
end
_(@tm.timed_stats.min).must_be :>, 0
_(@tm.value_stats.min).must_equal 0
end
it "keeps track of the max value" do
skip
3.times do |x|
@tm.start
@tm.stop(x)
end
_(@tm.timed_stats.max).must_be :>, 0
_(@tm.value_stats.max).must_equal 2
end
it "keeps track of the sum value" do
skip
3.times do |x|
@tm.start
@tm.stop(x)
end
_(@tm.timed_stats.sum).must_be :>, 0
_(@tm.value_stats.sum).must_equal 3
end
it "keeps track of the sum of squares value" do
skip
3.times do |x|
@tm.start
@tm.stop(x)
end
_(@tm.timed_stats.sumsq).must_be :>, 0
_(@tm.value_stats.sumsq).must_equal 5
end
it "keeps track of the minimum start time of all the intervals" do
f1 = Time.now.gmtime.to_f * 1_000_000
5.times do
@tm.start
@tm.stop(1)
end
f2 = Time.now.gmtime.to_f * 1_000_000
_(@tm.sampling_start_time).must_be :>=, f1
_(@tm.sampling_start_time).must_be :<, f2
end
it "keeps track of the last stop time of all the intervals" do
f1 = Time.now.gmtime.to_f * 1_000_000
5.times do
@tm.start
@tm.stop(1)
end
f2 = Time.now.gmtime.to_f * 1_000_000
_(@tm.sampling_stop_time).must_be :>, f1
_(@tm.sampling_stop_time).must_be :<=, f2
end
it "can create an already running timer" do
t = Hitimes::TimedValueMetric.now("already-running")
_(t.running?).must_equal true
end
it "can measure a block of code from an instance" do
skip
t = Hitimes::TimedValueMetric.new("measure a block")
3.times { t.measure(1) { sleep 0.001 } }
_(t.duration).must_be :>, 0
_(t.timed_stats.count).must_equal 3
_(t.value_stats.count).must_equal 3
end
it "returns the value of the block when measuring" do
skip
t = Hitimes::TimedValueMetric.new("measure a block")
x = t.measure(42) do
sleep 0.001
42
end
_(t.duration).must_be :>, 0
_(x).must_equal 42
end
describe "#to_hash" do
it "has name value" do
h = @tm.to_hash
_(h["name"]).must_equal "test-timed-value-metric"
end
it "has an empty has for additional_data" do
h = @tm.to_hash
_(h["additional_data"]).must_equal({})
_(h["additional_data"].size).must_equal 0
end
it "has a rate" do
skip
5.times do |x|
@tm.start
@tm.stop(x)
end
h = @tm.to_hash
_(h["rate"]).must_be :>, 0
end
it "has a unit_count" do
5.times do |x|
@tm.start
@tm.stop(x)
end
h = @tm.to_hash
_(h["unit_count"]).must_equal 10
end
fields = %w[name additional_data sampling_start_time sampling_stop_time value_stats timed_stats rate unit_count]
fields.each do |f|
it "has a value for #{f}" do
3.times { |x| @tm.measure(x) { sleep 0.001 } }
h = @tm.to_hash
_(h[f]).wont_be_nil
end
end
end
end
|