-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.py
129 lines (102 loc) · 3.23 KB
/
simulator.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import threading
from time import sleep
from delay import Delay
import requests
class Simulator:
def __init__ (self, cv):
"""
Initiating the Simulator
"""
self.cv = cv
def stop (self):
self.cv.running = False
def start_simulator(self, total, parallel, running, url, json, headers):
"""
Start the simulator by setting running flag to True &
by invoking run() method
"""
print ("Started")
self.run(total, parallel, running, url, json, headers)
return
def run(self, total, parallel, running, url, json, headers):
self.cv.current_counter = 1
started_threads = []
"""
Loop until all threads are submitted OR
simulator is stopped explicitely
"""
print ("Inside run. Running Flag: {}. Total: {}".format(running(), total()))
while running() and self.cv.current_counter <= total():
self.cv.current_parallel = len(started_threads)
print ("Target: {}. Current: {}. Parallel: {}. Current: {}".format(total(), self.cv.current_counter, parallel(), self.cv.current_parallel))
# Wait for a chance before starting new thread
self.wait_till_I_get_a_chance(started_threads, parallel)
# Okay. Good to go. Start a new thread.
d = Delay(self.cv)
t = threading.Thread(target=d.hit_a_url, args=(self.cv.url, self.cv.json, self.cv.headers))
t.daemon = True
started_threads.append(t)
t.start()
# Counter goes up
self.cv.current_counter+=1
if running():
print ("Submitted all {} jobs.".format(total()))
print ("Let's wait till the submitted jobs to finish")
"""
Now simulator started all the jobs OR got interrupted by user
If interrupted by the user, threads will be stopped anyway.
Just wait for all threads to stop gracefully
"""
self.wait_till_threads_complete(started_threads)
print ("All done.")
self.cv.running = False
print ("Current running status inside simulator: {}".format(self.cv.running))
return
def wait_till_I_get_a_chance(self, started_threads, parallel):
"""
Loops inifinitely till the number of theads running is less than
expected parallel running count
"""
while True:
# print ("Current queue size: {}".format(len(started_threads)))
if len(started_threads) < parallel():
self.cv.current_parallel = len(started_threads)
# print ("Cool. Under the limit")
# Okay. Now the parent program can start a new thread
return
else:
# print ("Queue is full. Take a nap")
sleep(0.25)
started_threads=self.remove_finished_threads(started_threads)
def remove_finished_threads(self, started_threads):
"""
Check each thread in the list for status
and remove those which are finished already
"""
for t in started_threads:
if not t.is_alive():
started_threads.remove(t)
return started_threads
def wait_till_threads_complete(self, started_threads):
"""
Wait till all the threads finish
"""
if len(started_threads) > 0:
for t in started_threads:
t.join()
if __name__ == "__main__":
url = "http://127.0.0.1:8081/delay"
json = '{"name":"Gireesh2"}'
headers = ""
r = requests.post (url=url, json=json, headers=headers)
print (r.json())
class ControlVariables():
total_threads = 50
parallel_threads = 5
running = False
current_counter = 0
current_parallel = 0
url = ""
json = None
headers = None
output = ""