-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_imgtimer.py
executable file
·111 lines (98 loc) · 2.96 KB
/
check_imgtimer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import urllib2
import getopt
import random
import time
import socket
import math
import sys
import re
curlmode = None
curlurl = None
curliter = None
curlportal = None
usecdn = None
socket.setdefaulttimeout(80)
#Takes a random page
page = random.Random().randint(1,40)
myopts, args = getopt.getopt(sys.argv[1:],"u:m:i:p:c:")
for o, a in myopts:
if o == '-u':
curlurl=a
elif o == '-m':
curlmode=a
elif o == '-p':
curlportal=a
elif o == '-i':
curliter=a
elif o == '-c':
usecdn=a
if None in (curlmode, curlurl, curliter, curlportal, usecdn):
print 'Usage: ./imgtimer.py -u server_url -m curl mode (balanced/single) -i number of iterations -p portal (one/two/three) -c Use cdn? (akamai/nginx)'
exit(2)
#Replaces akamai for static
if usecdn in ['Akamai', 'akamai']:
USE_STATIC = False
elif usecdn in ['Nginx','nginx']:
USE_STATIC = True
#Akamai url hardcoded :(
def akamaiurl(curlportal):
return {
'one': 'akamai.domainone.com',
'two': 'akamai.domaintwo.com',
'three': 'akamai.domainthree.com',
}.get(curlportal)
AKAMAI = akamaiurl(curlportal)
#We know static content is in another port. We'll use that if we're not through balancer
if curlmode in ['Single', 'single']:
STATIC2 = curlurl + ':67'
if curlmode in ['Balanced', 'balanced']:
STATIC2 = curlurl
IMAGES = int(curliter)
#Specific url depending on website
def portal(curlportal):
return {
'one': 'http://www.domainone.com/listing-' + str(page) + '.html',
'two': 'http://www.domaintwo.com/listing-' + str(page) + '.html',
'three' : 'http://www.domainthree.com/listing-' + str(page) + '.html',
}.get(curlportal)
page_url = portal(curlportal)
before = time.time()
search_results = urllib2.urlopen(page_url).read()
after = time.time()
if curlportal == 'domainone':
images = re.findall('data-lazy="([^"]+)" width="280" class="rsTmb rsHide"', search_results)
else:
images = re.findall('<a class="rsImg rsMainSlideImage rsHide" href="([^"]+)">', search_results)
if len(images) > IMAGES:
images = images[:IMAGES]
timings = []
errors = 0
cache_misses = 0
count = 0
for image in images:
try:
if USE_STATIC:
image = image.replace(AKAMAI, STATIC2)
count = count + 1
#print '[' + str(count) + '/' + str(len(images)) + ']', image #Intended for debugging
####DEBUGprint image
before = time.time()
u = urllib2.urlopen(image)
data = u.read()
u.close()
after = time.time()
t = int(math.ceil((after - before) * 1000))
timings.append(t)
except Exception, e:
###DEBUGprint e
errors = errors + 1
err_rate = str((errors/(len(images)))*100)
timing_data = ' Median time: ' + str(timings[len(timings)/2]) + 'ms Max time: ' + str(max(timings)) + 'ms Avg time: ' + str(sum(timings) / len(timings)) + 'ms | median=' + str(timings[len(timings)/2]) + ' avg=' + str(sum(timings) / len(timings))
if errors > 10:
print 'CRITICAL - Error rate ' + err_rate + '%' + timing_data
exit(2)
else:
print 'OK - Error rate ' + err_rate + '%' + timing_data
exit(0)