-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleClient.py
131 lines (102 loc) · 3.52 KB
/
SimpleClient.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
130
131
"""
Client
@author: James Smith (jws7) @ St Andrews
"""
# Allow command line args
import sys
# Allow sleeping
import time
# Allow multi-threading
import threading
# Time/Date object
import datetime
# Import HTTPClients
from HTTPClient import *
class SimpleClient(threading.Thread):
kill_received = False # Boolean to allow interruption
MAX_THREAD_COUNT = 1 # Maxium number of threads to run
# Thread list - hold the currently running HTTP Clients
threads = []
# Override constructor
def __init__(self, number):
# Call superclass constructo
threading.Thread.__init__(self)
print '[Client:Constructor:] in method Constructor'
# Set Thread number
self.MAX_THREAD_COUNT = number
# A flag to notify the thread that it should finish up and exit
self.kill_received = False
# Create list
self.threads = []
# Method to kill all currently running threads
def killThreads(self):
# kill current threads
print 'Killing all active threads'
for th in self.threads:
th.killself = True # set flag and threads will kill their self
# Threaded method - default calls the behaviour method (which can be overriden)
def run ( self ):
# Print the maximum thread count
print 'MAX_THREAD_COUNT: ' + str(self.MAX_THREAD_COUNT)
# Create threads
number = self.MAX_THREAD_COUNT - len(self.threads)
print 'Creating ' + str(number) + ' active threads'
for i in xrange(number):
# Start thread and add to list
th = HTTPClient()
th.start()
self.threads.append(th)
# Loop until interrupted by Ctrl + C
while not self.kill_received:
# Print current date and time and current thread count
now = datetime.datetime.now()
print "[Client:run] Current date and time:" + str(now)
print "[Client:run] Current concurrent thread count:" + str(len(self.threads))
# Get & reset counters
getsPerMinute = 0
postsPerMinute = 0
for th in self.threads:
getsPerMinute += th.getReqs
th.getReqs = 0
postsPerMinute += th.postReqs
th.postReqs = 0
getsPerMinute = getsPerMinute / len(self.threads)
postsPerMinute = postsPerMinute / len(self.threads)
print "[Client:run] Operating at " + str(getsPerMinute) + " GET/m and " + str(postsPerMinute) + " POST/m"
# Repeat for bandwidth
rxBytes = 0
txBytes = 0
for th in self.threads:
rxBytes += th.rxBytes
th.rxBytes = 0
txBytes += th.txBytes
th.txBytes = 0
rxBytes = rxBytes / len(self.threads)
txBytes = txBytes / len(self.threads)
#rxBytes = rxBytes / 1000000
#txBytes = txBytes / 1000000
print "[Client:run] Downloading at " + str(rxBytes) + " B/m and Uploading at " + str(txBytes) + " B/m"
# Have a snooze...
time.sleep(60)
# Create a Client class and start it running
def main():
# Setup number of threads default
number = 2
# Adjust if given a command line argument
if (len(sys.argv) > 1):
number = int(sys.argv[1])
print 'Starting client with ' + str(number)
client = SimpleClient(number) # Call Constructor
client.start() # Start thread
# Enable keyboard interrupts
while not client.kill_received:
try:
client.join(1)
except KeyboardInterrupt:
print "Ctrl-c received! Sending kill to threads..."
# and all HTTP clients
client.killThreads()
# and the main client
client.kill_received = True
if __name__ == '__main__':
main()