def send_rest_install_req(records_queue, account, url_tab):
ns = NotificationSimu(0, account,url_tab)
ns.send_rest_install_profile_req(records_queue, account)
pass
def _read_device_data():
#Get out devices info. from txt file
fd = open(DEVICES_DATA, 'r')
devices = fd.readlines()
fd.close()
return devices
pass
def _init_url_tab():
parser = ConfigParser.ConfigParser()
parser.read(CFG_DATA)
url_list = []
url_list.append(parser.get("Notification Server","REST_InstallProfile"))
url_list.append(parser.get("Notification Server","REST_InstallProfile2"))
url_list.append(parser.get("Notification Server","REST_InstallProfile3"))
url_list.append(parser.get("Notification Server","REST_InstallProfile4"))
install_url_tab = tuple(url_list)
return install_url_tab
pass
def multiple_process_installprofile():
#Read devices data
devices = _read_device_data()
#init url tab
url_tab = _init_url_tab()
#new settings
records_max = 80000
threads_max = 100
step_multiple = 4
accounts_idx = 0
records_queue_list = []
#so...
records_per_thread = records_max /threads_max
if not (records_per_thread %step_multiple == 0):
print 'Warning:%d records are not match setp multiple %d!' % (records_per_thread,step_multiple)
sys.exit(1)
else:
#set up queuesfor each thread, and it will hold devices' records
for i inxrange(0, threads_max):
queue = Queue.Queue(records_per_thread) #set max size
start_pos = i * records_per_thread
if start_pos > records_max:
print 'Warning: Reach the max records!'
break
end_pos = start_pos + records_per_thread - 1
if end_pos > records_max:
end_pos = records_max
print 'Thread %d: devices index: %d - %d!' % (i, start_pos,end_pos)
for pos in xrange(start_pos, end_pos):
queue.put(devices[pos])
records_queue_list.append(queue)
del devices[:] #free memory
starttime = time.time()
stime = datetime.now()
output = "mdmi RESTsimulator Start time : %s\n" %stime
print output
perf_log_content = output
thread_list = []
for i in xrange(0, threads_max):
work_thread =threading.Thread(target=send_rest_install_req, args=(records_queue_list[i],accounts_idx, url_tab, ));
work_thread.start()
thread_list.append(work_thread)
accounts_idx =(accounts_idx + 1) % 4
for working_thread in thread_list:
working_thread.join();
endtime = time.time()
etime = datetime.now()
output = "mdmi REST simulator Endtime : %s\n" % etime
print output
perf_log_content = perf_log_content +output
sec_cost = endtime - starttime
output = "mdmi REST simulaotr sent%d request with time cost: %d seconds.\n" % (records_max, sec_cost)
print output
perf_log_content = perf_log_content +output
output = '-'*40 + '\n'
print output
perf_log_content = perf_log_content +output
save_perf_log(SIMU_LOG, perf_log_content)
for queue in records_queue_list:
while notqueue.empty():
queue.get()
pass
if __name__ == '__main__':
multiple_process_installprofile()