<s> import sys <EOL> import time <EOL> import random <EOL> import threading <EOL> from voltdbclient import * <EOL> def main ( ) : <EOL> if len ( sys . argv ) < <NUM_LIT:8> : <EOL> print "<STR_LIT>" <EOL> exit ( <NUM_LIT:1> ) <EOL> global max_contestant <EOL> max_contestant = int ( sys . argv [ <NUM_LIT:1> ] ) <EOL> if max_contestant < <NUM_LIT:1> or max_contestant > <NUM_LIT:12> : <EOL> print "<STR_LIT>" <EOL> exit ( <NUM_LIT:1> ) <EOL> global results_lock <EOL> global params_lock <EOL> global requestPermit <EOL> global availableThreads <EOL> global shouldContinue <EOL> global min_execution_secs <EOL> global max_execution_secs <EOL> global tot_execution_secs <EOL> global tot_executions <EOL> global tot_executions_latency <EOL> global check_latency <EOL> global latency_counter <EOL> global vote_result_counter <EOL> global invocation_params <EOL> results_lock = threading . Lock ( ) <EOL> params_lock = threading . Lock ( ) <EOL> requestPermit = threading . Semaphore ( <NUM_LIT:0> ) <EOL> availableThreads = threading . Semaphore ( <NUM_LIT:0> ) <EOL> invocation_params = [ ] <EOL> shouldContinue = True <EOL> min_execution_secs = <NUM_LIT> <EOL> max_execution_secs = - <NUM_LIT:1> <EOL> tot_execution_secs = <NUM_LIT:0> <EOL> tot_executions = <NUM_LIT:0> <EOL> tot_executions_latency = <NUM_LIT:0> <EOL> check_latency = False <EOL> latency_counter = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> vote_result_counter = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> max_votes_per_phone_number = long ( sys . argv [ <NUM_LIT:2> ] ) <EOL> transactions_per_sec = long ( sys . argv [ <NUM_LIT:3> ] ) <EOL> transactions_per_milli = transactions_per_sec / float ( <NUM_LIT:1000> ) <EOL> client_feedback_interval_secs = long ( sys . argv [ <NUM_LIT:4> ] ) <EOL> test_duration_secs = long ( sys . argv [ <NUM_LIT:5> ] ) <EOL> lag_latency_secs = long ( sys . argv [ <NUM_LIT:6> ] ) <EOL> server_list = sys . argv [ <NUM_LIT:7> ] <EOL> this_outstanding = <NUM_LIT:0> <EOL> last_outstanding = <NUM_LIT:0> <EOL> contestant_names = "<STR_LIT>" <EOL> print "<STR_LIT>" % max_votes_per_phone_number <EOL> print "<STR_LIT>" % transactions_per_sec <EOL> print "<STR_LIT>" % client_feedback_interval_secs <EOL> print "<STR_LIT>" % test_duration_secs <EOL> print "<STR_LIT>" % lag_latency_secs <EOL> phone_number = None <EOL> contestant_number = None <EOL> transactions_this_sec = <NUM_LIT:0> <EOL> last_milli = time . time ( ) * <NUM_LIT:1000> <EOL> this_milli = time . time ( ) * <NUM_LIT:1000> <EOL> volt_servers = server_list . rsplit ( "<STR_LIT:U+002C>" ) <EOL> client = FastSerializer ( volt_servers [ <NUM_LIT:0> ] ) <EOL> initprocedure = VoltProcedure ( client , "<STR_LIT>" , [ FastSerializer . VOLTTYPE_INTEGER , FastSerializer . VOLTTYPE_STRING ] ) <EOL> response = initprocedure . call ( [ max_contestant , contestant_names ] ) <EOL> start_time = time . time ( ) <EOL> end_time = start_time + test_duration_secs <EOL> current_time = start_time <EOL> last_feedback_time = start_time <EOL> num_sp_calls = <NUM_LIT:0> <EOL> start_recording_latency = start_time + lag_latency_secs <EOL> thread_list = [ ] <EOL> for x in range ( <NUM_LIT:5> ) : <EOL> thread = doQueries ( volt_servers [ x % len ( volt_servers ) ] ) <EOL> thread . setDaemon ( True ) <EOL> thread . start ( ) <EOL> while end_time > current_time : <EOL> availableThreads . acquire ( ) <EOL> num_sp_calls = num_sp_calls + <NUM_LIT:1> <EOL> phone_number = random . randint ( <NUM_LIT> , <NUM_LIT> ) <EOL> contestant_number = ( int ( random . random ( ) * max_contestant ) * int ( random . random ( ) * max_contestant ) ) % max_contestant + <NUM_LIT:1> <EOL> if num_sp_calls % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> contestant_number = ( int ( random . random ( ) * max_contestant ) + <NUM_LIT:1> ) * <NUM_LIT:2> <EOL> params_lock . acquire ( ) <EOL> invocation_params . append ( [ phone_number , contestant_number ] ) <EOL> params_lock . release ( ) <EOL> requestPermit . release ( ) <EOL> transactions_this_sec = transactions_this_sec + <NUM_LIT:1> <EOL> if transactions_this_sec >= transactions_per_milli : <EOL> this_milli = time . time ( ) * <NUM_LIT:1000> <EOL> while this_milli <= last_milli : <EOL> this_milli = time . time ( ) * <NUM_LIT:1000> <EOL> time . sleep ( <NUM_LIT:0> ) <EOL> last_milli = this_milli <EOL> transactions_this_sec = <NUM_LIT:0> <EOL> current_time = time . time ( ) <EOL> if not check_latency and current_time >= start_recording_latency : <EOL> check_latency = True <EOL> if current_time >= ( last_feedback_time + client_feedback_interval_secs ) : <EOL> elapsed_time_secs_2 = time . time ( ) - start_time <EOL> last_feedback_time = current_time <EOL> run_time_secs = end_time - start_time <EOL> if tot_executions_latency == <NUM_LIT:0> : <EOL> tot_executions_latency = <NUM_LIT:1> <EOL> percent_complete = ( float ( elapsed_time_secs_2 ) / float ( run_time_secs ) ) * <NUM_LIT:100> <EOL> if percent_complete > <NUM_LIT:100> : <EOL> percent_complete = <NUM_LIT:100> <EOL> results_lock . acquire ( ) <EOL> this_outstanding = num_sp_calls - tot_executions <EOL> avg_latency = float ( tot_execution_secs ) * <NUM_LIT:1000> / float ( tot_executions_latency ) <EOL> print "<STR_LIT>" % ( percent_complete , num_sp_calls , ( float ( num_sp_calls ) / float ( elapsed_time_secs_2 ) ) , this_outstanding , ( this_outstanding - last_outstanding ) , ( min_execution_secs * <NUM_LIT:1000> ) , ( max_execution_secs * <NUM_LIT:1000> ) , avg_latency ) <EOL> last_outstanding = this_outstanding <EOL> results_lock . release ( ) <EOL> shouldContinue = False <EOL> for thread in thread_list : <EOL> if thread . isAlive ( ) : <EOL> thread . join ( ) <EOL> elapsed_time_secs = time . time ( ) - start_time <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % vote_result_counter [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" % vote_result_counter [ <NUM_LIT:1> ] <EOL> print "<STR_LIT>" % vote_result_counter [ <NUM_LIT:2> ] <EOL> print <EOL> winner_name = "<STR_LIT>" <EOL> winner_votes = - <NUM_LIT:1> <EOL> resultsprocedure = VoltProcedure ( client , "<STR_LIT>" , [ ] ) <EOL> response = resultsprocedure . call ( [ ] ) <EOL> table = response . tables [ <NUM_LIT:0> ] <EOL> if len ( table . tuples ) == <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> else : <EOL> for row in table . tuples : <EOL> result_name = row [ <NUM_LIT:0> ] <EOL> result_votes = row [ <NUM_LIT:2> ] <EOL> print "<STR_LIT>" % ( result_name , result_votes ) <EOL> if result_votes > winner_votes : <EOL> winner_votes = result_votes <EOL> winner_name = result_name <EOL> print <EOL> print "<STR_LIT>" % ( winner_name , winner_votes ) <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % elapsed_time_secs <EOL> print "<STR_LIT>" % num_sp_calls <EOL> print "<STR_LIT>" % ( num_sp_calls / elapsed_time_secs ) <EOL> print "<STR_LIT>" % ( float ( tot_execution_secs ) * <NUM_LIT:1000> / float ( tot_executions_latency ) ) <EOL> print "<STR_LIT>" % latency_counter [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" % latency_counter [ <NUM_LIT:1> ] <EOL> print "<STR_LIT>" % latency_counter [ <NUM_LIT:2> ] <EOL> print "<STR_LIT>" % latency_counter [ <NUM_LIT:3> ] <EOL> print "<STR_LIT>" % latency_counter [ <NUM_LIT:4> ] <EOL> print "<STR_LIT>" % latency_counter [ <NUM_LIT:5> ] <EOL> print "<STR_LIT>" % latency_counter [ <NUM_LIT:6> ] <EOL> print "<STR_LIT>" % latency_counter [ <NUM_LIT:7> ] <EOL> print "<STR_LIT>" % latency_counter [ <NUM_LIT:8> ] <EOL> class doQueries ( threading . Thread ) : <EOL> def __init__ ( self , server ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . client = FastSerializer ( server ) <EOL> self . proc = VoltProcedure ( self . client , "<STR_LIT>" , [ FastSerializer . VOLTTYPE_BIGINT , FastSerializer . VOLTTYPE_TINYINT , FastSerializer . VOLTTYPE_BIGINT ] ) <EOL> def run ( self ) : <EOL> global vote_result_counter <EOL> global tot_executions <EOL> global tot_executions_latency <EOL> global tot_execution_secs <EOL> global check_latency <EOL> global latency_counter <EOL> global min_execution_secs <EOL> global max_execution_secs <EOL> global requestPermit <EOL> global shouldContinue <EOL> global max_contestant <EOL> global results_lock <EOL> global params_lock <EOL> global availableThreads <EOL> global invocation_params <EOL> num_sp_calls = <NUM_LIT:0> <EOL> while shouldContinue : <EOL> availableThreads . release ( ) ; <EOL> requestPermit . acquire ( ) <EOL> num_sp_calls = num_sp_calls + <NUM_LIT:1> <EOL> params_lock . acquire ( ) ; <EOL> params = invocation_params . pop ( ) <EOL> params_lock . release ( ) <EOL> time_before = time . time ( ) <EOL> response = self . proc . call ( [ params [ <NUM_LIT:0> ] , params [ <NUM_LIT:1> ] , max_contestant ] ) <EOL> time_after = time . time ( ) <EOL> if response . status != <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> exit ( - <NUM_LIT:1> ) <EOL> else : <EOL> results_lock . acquire ( ) <EOL> tot_executions = tot_executions + <NUM_LIT:1> <EOL> table = response . tables [ <NUM_LIT:0> ] <EOL> vote_result = table . tuples [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> vote_result_counter [ vote_result ] = vote_result_counter [ vote_result ] + <NUM_LIT:1> <EOL> if check_latency : <EOL> execution_time = time_after - time_before <EOL> tot_executions_latency = tot_executions_latency + <NUM_LIT:1> <EOL> tot_execution_secs = tot_execution_secs + execution_time <EOL> if execution_time < min_execution_secs : <EOL> min_execution_secs = execution_time <EOL> if execution_time > max_execution_secs : <EOL> max_execution_secs = execution_time <EOL> latency_bucket = int ( execution_time * <NUM_LIT> ) <EOL> if latency_bucket > <NUM_LIT:8> : <EOL> latency_bucket = <NUM_LIT:8> <EOL> latency_counter [ latency_bucket ] = latency_counter [ latency_bucket ] + <NUM_LIT:1> <EOL> results_lock . release ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
