[pLog-svn] r3423 - plog/trunk/tools

oscar at devel.lifetype.net oscar at devel.lifetype.net
Tue May 16 16:12:08 GMT 2006


Author: oscar
Date: 2006-05-16 16:12:08 +0000 (Tue, 16 May 2006)
New Revision: 3423

Modified:
   plog/trunk/tools/play.py
Log:
fixed a bug in the generation of the URL to be requested and implemented support for time-defined test runs via the '-t' parameter (specified in seconds) Threads will just loop over their original contents once they reach the end and they have not been requested to stop


Modified: plog/trunk/tools/play.py
===================================================================
--- plog/trunk/tools/play.py	2006-05-16 15:08:29 UTC (rev 3422)
+++ plog/trunk/tools/play.py	2006-05-16 16:12:08 UTC (rev 3423)
@@ -4,6 +4,7 @@
 import sys
 import time
 from threading import Thread
+from threading import Timer
 from optparse import OptionParser
 import httplib
 import urlparse
@@ -30,15 +31,27 @@
         self.num_requests = 0
         self.num_gets = 0
         self.num_posts = 0
+        self.cont_mode = False
+        # the thread will be checking this variable in order to know
+        # whether it's been (politely) asked to stop. After being requested
+        # to stop, it is supposed to quite processing and nicely present
+        # its data
+        self.force_stop = False
         
+    def set_cont_mode( self, mode ):
+        self.cont_mode = mode
+        
     def run(self ):        
-        
-        for line in self.data:
+    
+        #for line in self.data:
+        i = 0
+        while (i < len( self.data) and (self.force_stop == False)):
             # prepare the data
-            urldata = urlparse.urlsplit( line['url'] )            
+            line = self.data[i]            
+            urldata = urlparse.urlsplit( line['url'] )
             request = urldata[2]
-            if urldata[4]:
-                request = request + urldata[4];            
+            if urldata[3]:
+                request = request + "?" + urldata[3];
             
             # store the starting time
             req_start_time = time.time()
@@ -80,14 +93,16 @@
                 
             # check if we have to wait
             if self.wait > 0.0:
-                time.sleep( self.wait )            
+                time.sleep( self.wait )
+                
+            # move to the next line
+            i = i + 1
+            # unless we're in 'non-stop' mode...
+            if self.cont_mode == True:
+                i = i % len( self.data )
             
-        # save the average time
-        self.avg_time_per_request = self.total_time / self.num_requests
-        
-        #print self.data
-            
-        #print "requests = " + str( self.num_requests ) + ", total = " + str( self.total_time ) + ", avg = " + str( self.avg_time_per_request ) + ", max = " +  str( self.max_request_time ) + ", min = " + str( self.min_request_time )
+            # save the average time
+            self.avg_time_per_request = self.total_time / self.num_requests            
 
 #
 # class that loads a script file and parses it
@@ -138,6 +153,16 @@
             result[var[0].strip().replace( "\"", "" )] = var[1].strip()
             
         return result
+        
+#
+# callback method for the timer, that will tell the threads
+# that it's time to stop
+#
+def timer_callback():
+    for t in pool:
+        print "Informing thread " + str( t.id ) + " to stop!"
+        t.force_stop = True
+
 # 
 # process command line args
 #
@@ -147,6 +172,8 @@
 parser.add_option( "-w", "--wait", type="int", dest="milliseconds_wait", default=0 )
 parser.add_option( "-f", "--file", type="string", dest="filename" )
 parser.add_option( "-o", "--outfile", type="string", dest="outfile" )
+parser.add_option( "-t", "--time", type="int", dest="run_time", default=0 )
+
 (options, args ) = parser.parse_args()
 
 if options.filename == "":
@@ -180,11 +207,24 @@
 # initialize and call threads
 for t in range( 0, options.num_threads ):
     thread = WorkerThread( t, loader.data, seconds_wait )
+    # check if we've given a time frame in minutes to run
+    if options.run_time > 0:
+        thread.set_cont_mode( True )
+        
+    # append the thread to the pool
     pool.append( thread )
     thread.start()
     
+# configure our timer callback to stop the threads after the 
+# given amount of seconds
+if options.run_time > 0:
+    print "Running for " + str( options.run_time ) + " seconds..."
+    callback = Timer( options.run_time, timer_callback )
+    callback.start()
+else:
+    print "Waiting for threads to terminate..."
+    
 # collect statistics information from the threads
-print "Waiting for threads to terminate..."
 total_average_time = 0.0
 total_max_time = 0.0
 total_min_time = 99999999.0



More information about the pLog-svn mailing list