-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_script.py
More file actions
141 lines (121 loc) · 4.05 KB
/
Copy pathtest_script.py
File metadata and controls
141 lines (121 loc) · 4.05 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
# this script is used to test scalability of our design
import requests
import time
import matplotlib.pyplot as plt
from threading import Thread
uri_get = "http://127.0.0.1:5000/"
uri_post = "http://127.0.0.1:5000/timetable/room"
test_1_results = []
test_2_results = []
def create_graph(y_values):
x = 0
for value in y_values:
y = value[0]
status_code = value[1]
if status_code == 200:
plt.plot(x, y, 'go')
else:
plt.plot(x, y, 'ro')
x += 1
plt.xlabel("Request")
plt.ylabel("Response time")
return plt
# sends a get request to API
def generate_get(num_request, test_1=False, test_2=False):
for i in range(0, num_request):
start_time = time.time()
response = requests.get(uri_get)
status_code = response.status_code
end_time = time.time()
time_taken = end_time - start_time
result = (time_taken, status_code)
if test_1:
test_1_results.append(result)
elif test_2:
test_2_results.append(result)
return "Test done"
# sends a post request to API
def generate_post(num_request, test_1=False, test_2=False):
for i in range(0, num_request):
start_time = time.time()
response = requests.post(uri_post, data="T101")
status_code = response.status_code
end_time = time.time()
time_taken = end_time - start_time
result = (time_taken, status_code)
if test_1:
test_1_results.append(result)
elif test_2:
test_2_results.append(result)
return "Test done"
# test to run 100 get request each from 10 clients
def test1():
clients = []
# create threads to run clients at same time
# return the time and status code to the graph function
for i in range(0, 10):
clients.append(Thread(target=generate_get, kwargs=dict(num_request=100, test_1=True)))
for client in clients:
client.start()
for client in clients:
client.join()
average = sum([value[0] for value in test_1_results])/len(test_1_results)
print(average)
# call graph function
graph = create_graph(test_1_results)
return graph.show()
# test to run 1000 get request each from 100 clients
def test2():
clients = []
# create threads to run clients at same time
# return the time and status code to the graph function
for i in range(0, 100):
clients.append(Thread(target=generate_get, kwargs=dict(num_request=100, test_2=True)))
for client in clients:
client.start()
for client in clients:
client.join()
average = sum([value[0] for value in test_2_results])/len(test_2_results)
print(average)
# call graph function
graph = create_graph(test_2_results)
return graph.show()
# test to run 100 post request each from 10 clients
def test3():
clients = []
# create threads to run clients at same time
# return the time and status code to the graph function
for i in range(0, 10):
clients.append(Thread(target=generate_post, kwargs=dict(num_request=100, test_1=True)))
for client in clients:
client.start()
for client in clients:
client.join()
average = sum([value[0] for value in test_1_results]) / len(test_1_results)
print(average)
# call graph function
graph = create_graph(test_1_results)
return graph.show()
# test to run 1000 get request each from 100 clients
def test4():
clients = []
# create threads to run clients at same time
# return the time and status code to the graph function
for i in range(0, 100):
clients.append(Thread(target=generate_post, kwargs=dict(num_request=100, test_2=True)))
for client in clients:
client.start()
for client in clients:
client.join()
average = sum([value[0] for value in test_2_results]) / len(test_2_results)
print(average)
# call graph function
graph = create_graph(test_2_results)
return graph.show()
def main():
#test1()
#test2()
#test3()
test4()
if __name__ == "__main__":
main()