-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab.rb
67 lines (55 loc) · 1.25 KB
/
gitlab.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
require 'net/http'
def redirected(location)
# warn "redirected to #{location}"
fetcher(location)
end
def fetcher(uri_str)
response = Net::HTTP.get_response(URI(uri_str))
case response
when Net::HTTPSuccess
response
when Net::HTTPRedirection
redirected(response['location'])
else
response.value
end
end
def now
Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
end
class Timer
attr_accessor :nanoseconds, :calls
def initialize(start)
@nanoseconds = 0
@calls = 0
@start = start
end
def seconds
@nanoseconds / (10**9)
end
def fetch(current_time)
response = fetcher('https://gitlab.com')
elapsed = now - current_time
time = Time.at(0, elapsed, :nsec).nsec
duration = time.to_f.truncate(2).to_i
puts "dur=#{duration} ns; status=#{response.class.name};"
duration
end
def run
# Maybe interval?
start_time = now
current_time = start_time
while current_time < start_time + @start * (10**9)
ns = fetch(current_time)
@nanoseconds += ns if ns
@calls += 1
current_time = now
end
end
end
def timer(start)
t = Timer.new(start)
t.run
puts "elapsed=#{t.nanoseconds}/#{t.seconds} ns/s, calls=#{t.calls}"
end
timer 5 * 60 if __FILE__ == $0