-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
118 lines (94 loc) · 3.57 KB
/
client.py
File metadata and controls
118 lines (94 loc) · 3.57 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
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import time
from collections import deque
import threading
# 1. We are ignoring Caching here
# 2. Only One EXTRA Thread for Client Function used
# Final Request
class Request:
"""Request Object of method: GET POST CONST RAND\n"""
def __init__(self, method = 'RAND'):
self.granted = False
self._method = method
self.size = None
if self._method == 'GET':
self.size = np.random.randint(1, 5)
elif self._method == 'POST':
self.size = np.random.randint(3, 10)
elif self._method == 'CONST':
self.size = 4
elif self._method == 'RAND':
self._method = np.random.choice(('GET', 'POST'))
self.__init__(self._method)
self.src_ip = '.'.join(tuple(map(str,np.random.randint(0, 255, 4))))
def __del__(self):
#print("Request Complete .. Being deleted.")
pass
def __repr__(self):
return f"Request('{self._method}')"
def __str__(self):
return f"Request Object\nMethod: {self._method}\nSize: {self.size}\nGranted: {self.granted}"
# ## Final Client
class Client:
"""Fires Requests towards the Load Balancer"""
def __init__(self, deq):
self._flag = 0
self._deq = deq
self._current = None
def _uniform(self, r_type, rps : int):
"""Uniformly fires Requests of type rtype"""
sleep_duration = 1 / rps
while self._flag == 2:
new_r = Request(r_type)
#print("U")
self._deq.appendleft(new_r)
time.sleep(sleep_duration*0.99)
def _random(self, r_type, rps : int):
"""Fires Requests Randomly of type rtype : On average equals rps"""
sleep_duration = 1 / rps
while self._flag == 1:
fill = min(2*sleep_duration, abs(0.5 * np.random.randn() + sleep_duration))
#print("R")
new_r = Request(r_type)
self._deq.appendleft(new_r)
time.sleep(fill)
def _single(self, r_type):
"""Fires Single Request of type rtype"""
new_r = Request(r_type)
self._deq.appendleft(new_r)
def _fire(self, method, r_type, rps):
if method == 'stop':
self._flag = 0
if self._current:
self._current.join()
self._current = None
elif method == 'uniform':
self._flag = -1
if self._current:
self._current.join()
self._flag = 2
self._current = threading.Thread(target = self._uniform, args = (r_type, rps))
self._current.start()
elif method == "random":
self._flag = -1
if self._current:
self._current.join()
self._flag = 1
self._current = threading.Thread(target = self._random, args = (r_type, rps))
self._current.start()
elif method == 'single':
self._flag = -1
if self._current:
self._current.join()
self._flag = 3
self._current = None
self._single(r_type) # Need thread here ?
def fire(self, method, rtype = "RAND", rps = 5):
"""method: Type of firing -> 'uniform', 'random', 'single'\n
rtype: 'RAND', 'GET', 'POST', 'CONST'\n\tdefault RAND\n
rps: int\n\tdefault 5"""
self._fire(method, rtype, rps)
def __str__(self):
return f"Client Simulator for Deque"