<s> """<STR_LIT>""" <EOL> import logging <EOL> import time <EOL> from math import ceil <EOL> from os import path <EOL> from bzt . engine import ScenarioExecutor <EOL> from bzt . modules . aggregator import ConsolidatingAggregator , ResultsReader <EOL> from bzt . six import iteritems <EOL> from bzt . utils import shell_exec , shutdown_process , RequiredTool , dehumanize_time <EOL> class ApacheBenchmarkExecutor ( ScenarioExecutor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( ApacheBenchmarkExecutor , self ) . __init__ ( ) <EOL> self . log = logging . getLogger ( '<STR_LIT>' ) <EOL> self . process = None <EOL> self . __tsv_file_name = None <EOL> self . __out = None <EOL> self . __err = None <EOL> self . tool_path = None <EOL> self . scenario = None <EOL> self . reader = None <EOL> self . start_time = None <EOL> def prepare ( self ) : <EOL> self . scenario = self . get_scenario ( ) <EOL> self . tool_path = self . _check_installed ( ) <EOL> self . __tsv_file_name = self . engine . create_artifact ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> out_file_name = self . engine . create_artifact ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . reader = TSVDataReader ( self . __tsv_file_name , self . log ) <EOL> if isinstance ( self . engine . aggregator , ConsolidatingAggregator ) : <EOL> self . engine . aggregator . add_underling ( self . reader ) <EOL> self . __out = open ( out_file_name , '<STR_LIT:w>' ) <EOL> self . __err = open ( self . engine . create_artifact ( "<STR_LIT>" , "<STR_LIT>" ) , '<STR_LIT:w>' ) <EOL> def startup ( self ) : <EOL> args = [ self . tool_path ] <EOL> load = self . get_load ( ) <EOL> if load . hold : <EOL> hold = int ( ceil ( dehumanize_time ( load . hold ) ) ) <EOL> args += [ '<STR_LIT>' , str ( hold ) ] <EOL> elif load . iterations : <EOL> args += [ '<STR_LIT>' , str ( load . iterations ) ] <EOL> else : <EOL> args += [ '<STR_LIT>' , '<STR_LIT:1>' ] <EOL> load_concurrency = load . concurrency if load . concurrency is not None else <NUM_LIT:1> <EOL> args += [ '<STR_LIT:-c>' , str ( load_concurrency ) ] <EOL> args += [ '<STR_LIT>' ] <EOL> args += [ '<STR_LIT>' , str ( self . __tsv_file_name ) ] <EOL> for key , val in iteritems ( self . scenario . get_headers ( ) ) : <EOL> args += [ '<STR_LIT>' , "<STR_LIT>" % ( key , val ) ] <EOL> requests = list ( self . scenario . get_requests ( ) ) <EOL> if not requests : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if len ( requests ) > <NUM_LIT:1> : <EOL> self . log . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> request = requests [ <NUM_LIT:0> ] <EOL> for header in request . headers : <EOL> for key , val in iteritems ( header ) : <EOL> args += [ '<STR_LIT>' , "<STR_LIT>" % ( key , val ) ] <EOL> if request . method != '<STR_LIT:GET>' : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> keepalive = True <EOL> if request . config . get ( '<STR_LIT>' ) is not None : <EOL> keepalive = request . config . get ( '<STR_LIT>' ) <EOL> elif self . scenario . get ( '<STR_LIT>' ) is not None : <EOL> keepalive = self . scenario . get ( '<STR_LIT>' ) <EOL> if keepalive : <EOL> args += [ '<STR_LIT>' ] <EOL> args += [ request . url ] <EOL> self . reader . setup ( load_concurrency , request . label ) <EOL> self . start_time = time . time ( ) <EOL> self . process = self . execute ( args , stdout = self . __out , stderr = self . __err ) <EOL> def check ( self ) : <EOL> ret_code = self . process . poll ( ) <EOL> if ret_code is None : <EOL> return False <EOL> self . log . info ( "<STR_LIT>" , ret_code ) <EOL> if ret_code != <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return True <EOL> def shutdown ( self ) : <EOL> shutdown_process ( self . process , self . log ) <EOL> def post_process ( self ) : <EOL> if self . __out and not self . __out . closed : <EOL> self . __out . close ( ) <EOL> if self . __err and not self . __err . closed : <EOL> self . __err . close ( ) <EOL> def _check_installed ( self ) : <EOL> tool_path = self . settings . get ( '<STR_LIT:path>' , '<STR_LIT>' ) <EOL> ab_tool = ApacheBenchmark ( tool_path , self . log ) <EOL> if not ab_tool . check_if_installed ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return tool_path <EOL> class TSVDataReader ( ResultsReader ) : <EOL> def __init__ ( self , filename , parent_logger ) : <EOL> super ( TSVDataReader , self ) . __init__ ( ) <EOL> self . log = parent_logger . getChild ( self . __class__ . __name__ ) <EOL> self . filename = filename <EOL> self . fds = None <EOL> self . skipped_header = False <EOL> self . concurrency = None <EOL> self . url_label = None <EOL> def setup ( self , concurrency , url_label ) : <EOL> self . concurrency = concurrency <EOL> self . url_label = url_label <EOL> def __open_fds ( self ) : <EOL> if not path . isfile ( self . filename ) : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> return False <EOL> if not path . getsize ( self . filename ) : <EOL> self . log . debug ( "<STR_LIT>" , self . filename ) <EOL> return False <EOL> if not self . fds : <EOL> self . fds = open ( self . filename ) <EOL> return True <EOL> def __del__ ( self ) : <EOL> if self . fds : <EOL> self . fds . close ( ) <EOL> def _read ( self , last_pass = False ) : <EOL> while not self . fds and not self . __open_fds ( ) : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> yield None <EOL> if last_pass : <EOL> lines = self . fds . readlines ( ) <EOL> self . fds . close ( ) <EOL> else : <EOL> lines = self . fds . readlines ( <NUM_LIT> * <NUM_LIT> ) <EOL> for line in lines : <EOL> if not self . skipped_header : <EOL> self . skipped_header = True <EOL> continue <EOL> log_vals = [ val . strip ( ) for val in line . split ( '<STR_LIT:\t>' ) ] <EOL> _error = None <EOL> _rstatus = None <EOL> _url = self . url_label <EOL> _concur = self . concurrency <EOL> _tstamp = int ( log_vals [ <NUM_LIT:1> ] ) <EOL> _con_time = float ( log_vals [ <NUM_LIT:2> ] ) <EOL> _etime = float ( log_vals [ <NUM_LIT:4> ] ) <EOL> _latency = float ( log_vals [ <NUM_LIT:5> ] ) <EOL> yield _tstamp , _url , _concur , _etime , _con_time , _latency , _rstatus , _error , '<STR_LIT>' <EOL> class ApacheBenchmark ( RequiredTool ) : <EOL> def __init__ ( self , tool_path , parent_logger ) : <EOL> super ( ApacheBenchmark , self ) . __init__ ( "<STR_LIT>" , tool_path ) <EOL> self . tool_path = tool_path <EOL> self . log = parent_logger . getChild ( self . __class__ . __name__ ) <EOL> def check_if_installed ( self ) : <EOL> self . log . debug ( '<STR_LIT>' % self . tool_path ) <EOL> try : <EOL> shell_exec ( [ self . tool_path , '<STR_LIT>' ] ) <EOL> except OSError : <EOL> return False <EOL> return True </s>
