-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.rb
68 lines (60 loc) · 1.68 KB
/
timer.rb
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
require 'compsci/timer'
require 'minitest/autorun'
include CompSci
describe Timer do
parallelize_me!
describe "elapsed" do
it "returns the block value and a positive number" do
answer, elapsed = Timer.elapsed { sleep 0.01; :foo }
expect(answer).must_equal :foo
expect(elapsed).must_be :>, 0
expect(elapsed).must_be :<, 0.1
end
end
describe "since" do
it "must be positive" do
start = Timer.now
sleep 0.01
elapsed = Timer.since(start)
expect(elapsed).must_be :>, 0
expect(elapsed).must_be :<, 0.1
end
end
describe "loop_avg" do
it "returns the block value and a positive number" do
start = Timer.now
answer, avg_et = Timer.loop_avg(seconds: 0.1) {
sleep 0.01
:foo
}
elapsed = Timer.since(start)
expect(answer).must_equal :foo
expect(avg_et).must_be :>, 0.005
expect(avg_et).must_be :<, 0.05
expect(elapsed).must_be :>, 0.05
expect(elapsed).must_be :<, 0.5
end
it "ceases looping after 5 loops" do
start = Timer.now
_answer, avg_et = Timer.loop_avg(count: 5) {
sleep 0.01
}
elapsed = Timer.since(start)
expect(avg_et).must_be :>, 0.005
expect(avg_et).must_be :<, 0.05
expect(elapsed).must_be :>, 0.01
expect(elapsed).must_be :<, 0.1
end
it "won't interrupt long loops" do
start = Timer.now
_answer, avg_et = Timer.loop_avg(seconds: 0.01) {
sleep 0.1
}
elapsed = Timer.since(start)
expect(avg_et).must_be :>, 0.05
expect(avg_et).must_be :<, 0.5
expect(elapsed).must_be :>, 0.05
expect(elapsed).must_be :<, 0.5
end
end
end