-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_test.py
executable file
·93 lines (73 loc) · 2.34 KB
/
load_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
from Queue import Queue
import threading
import os
import time
import subprocess
import logging as log
def operation(name):
command = './load_action.sh'
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.communicate()
def loader(inq, outq, errq):
while True:
tupl = inq.get()
if tupl is None:
inq.task_done()
break
name = tupl
start = time.time()
operation(name)
duration = time.time() - start
outq.put((duration, name))
inq.task_done()
return
def load(name_list, con_num):
(outq, errq,) = run_concurrently(loader, name_list, con_num)
times = []
names = []
while not outq.empty():
t, n = outq.get()
times.append(t)
names.append(n)
outq.task_done()
return times
def run_concurrently(func, seq, threads_number):
# Accepts:
# func - a function or a bound method which is to take 3 parameters:
# input_queue, output_queue, error_queue;
# seq - a sequence of items that are put into the input queue;
# threads_number - concurrency factor, the number of threads to use
# in a pool.
# Returns output and error queues, correspondingly.
inq, outq, errq, = Queue(), Queue(), Queue()
# Spawn thread pool
workers = []
for i in range(threads_number):
worker = threading.Thread(target=func, args=(inq, outq, errq))
worker.daemon = False
worker.start()
workers.append(worker)
# Place work in queue
for item in seq:
inq.put(item)
# Threads wait 'None' for exit
for i in range(threads_number):
inq.put(None)
# Wait until worker threads are done to exit, then terminate
#inq.join()
for worker in workers:
worker.join()
return (outq, errq,)
if __name__ == '__main__':
log.basicConfig(level=log.DEBUG)
dir_path = os.path.dirname(os.path.realpath(__file__))
f = open(os.path.join(dir_path, 'token'), 'r')
token = f.read() #'310046588:AAGqktDy4wf71g-wpZD_H84JTJLY7nOD9b8'
glob_avg = []
for i in range(50):
stat = load(map(str, range(100)), i+1)
avg = sum(stat)/len(stat)
glob_avg.append((i+1, avg))
import pprint
pprint.pprint(glob_avg)