-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrtsloadtest.py
More file actions
228 lines (184 loc) · 6.6 KB
/
rtsloadtest.py
File metadata and controls
228 lines (184 loc) · 6.6 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'''
After deploying your Web Service, you can use this program to load test the endpoint.
Read the function loadArguments() to determine what parameters to pass in.
Flow:
1. Create and start t number of threads where t is identified in the parameters.
Each thread will run for i iterations (calls to the endpoint) where i is identified
in the parameters.
2. Upon completion of all threads collect statistics.
- All up stats on all calls from all threads
- Individual thread statistics
- Each stats bundle contains
Total Number of Calls
Succesful Number of Calls
Average Latency (seconds)
Min Latency (seconds)
Max Latency (seconds)
3. Print the results to the console.
'''
from threading import *
from datetime import datetime, timedelta
import sys
import argparse
import time
import requests
import json
import collections
import random
import statistics
# Named tuple to collect from each call
test_point = collections.namedtuple('test_point', 'thread status elapsed')
# Collection of each call made
test_collection = []
# Global lock to protect collection
test_collection_lock = RLock()
# Counter of running threads
running_threads = 0
# Headers for every call
api_headers = {}
def loadArguments(sys_args):
'''
Load arguments for the program:
u = URL of the Web Service URL
k = API Key for the web service
t = Number of threads to run
i = Number of calls to make per thread.
'''
global api_headers
parser = argparse.ArgumentParser(description='Simple model deployment.')
parser.add_argument("-u", required=False, default='http://52.151.239.210:80/api/v1/service/cmkservice2/score', type=str, help="Web Service URI")
parser.add_argument("-k", required=False, default="oZ67iku9ddYtkJGYwGGNCZc2psT27qoC", type=str, help="Web Service Key")
parser.add_argument("-t", required=False, default=20, type=int, help="Thread Count")
parser.add_argument("-i", required=False, default=1, type=int, help="Thread Iterations")
prog_args = parser.parse_args(sys_args)
api_headers["Authorization"] = "Bearer " + prog_args.k
api_headers["Content-Type"] = "application/json"
return prog_args
def dumpStats(stats):
'''
Dump out a dictionary of stats
'''
for key in stats.keys():
print(" ", key, "=", stats[key])
def getStatistics(collection):
'''
From a collection of test_point objects collect the following
- T0tal Calls
- Succesful calls
- Average latency
- Min latency
- Maximum Latency
'''
stats = {}
success = [x for x in collection if x.status == 200]
times = [x.elapsed for x in collection ]
stats["calls"] = len(collection)
stats["success"] = len(success)
stats["average"] = statistics.mean(times)
stats["min"] = min(times)
stats["max"] = max(times)
return stats
def getThreadStatistics():
'''
Load statistics of the run. Two items are returned as a list
[0] = Dictionary of global stats
[1] = Dictionary of dictionaries for each thread.
'''
global test_collection
'''
Get stats across threads
'''
global_stats = getStatistics(test_collection)
'''
Get individual stats
'''
thread_stats = {}
thread_ids = [x.thread for x in test_collection]
thread_ids = list(set(thread_ids))
for tid in thread_ids:
thread_stats[tid] = {}
thread_collection = [x for x in test_collection if x.thread == tid]
thread_stats[tid] = getStatistics(thread_collection)
return [global_stats, thread_stats]
class ThreadRun(Thread):
'''
Class used as a thread to run the load test against the
endpoint.
'''
def __init__(self, id, iterations, url, headers, payload):
Thread.__init__(self)
self.id = id
self.iterations = iterations
self.url = url
self.headers = headers
self.payload = payload
'''
Calling start() runs this as well, but when you queue a thread it will
trigger this as well.
'''
def run(self):
global running_threads
global test_collection
global test_collection_lock
print("Staring thread", self.id)
for i in range(self.iterations):
try:
response = requests.post(url = self.url, headers = self.headers, data = json.dumps(self.payload))
current_test = test_point( self.id, response.status_code, response.elapsed.total_seconds())
except Exception as ex:
print(self.id, ex)
print(str(ex))
current_test = test_point( self.id, 500, 1)
test_collection_lock.acquire()
test_collection.append(current_test)
test_collection_lock.release()
test_collection_lock.acquire()
running_threads -= 1
test_collection_lock.release()
'''
Program Code:
The configured number of threads will be executed for the configured number of iterations each
hitting the endpoint.
This can be used for any number of AMLS endpoints, with the real change being to the payload that is set
into the thread class to perform the execution.
'''
'''
Using the configuration, fire up as many threads as we need.
'''
configuration = loadArguments(sys.argv[1:])
names = ["Dave", "Sue", "Dan", "Joe", "Beth"]
# Capture the start time.
start_time = datetime.now()
for i in range(configuration.t):
payload = {'name' : names[random.randint(0, len(names) -1)]}
run = ThreadRun(i+1, configuration.i, configuration.u, api_headers, payload)
# Increase the thread counter
test_collection_lock.acquire()
running_threads += 1
test_collection_lock.release()
# Start the worker thread.
run.start()
'''
Wait until all threads complete.
'''
counter = 0
while running_threads > 0:
counter += 1
if counter %3 == 0:
print("Waiting on threads, current count =", running_threads)
time.sleep(.5)
# Capture the start time.
end_time = datetime.now()
total_seconds = (end_time - start_time).total_seconds()
print(total_seconds)
'''
Get and print out the statistics for this run.
'''
stats = getThreadStatistics()
print("Global Stats:")
print(" Total Time : ", total_seconds )
print(" Overall RPS : ", stats[0]["calls"] / total_seconds )
dumpStats(stats[0])
for thread_id in stats[1].keys():
print("Thread", thread_id, "Stats:")
dumpStats(stats[1][thread_id])