Skip to content

Instantly share code, notes, and snippets.

@tsertkov
Last active May 26, 2023 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsertkov/d06a78a987a95e0f409408908f01a5fc to your computer and use it in GitHub Desktop.
Save tsertkov/d06a78a987a95e0f409408908f01a5fc to your computer and use it in GitHub Desktop.
Http response time monitoring script (ruby)

http-monitor.rb

Simple ruby script to monitor http response times over a period of time.

% ruby http-monitor.rb https://google.com 20 3

2023-05-26 12:13:59 : (GET https://google.com) : 1.207913s
2023-05-26 12:14:02 : (GET https://google.com) : 0.102176s
2023-05-26 12:14:05 : (GET https://google.com) : 0.146981s
2023-05-26 12:14:08 : (GET https://google.com) : 0.100712s
2023-05-26 12:14:11 : (GET https://google.com) : 0.101287s
2023-05-26 12:14:15 : (GET https://google.com) : 0.110682s
2023-05-26 12:14:19 : (GET https://google.com) : 1.131652s
---
Total running time: 23.904733s
Total probes made: 7
Average response time: 0.41448614285714286s
Min response time: 0.100712s
Max response time: 1.207913s
# http-monitor.rb
require 'uri'
require 'net/http'
# input parameters
url = ARGV[0] || 'https://google.com'
time = (ARGV[1] || 300).to_f
sleep_interval = (ARGV[2] || 5).to_f
# lib
def stats(start_time, end_time, probes)
puts '---'
puts 'Total running time: ' + (end_time - start_time).to_s + 's'
puts 'Total probes made: ' + probes.length.to_s
puts 'Average response time: ' + probes.sum.fdiv(probes.size).to_s + 's'
puts 'Min response time: ' + probes.min.to_s + 's'
puts 'Max response time: ' + probes.max.to_s + 's'
end
# init http
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == 'https'
start_time = Time.now
probes = []
# process ctrl_c nicely and display monitoring stats
Signal.trap('INT') {
stats(start_time, Time.now, probes)
abort 'Interrupted by user'
}
# run probes in a loop with sleeping between probes
while Time.now - start_time < time
start_probe_time = Time.now
res = http.get(uri.path ? '/' : uri.path)
elapsed_probe_time = Time.now - start_probe_time
probes << elapsed_probe_time
puts Time.now.strftime('%Y-%m-%d %H:%M:%S') + ' : (GET ' + uri.to_s + ') : ' + elapsed_probe_time.to_s + 's'
sleep(sleep_interval)
end
# display monitoring stats
stats(start_time, Time.now, probes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment