forked from SvenskaSpel/locust-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_pools.py
150 lines (118 loc) · 5.01 KB
/
connection_pools.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Connection pools can be used to pad out the number of ports occupied by each user.
This is potentially useful in low user count high throughput scenarios, where the
target load balancer may not behaving as expected.
Follow up information on Azure:
https://docs.microsoft.com/en-us/azure/load-balancer/distribution-mode-concepts
Follow up information on AWS:
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/load-balancer-types.html
"""
from locust import FastHttpUser, HttpUser, events
from locust.clients import HttpSession
from locust.contrib.fasthttp import FastHttpSession
from itertools import cycle
from argparse import ArgumentParser
from typing import Optional, List
@events.init_command_line_parser.add_listener
def _(parser: ArgumentParser):
parser.add_argument(
"--pool_size",
type=int,
env_var="POOL_SIZE",
default=20,
help="inflate the number of connections that a user uses",
)
class FastHttpPool:
def __init__(self, *, user: FastHttpUser, size: Optional[int] = None):
"""
Connection pool for the FastHttpUser, the user will use the connections
in round robin style for every HTTP request.
Args:
user (FastHttpUser): Instance of a FastHttpUSer, pass in self in most circumstances
size (int, optional): [description]. Override for the pool size, value defaults to
the input from the command line.
"""
if size:
self.size = size
else:
self.size = user.environment.parsed_options.pool_size # type: ignore
self._pool: List[FastHttpSession] = []
for _ in range(self.size):
self._pool.append(
FastHttpSession(
user.environment, # type: ignore
base_url=user.host,
network_timeout=user.network_timeout,
connection_timeout=user.connection_timeout,
max_redirects=user.max_redirects,
max_retries=user.max_retries,
insecure=user.insecure,
concurrency=user.concurrency,
user=user,
)
)
self.pool = cycle(self._pool)
def delete(self, path, **kwargs):
"""sends a DELETE request"""
return next(self.pool).delete(path, **kwargs)
def get(self, path, **kwargs):
"""Sends a GET request"""
return next(self.pool).get(path, **kwargs)
def head(self, path, **kwargs):
"""Sends a HEAD request"""
return next(self.pool).head(path, **kwargs)
def options(self, path, **kwargs):
"""Sends a OPTIONS request"""
return next(self.pool).options(path, **kwargs)
def patch(self, path, data=None, **kwargs):
"""Sends a PATCH request"""
return next(self.pool).patch(path, data=data, **kwargs)
def post(self, path, data=None, **kwargs):
"""Sends a POST request"""
return next(self.pool).post(path, data=data, **kwargs)
def put(self, path, data=None, **kwargs):
"""Sends a PUT request"""
return next(self.pool).put(path, data=data, **kwargs)
class RequestPool:
def __init__(self, *, user: HttpUser, size: Optional[int] = None):
"""
Connection pool for the HttpUser, the user will use the connections
in round robin style for every HTTP request.
Args:
user (HttpUser): Instance of a HttpUser, pass in self in most circumstances
size (int, optional): [description]. Override for the pool size, value defaults to
the input from the command line.
"""
if size:
self.size = size
else:
self.size = user.environment.parsed_options.pool_size # type: ignore
self._pool: List[HttpSession] = []
for _ in range(self.size):
self._pool.append(
HttpSession(
base_url=user.host, request_event=user.environment.events.request, user=user # type: ignore
)
)
self.pool = cycle(self._pool)
def delete(self, url, **kwargs):
"""sends a DELETE request"""
return next(self.pool).delete(url=url, **kwargs)
def get(self, url, **kwargs):
"""Sends a GET request"""
return next(self.pool).get(url=url, **kwargs)
def head(self, url, **kwargs):
"""Sends a HEAD request"""
return next(self.pool).head(url=url, **kwargs)
def options(self, url, **kwargs):
"""Sends a OPTIONS request"""
return next(self.pool).options(url=url, **kwargs)
def patch(self, url, data=None, **kwargs):
"""Sends a PATCH request"""
return next(self.pool).patch(url=url, data=data, **kwargs)
def post(self, url, data=None, **kwargs):
"""Sends a POST request"""
return next(self.pool).post(url=url, data=data, **kwargs)
def put(self, url, data=None, **kwargs):
"""Sends a PUT request"""
return next(self.pool).put(url=url, data=data, **kwargs)