Author: tmckay
Date: 2011-01-23 00:30:31 +0000 (Sun, 23 Jan 2011)
New Revision: 4480
Added:
branches/scale_testing/mech_scripts/
branches/scale_testing/mech_scripts/test_driver.py
Log:
Don't want to lose this test script.
For use with multi-mechanize, or standalone.
Added: branches/scale_testing/mech_scripts/test_driver.py
===================================================================
--- branches/scale_testing/mech_scripts/test_driver.py (rev 0)
+++ branches/scale_testing/mech_scripts/test_driver.py 2011-01-23 00:30:31 UTC (rev 4480)
@@ -0,0 +1,121 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2010 Corey Goldberg (corey(a)goldb.org)
+# License: GNU LGPLv3
+#
+# This file is part of Multi-Mechanize
+
+
+import httplib
+from time import time as time_time
+#import datetime
+from datetime import *
+import optparse
+import sys
+import string
+import os
+import traceback
+import copy
+
+class Transaction(object):
+ def __init__(self):
+ self.custom_timers = {}
+
+ self.http_date_gmt = "%a, %d %b %Y %H:%M:%S GMT"
+ self.headers = {}
+ self.script_options = None
+ self.machine = None
+ self.link_file = None
+ self.visited = {}
+
+ self.file_init = False
+ self.file_number = -1
+ self.file_iterations = 0
+
+ def next_file(self):
+ if not self.file_init:
+ self.link_init = True
+ if os.path.isdir(self.link_file):
+ self.file_list = os.listdir(self.link_file)
+ for i in range(len(self.file_list)):
+ self.file_list[i] = os.path.join(self.link_file, self.file_list[i])
+ else:
+ self.file_list = [self.link_file]
+
+ self.file_number += 1
+ if self.file_number == len(self.file_list):
+ self.file_iterations += 1
+ self.file_number = 0
+ return self.file_list[self.file_number]
+
+ def run(self):
+ try:
+ # parse options on first invocation, get machine and links file
+ if self.machine == None:
+ parser = optparse.OptionParser()
+ parser.add_option('--machine', dest='machine',
type='str', default="localhost:45672")
+ parser.add_option('--file', dest='file',
type='str', default="")
+ parser.add_option('--iter', dest='iter',
type='int', default=0)
+ if type(self.script_options) == str:
+ self.script_options = self.script_options.split()
+ opts, args = parser.parse_args(self.script_options)
+ self.machine = opts.machine
+ self.link_file = os.path.abspath(opts.file)
+ self.iteration_limit = opts.iter
+
+ start_timer = time_time()
+ f = open(self.next_file(), "r")
+
+ # If an iteration limit was set, bail after going through file list
+ # that many times
+ if self.iteration_limit and self.file_iterations == self.iteration_limit:
+ return True
+
+ total_gets = 0
+ get_latency = 0
+ for link in f:
+ link_info = link.split(',')
+
+ # Check for initial Boolean, indicates whether "If_Modified"
header was used
+ if len(link_info) == 2:
+ if_modified_header = link_info[0] = "True"
+ link = link_info[1].strip()
+ else:
+ if_modified_header = True
+ link = link_info[0].strip()
+
+ time_for_get = time_time()
+ conn = httplib.HTTPConnection(self.machine)
+ if link in self.visited and self.visited[link] and if_modified_header:
+ # value for link will be the time it was visited.
+ headers={"If-Modified-Since": self.visited[link]}
+ conn.request('GET', link, headers=headers)
+ else:
+ conn.request('GET', link)
+ resp = conn.getresponse()
+ if resp.status == 200:
+ self.visited[link] = datetime.utcnow().strftime(self.http_date_gmt)
+ print "Link " + link
+ elif resp.status == 304:
+ pass
+ print ("already visited", link)
+ else:
+ print ("other status", resp.status, link)
+ self.visited[link] = None
+ get_latency += time_time() - time_for_get
+ total_gets += 1
+
+ latency = time_time() - start_timer
+ self.custom_timers['Page File'] = latency
+ self.custom_timers['Avg Get'] = get_latency / total_gets
+ self.custom_timers['Total Gets'] = total_gets
+
+ except Exception, e:
+ traceback.print_exc()
+
+if __name__ == '__main__':
+ trans = Transaction()
+ trans.script_options=sys.argv[1:]
+ while not trans.run():
+ print trans.custom_timers
+
Show replies by date