As I write more and more functions to give results this will evolve into a full fledged test suite. For now it will start as a basic series of functions. I choose ruby since it has excellent cross platform socket creation. I'm using python for better community support and there's more documentation and examples available. Python is more widely used than ruby and figured this would be a better choice in the long run.
require 'net/http'
# basic function to find response time of HTTP request
def checkHTTP(host)
http_connect = Net::HTTP.new(host, 80)
init_time = Time.now
http_connect.get("/", nil)
time_diff = Time.now - init_time
# return in milliseconds
return time_diff * 1000
end
url = "allanfeid.com"
response_time = checkHTTP(url)
puts "Reponse time is #{response_time} ms"
After some thought and some issues with ruby's Socket class, I may do everything in python. Here's the same function in python.
import urllib
import time
def checkHTTP(host, page):
init_time = time.time()
urllib.urlretrieve(host, page)
# return in milliseconds
return (time.time() - init_time) * 1000
print checkHTTP("http://oslbproject.net", "index.php")
checkPing function, written in python using dpkt. I figured using raw packets was the best way to go as it will be used again later.
import dpkt import socket, time def checkPing(host): # setup echo payload echo = dpkt.icmp.ICMP.Echo() echo.data = 'ping' # create icmp packet icmp = dpkt.icmp.ICMP() icmp.type = dpkt.icmp.ICMP_ECHO icmp.data = echo # send the packet over a socket s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP) s.connect((host, 1)) start = time.time() sent = s.send(str(icmp)) buf = s.recv(0xffff) rtt = time.time() - start # return the response time in ms return rtt * 1000 ip = '192.168.1.1' print 'Response time: %.3f ms' % checkPing(ip)