-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_ping.rb
50 lines (43 loc) · 1.44 KB
/
http_ping.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
require 'net/http'
require 'uri'
require 'benchmark'
# Use the passed URI as a parameter - otherwise use GitLab
if ARGV.empty?
use_uri = 'https://www.gitlab.com/'
else
use_uri = ARGV[0]
if use_uri =~ /\A#{URI::regexp(['http', 'https'])}\z/
# URL is valid
else
puts "The provided URI, \"#{use_uri}\" is not valid. Exiting."
exit
end
end
begin
# Start the program timer, stop after five minutes
start = Time.now
time_arr = []
# Repeat the test until 300 seconds (five minutes) have elapsed
while (Time.now - start < 300)
# Benchmark the elapsed time to retrieve the URI, then push in on the array
response_time = Benchmark.realtime {Net::HTTP.get_response(URI.parse(use_uri))}
time_arr.push (response_time)
puts "http_ping #{use_uri}: #{response_time.round(3)}s"
end
# Run through all values, determine the average and longest time values
curr_total = 0
curr_longest = 0
time_arr.each do |timeval|
curr_total += timeval
timeval > curr_longest ? curr_longest = timeval : true
end
# Find the average time, or set to zero if there are no values
time_arr.length > 0 ? curr_avg = curr_total / time_arr.length : curr_avg = 0
# Output the average, longest and complete response times
puts "The average HTTP response time was #{curr_avg.round(3)}s, with " +
"a longest time of #{curr_longest.round(3)}s."
rescue => err
# Handle any HTTP related errors
puts "Error encountered: #{err}"
err
end