Last active
November 18, 2015 13:21
-
-
Save olavmrk/01cd0237b3629f539060 to your computer and use it in GitHub Desktop.
Time DNS requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
import socket | |
import sys | |
import threading | |
import time | |
if len(sys.argv) != 2: | |
print('Usage: dnstest.py HOSTNAME', file=sys.stderr) | |
sys.exit(1) | |
hostname = sys.argv[1] | |
try: | |
socket.gethostbyname(hostname) | |
except Exception as e: | |
print('Failed to look up hostname {hostname}: {err}'.format(hostname=hostname, err=unicode(e)), file=sys.stderr) | |
sys.exit(1) | |
def do_test(): | |
start = time.time() | |
addr = socket.gethostbyname(hostname) | |
end = time.time() | |
delay = end - start | |
ts = time.strftime('%H:%M:%S', time.localtime(start)) + '.{0:03d}'.format(int((start-int(start)) * 1000)) | |
logline = '{ts} {delay:.5f}\n'.format(ts=ts, delay=delay) | |
sys.stdout.write(logline) | |
sys.stdout.flush() | |
while True: | |
t = threading.Thread(target=do_test) | |
t.daemon = True | |
t.start() | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment