1+ import  argparse 
2+ import  subprocess 
3+ import  time 
4+ import  os 
5+ import  sys 
6+ from  config  import  Config 
7+ 
8+ def  run_stress_test (num_submissions , submission_delay , autograder_image , output_file , tango_port , tango_path , job_name , job_path ):
9+     with  open (output_file , 'a' ) as  f :        
10+         f .write (f"Stress testing with { num_submissions }   submissions\n " )
11+         for  i  in  range (1 , num_submissions  +  1 ):
12+             command  =  [
13+             'python3' , os .path .join (tango_path , 'clients/tango-cli.py' ),
14+             '-P' , str (tango_port ),
15+             '-k' , 'test' ,
16+             '-l' , job_name ,
17+             '--runJob' , job_path ,
18+             '--image' , autograder_image 
19+             ]
20+             subprocess .run (command , stdout  =  f , stderr  =  f )
21+             f .write (f"Submission { i }   submitted \n " )  
22+ 
23+             if  submission_delay  >  0 :
24+                 time .sleep (submission_delay )
25+     
26+     sys .exit ()
27+ 
28+ def  get_metrics (output_file ):
29+     if  Config .LOGFILE  ==  None :
30+         print ("Make sure logs are recorded in a log file" )
31+ 
32+     job_times  =  []
33+     with  open (Config .LOGFILE , 'r' ) as  f :  
34+         for  line  in  f :
35+             if  "finished after "  in  line :
36+                 start  =  line .find ("finished after " ) +  len ("finished after " )
37+                 seconds  =  int (line [start :].split ()[0 ])
38+                 job_times .append (seconds )
39+     
40+     with  open (output_file , 'a' ) as  f :
41+         if  len (job_times ) ==  0 :
42+             print ("No jobs have been completed" )
43+         else :
44+             avg  =  sum (job_times )/ len (job_times )
45+             f .write (f"Average job time is { avg }   seconds \n " )
46+     
47+     sys .exit ()
48+ 
49+ 
50+ if  __name__  ==  "__main__" :
51+     parser  =  argparse .ArgumentParser (description = "Stress test script for Tango" )
52+     parser .add_argument ('--num_submissions' , type = int , default = 10 , help = "Number of submissions" )
53+     parser .add_argument ('--submission_delay' , type = float , default = 1.0 , help = "Delay between submissions" )
54+     parser .add_argument ('--autograder_image' , type = str , required = True , help = "Autograder image" )
55+     parser .add_argument ('--output_file' , type = str , default = 'stress_test.out' , help = "Output file" )
56+     parser .add_argument ('--tango_port' , type = int , default = 4567 , help = "Tango server port" )
57+     parser .add_argument ('--tango_path' , type = str , required = True , help = "Path to Tango" )
58+     parser .add_argument ('--job_name' , type = str , required = True , help = "Name of the job" )
59+     parser .add_argument ('--job_path' , type = str , required = True , help = "Path to the job" )
60+     parser .add_argument ('--get_metrics' , type = bool , default  =  False , help =  "Set to true to get metrics, does not create new jobs" )
61+ 
62+     args  =  parser .parse_args ()
63+ 
64+     if  args .get_metrics :
65+         get_metrics (args .output_file )
66+     else :
67+         run_stress_test (
68+             args .num_submissions ,
69+             args .submission_delay ,
70+             args .autograder_image ,
71+             args .output_file ,
72+             args .tango_port ,
73+             args .tango_path ,
74+             args .job_name ,
75+             args .job_path 
76+         )
0 commit comments