<s> import numpy <EOL> import numpy . random <EOL> import random <EOL> import json <EOL> import time <EOL> from datetime import datetime <EOL> import requests <EOL> from scipy . linalg import norm <EOL> import time <EOL> from multiprocessing import Pool <EOL> import os <EOL> HOSTNAME = os . environ . get ( '<STR_LIT>' , '<STR_LIT:localhost>' ) + '<STR_LIT::>' + os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def run_all ( assert_200 ) : <EOL> app_id = '<STR_LIT>' <EOL> num_objects = <NUM_LIT:30> <EOL> desired_dimension = <NUM_LIT:2> <EOL> x = numpy . linspace ( <NUM_LIT:0> , <NUM_LIT:1> , num_objects ) <EOL> X_true = numpy . vstack ( [ x , x ] ) . transpose ( ) <EOL> total_pulls_per_client = <NUM_LIT:100> <EOL> num_experiments = <NUM_LIT:1> <EOL> num_clients = <NUM_LIT:30> <EOL> pool = Pool ( processes = num_clients ) <EOL> n = num_objects <EOL> d = desired_dimension <EOL> delta = <NUM_LIT> <EOL> supported_alg_ids = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> alg_list = [ ] <EOL> for idx , alg_id in enumerate ( supported_alg_ids ) : <EOL> alg_item = { } <EOL> alg_item [ '<STR_LIT>' ] = alg_id <EOL> if idx == <NUM_LIT:0> : <EOL> alg_item [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> alg_item [ '<STR_LIT>' ] = alg_id <EOL> alg_item [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> alg_item [ '<STR_LIT>' ] = { } <EOL> alg_list . append ( alg_item ) <EOL> params = { } <EOL> params [ '<STR_LIT>' ] = [ ] <EOL> for algorithm in alg_list : <EOL> params [ '<STR_LIT>' ] . append ( { '<STR_LIT>' : algorithm [ '<STR_LIT>' ] , '<STR_LIT>' : <NUM_LIT:1.> / len ( alg_list ) } ) <EOL> algorithm_management_settings = { } <EOL> algorithm_management_settings [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> algorithm_management_settings [ '<STR_LIT>' ] = params <EOL> initExp_args_dict = { } <EOL> initExp_args_dict [ '<STR_LIT:args>' ] = { } <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT:n>' ] = n <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT:d>' ] = d <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] = delta <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] = algorithm_management_settings <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] = alg_list <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] = '<STR_LIT:text>' <EOL> initExp_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> initExp_args_dict [ '<STR_LIT>' ] = app_id <EOL> initExp_args_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> initExp_args_dict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> exp_info = [ ] <EOL> for ell in range ( num_experiments ) : <EOL> url = "<STR_LIT>" + HOSTNAME + "<STR_LIT>" <EOL> response = requests . post ( url , json . dumps ( initExp_args_dict ) , headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } ) <EOL> print "<STR_LIT>" , response . text , response . status_code <EOL> if assert_200 : assert response . status_code is <NUM_LIT:200> <EOL> initExp_response_dict = json . loads ( response . text ) <EOL> exp_uid = initExp_response_dict [ '<STR_LIT>' ] <EOL> exp_key = initExp_response_dict [ '<STR_LIT>' ] <EOL> exp_info . append ( { '<STR_LIT>' : exp_uid , '<STR_LIT>' : exp_key } ) <EOL> url = "<STR_LIT>" + HOSTNAME + "<STR_LIT>" + exp_uid + "<STR_LIT:/>" + exp_key <EOL> response = requests . get ( url ) <EOL> print "<STR_LIT>" , response . text , response . status_code <EOL> if assert_200 : assert response . status_code is <NUM_LIT:200> <EOL> initExp_response_dict = json . loads ( response . text ) <EOL> participants = [ ] <EOL> pool_args = [ ] <EOL> for i in range ( num_clients ) : <EOL> participant_uid = '<STR_LIT>' % random . randrange ( <NUM_LIT:16> ** <NUM_LIT:30> ) <EOL> participants . append ( participant_uid ) <EOL> experiment = numpy . random . choice ( exp_info ) <EOL> exp_uid = experiment [ '<STR_LIT>' ] <EOL> exp_key = experiment [ '<STR_LIT>' ] <EOL> pool_args . append ( ( exp_uid , exp_key , participant_uid , total_pulls_per_client , X_true , assert_200 ) ) <EOL> results = pool . map ( simulate_one_client , pool_args ) <EOL> for result in results : <EOL> print result <EOL> def simulate_one_client ( input_args ) : <EOL> exp_uid , exp_key , participant_uid , total_pulls , X_true , assert_200 = input_args <EOL> avg_response_time = <NUM_LIT:1.> <EOL> getQuery_times = [ ] <EOL> processAnswer_times = [ ] <EOL> for t in range ( total_pulls ) : <EOL> print t , participant_uid <EOL> getQuery_args_dict = { } <EOL> getQuery_args_dict [ '<STR_LIT>' ] = exp_uid <EOL> getQuery_args_dict [ '<STR_LIT>' ] = exp_key <EOL> getQuery_args_dict [ '<STR_LIT:args>' ] = { } <EOL> getQuery_args_dict [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] = participant_uid <EOL> url = '<STR_LIT>' + HOSTNAME + '<STR_LIT>' <EOL> response , dt = timeit ( requests . post ) ( url , json . dumps ( getQuery_args_dict ) , headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } ) <EOL> print "<STR_LIT>" , response . text , response . status_code <EOL> if assert_200 : assert response . status_code is <NUM_LIT:200> <EOL> print "<STR_LIT>" , dt <EOL> getQuery_times . append ( dt ) <EOL> print <EOL> query_dict = json . loads ( response . text ) <EOL> query_uid = query_dict [ '<STR_LIT>' ] <EOL> targets = query_dict [ '<STR_LIT>' ] <EOL> print targets <EOL> for target in targets : <EOL> if target [ '<STR_LIT:label>' ] == '<STR_LIT>' : <EOL> index_center = target [ '<STR_LIT:index>' ] <EOL> elif target [ '<STR_LIT:label>' ] == '<STR_LIT:left>' : <EOL> index_left = target [ '<STR_LIT:index>' ] <EOL> elif target [ '<STR_LIT:label>' ] == '<STR_LIT:right>' : <EOL> index_right = target [ '<STR_LIT:index>' ] <EOL> ts = time . time ( ) <EOL> time . sleep ( avg_response_time * numpy . log ( <NUM_LIT:1.> / numpy . random . rand ( ) ) ) <EOL> direction = norm ( X_true [ index_left ] - X_true [ index_center ] ) - norm ( X_true [ index_right ] - X_true [ index_center ] ) <EOL> r = numpy . random . rand ( ) <EOL> if r < <NUM_LIT> : <EOL> direction = - direction <EOL> if direction < <NUM_LIT:0.> : <EOL> target_winner = index_left <EOL> else : <EOL> target_winner = index_right <EOL> response_time = time . time ( ) - ts <EOL> processAnswer_args_dict = { } <EOL> processAnswer_args_dict [ "<STR_LIT>" ] = exp_uid <EOL> processAnswer_args_dict [ "<STR_LIT>" ] = exp_key <EOL> processAnswer_args_dict [ "<STR_LIT:args>" ] = { } <EOL> processAnswer_args_dict [ "<STR_LIT:args>" ] [ "<STR_LIT>" ] = query_uid <EOL> processAnswer_args_dict [ "<STR_LIT:args>" ] [ "<STR_LIT>" ] = target_winner <EOL> processAnswer_args_dict [ "<STR_LIT:args>" ] [ '<STR_LIT>' ] = response_time <EOL> url = '<STR_LIT>' + HOSTNAME + '<STR_LIT>' <EOL> print "<STR_LIT>" , processAnswer_args_dict <EOL> response , dt = timeit ( requests . post ) ( url , json . dumps ( processAnswer_args_dict ) , headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } ) <EOL> print "<STR_LIT>" , response . text , response . status_code <EOL> if assert_200 : assert response . status_code is <NUM_LIT:200> <EOL> print "<STR_LIT>" , dt <EOL> processAnswer_times . append ( dt ) <EOL> print <EOL> processAnswer_json_response = eval ( response . text ) <EOL> processAnswer_times . sort ( ) <EOL> getQuery_times . sort ( ) <EOL> return_str = '<STR_LIT>' % ( participant_uid , getQuery_times [ int ( <NUM_LIT> * total_pulls ) ] , getQuery_times [ int ( <NUM_LIT> * total_pulls ) ] , getQuery_times [ int ( <NUM_LIT> * total_pulls ) ] , processAnswer_times [ int ( <NUM_LIT> * total_pulls ) ] , processAnswer_times [ int ( <NUM_LIT> * total_pulls ) ] , processAnswer_times [ int ( <NUM_LIT> * total_pulls ) ] ) <EOL> return return_str <EOL> def timeit ( f ) : <EOL> """<STR_LIT>""" <EOL> def timed ( * args , ** kw ) : <EOL> ts = time . time ( ) <EOL> result = f ( * args , ** kw ) <EOL> te = time . time ( ) <EOL> if type ( result ) == tuple : <EOL> return result + ( ( te - ts ) , ) <EOL> else : <EOL> return result , ( te - ts ) <EOL> return timed <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print HOSTNAME <EOL> run_all ( False ) </s>
