Created
May 8, 2017 16:11
-
-
Save wil3/149d62198c0621c04aca9c84804e097a to your computer and use it in GitHub Desktop.
This file contains 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
import requests, time, sys, random, string | |
from urlparse import urlparse | |
from time import sleep | |
import argparse | |
#import subprocess | |
class MeasureBoot: | |
def __init__(self): | |
self.current_token = None | |
self.start = None | |
self.redirect_times = [] | |
def handle_redirect(self, r, *args, **kwargs): | |
if r.status_code == 307 and "Location" in r.headers: | |
u = urlparse(r.headers["Location"]) | |
self.current_token = u.hostname.split(".")[0] | |
elapsed_time = time.time() - self.start | |
print "Token ", self.current_token | |
print "Time for 307", elapsed_time | |
self.redirect_times.append(elapsed_time) | |
if r.status_code == 200: | |
elapsed_time = time.time() - self.start | |
#Time when we get header but not all data | |
print "Time for 200", elapsed_time | |
def boot(self, url, N, timeout=1): | |
o = urlparse(url) | |
times = [] | |
for i in range(N): | |
#we are adding a subdomain to bypass the resolving cache | |
random_subdomain = 'A' + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(9)) | |
port = 80 | |
if o.port: | |
port = o.port | |
random_url = o._replace(netloc="{}.{}:{}".format(random_subdomain, o.hostname, port)) | |
print "GET", random_url.geturl() | |
#subprocess.call(["curl", "-s", "-o", "/dev/null", "-w", '"%{http_code}"', "-L", "-b", "-j",random_url.geturl()]) | |
try: | |
start_time = time.time() | |
self.start = start_time | |
r = requests.get(random_url.geturl(), hooks=dict(response=self.handle_redirect)) | |
#print r.text | |
if r.status_code == 200: | |
if "secret" in r.cookies: | |
print "ID ", r.cookies['secret'] | |
print "Size returned ", len(r.text) | |
elapsed_time = time.time() - start_time | |
print i, " ", elapsed_time | |
times.append(elapsed_time) | |
else: | |
print "Something bad happened ", r.status_code | |
sys.exit(0) | |
#Be nice | |
sleep(timeout) | |
except Exception as e: | |
print e | |
return times | |
import socket | |
try: | |
from http.client import HTTPConnection | |
except ImportError: | |
from httplib import HTTPConnection | |
# HTTP | |
class MyHTTPConnection(HTTPConnection): | |
def connect(self): | |
self.sock = socket.socket(socket.AF_INET) | |
self.sock.connect((self.host, self.port)) | |
if self._tunnel_host: | |
self._tunnel() | |
requests.packages.urllib3.connectionpool.HTTPConnection = MyHTTPConnection | |
requests.packages.urllib3.connectionpool.HTTPConnectionPool.ConnectionCls = MyHTTPConnection | |
def mean_confidence(data, zscore=1.96): | |
#http://sphweb.bumc.bu.edu/otlt/mph-modules/bs/bs704_confidence_intervals/bs704_confidence_intervals_print.html | |
n = float(len(data)) | |
mean = np.mean(data) | |
std = np.std(data) | |
confidence = zscore * std / np.sqrt(n) | |
return mean, confidence | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="measure time it takes to boot, or init a client into system") | |
parser.add_argument("url", help="website root url") | |
parser.add_argument("N", type=int, help="number times to run experiement") | |
args = parser.parse_args() | |
import numpy as np | |
mb = MeasureBoot() | |
times = mb.boot(args.url, args.N, timeout=0.25) | |
m, c = mean_confidence(times) | |
if len(mb.redirect_times) > 0: | |
_307_m, _307_c = mean_confidence(mb.redirect_times) | |
print "307 Mean ",_307_m | |
print "307 +-",_307_c | |
print "200 Mean ", m | |
print "200 +-", c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment