Created
April 10, 2013 03:33
-
-
Save ernestom/5351576 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
from re import findall | |
from time import time | |
from datetime import datetime | |
from csv import writer | |
from pprint import pformat | |
from requests import get | |
### <CONFING> | |
EDGE_HOSTNAMES = ( | |
'brss.clarovideo.com.edgesuite.net', # UNINET | |
'brss2.clarovideo.com.edgesuite.net', # UNINET | |
#'brss1.clarovideo.com.edgesuite.net', # BRAZILIAN HOST | |
) | |
URLS = ( | |
'/multimediav81/plataforma_vod/ISM/WMP4H00984MTDS_full/WMP4H00984MTDS_full.ism/QualityLevels(2524000)/Fragments(video=300300000)', | |
'/multimediav81/plataforma_vod/ISM/WMP4H00984MTDS_full/WMP4H00984MTDS_full.ism/QualityLevels(2524000)/Fragments(video=380380000)', | |
'/multimediav81/plataforma_vod/ISM/WMP4H00984MTDS_full/WMP4H00984MTDS_full.ism/QualityLevels(2524000)/Fragments(video=440440000)', | |
'/multimediav81/plataforma_vod/ISM/WMP4H00984MTDS_full/WMP4H00984MTDS_full.ism/QualityLevels(2524000)/Fragments(video=480480000)', | |
'/multimediav81/plataforma_vod/ISM/WMP4H00984MTDS_full/WMP4H00984MTDS_full.ism/QualityLevels(2524000)/Fragments(video=80080000)', | |
'/multimediav81/plataforma_vod/ISM/WMP4H00984MTDS_full/WMP4H00984MTDS_full.ism/QualityLevels(2524000)/Fragments(video=180180000)', | |
'/multimediav81/plataforma_vod/ISM/WMP4H00984MTDS_full/WMP4H00984MTDS_full.ism/QualityLevels(2524000)/Fragments(video=240240000)', | |
) | |
### </CONFIG> | |
AKAMAI_PRAGMA = ( | |
'akamai-x-cache-on', 'akamai-x-cache-remote-on', | |
'akamai-x-check-cacheable', 'akamai-x-get-cache-key', | |
'akamai-x-get-extracted-values', 'akamai-x-get-nonces', | |
'akamai-x-get-ssl-client-session-id', 'akamai-x-get-true-cache-key', | |
'akamai-x-serial-no', 'akamai-x-cache-on', 'akamai-x-cache-remote-on', | |
'akamai-x-check-cacheable', 'akamai-x-get-cache-key', | |
'akamai-x-get-extracted-values', 'akamai-x-get-nonces', | |
'akamai-x-get-ssl-client-session-id', | |
'akamai-x-get-true-cache-key', 'akamai-x-serial-no' | |
) | |
def request(url): | |
"""Returns a tuple of: | |
* timestamp | |
* url | |
* time taken | |
* content length in KB | |
* cache code, e.g. TCP_HIT | |
* edge ip | |
* raw headers | |
""" | |
headers = { | |
'Host': findall(r'http://([^/]+)\.edgesuite\.net.+', url)[0], | |
'Pragma': ','.join(AKAMAI_PRAGMA), | |
} | |
start = time() | |
response = get(url, headers=headers) | |
end = time() | |
timestamp = datetime.now().isoformat() | |
time_taken = '%0.3fs' % (end - start) | |
content_length = '%0.2fKB' % (int(response.headers['content-length']) / 1024) | |
cache_info = response.headers['X-Cache'].split() | |
cache_code = cache_info[0] | |
edge_host = cache_info[2] | |
headers = pformat(response.headers) | |
return timestamp, url, time_taken, content_length, cache_code, edge_host, headers | |
def collect_stats(edge_hostnames, urls, rounds=1): | |
"""Collects and saves the stats of HTTP requests made to the provided hosts/urls.""" | |
# build a list of all urls needed | |
urls = ['http://%s%s' % (edge_hostname, url) | |
for edge_hostname in edge_hostnames for url in urls] | |
# collect the stats by making http requests | |
stats = [request(url) for url in urls * rounds] | |
# write the stats in a csv file | |
now = datetime.now().isoformat()[:16].replace(':', '-') | |
csv_file_name = 'clarotests_%s.csv' % now | |
with open(csv_file_name, 'wb') as csv_file: | |
w = writer(csv_file) | |
w.writerows(stats) | |
return csv_file_name | |
if __name__ == '__main__': | |
csv_file = collect_stats(EDGE_HOSTNAMES, URLS) | |
print 'Stats collected successfully in %s' % csv_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment