/
clustrloadr.py
177 lines (140 loc) · 6.46 KB
/
clustrloadr.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
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
"""
Copyright (c) 2016 Olof Montin <olof@thebrewery.se>
This file is part of loadr.
loadr is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
loadr is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with loadr. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
from multiprocessing import get_context
from providers import get_provider
class Session:
"""A Session is a set of multiple providers and merges their output into
the same output Queue.
The Session workflow are as follows:
+---------+
+-+ Session |
| +---------+---------------------------------------------+ +--------------------------+
1.+-> providers({"provider-name": {"type": "provider-type", +---> providers.get_provider() |
| | ... provider-config }}) | +-+------------------------+
| +-------------------------------------------------------+ |
2.+-> start({"provider": "<provider-name>", | |
| | "instances": <number-of-instances>, | |
| | "concurrency": <simultaneous-requests>, | +-v--------+
| | "repeat": <repeat-requests-x-times>}) +-+ | Provider |
| +-------------------------------------------------------+ | +----------+---------------+
3.+-> requests(<requests-list>) | +-> create_instances() |
| +-------------------------------------------------------+ +--------------------------+
4.+-> run() +---> run_multiple_workers() |
| +-------------------------------------------------------+ +--------------------------+
5.+-> stop() +---> remove_instances() |
+-------------------------------------------------------+ +--------------------------+
"""
def __init__(self, output):
# Added providers as a dict, indexed by name
self._providers = {}
# Requests configuration
self._requests = []
# Session configuration
self._session = []
# Output Queue
self.output = output
def add_provider(self, name, provider_type, **config):
"""Adds a provider by name, type and configuration.
Storea provider indexed by name.
"""
self._providers[name] = get_provider(provider_type,
self.output,
**config)
def providers(self, providers={}):
"""Adds multiple providers by a dict, indexed by provider names.
Loops through the list and adds them with add_provider.
"""
for name, config in providers.items():
provider_type = config['type']
self.add_provider(name,
provider_type,
**{key: val
for key, val
in config.items()
if key != 'type'})
# Remove type property - which whould mess up provider.
return self._providers
def requests(self, requests=None):
"""Set requests config and return it.
If requests config not set, then just return it.
"""
if requests is not None:
self._requests = requests
return self._requests
def start(self, config=[]):
"""Starts all providers' instances simultaneously and blocks until
all instances are running.
Config argument is the session configuration which defines which
provider to run and with how many instances etc:
{
"provider": "<provider-name>",
"instances": <number-of-instances>,
"concurrency" <number-of-simultaneous-request-workers>,
"repeat": <number-of-repeatness>
}
"""
self._session = config
processes = []
mp = get_context('fork')
for s in self._session:
provider = self._providers[s['provider']]
# Start instance in background and do the waiting later...
provider.create_instances(s['instances'], wait=False)
# Then start the waiting in it's on thread which we'll join when
# all instances has been created.
p = mp.Process(target=provider.wait_for_running_instances)
p.start()
processes += [p]
# Now wait for all instance waiters to be finished
for p in processes:
p.join()
def stop(self):
"""Stops all providers' instances simultaneously and blocks until
all instances are stopped.
"""
processes = []
mp = get_context('fork')
for provider in self._providers.values():
# Stop provider, but we'll do the waiting later on...
provider.remove_instances(wait=False)
# Start waiting in it's own thread for later joining.
p = mp.Process(target=provider.wait_for_removed_instances)
p.start()
processes += [p]
# Now wait for all the waiters to get done.
for p in processes:
p.join()
def run(self):
"""Starts all the workers within all running instances.
It uses the configuration defined in the start method.
See documentation for start method.
"""
processes = []
mp = get_context('fork')
for s in self._session:
provider = self._providers[s['provider']]
# Start the worker runner in it's own thread for later joining...
process = mp.Process(target=provider.run_multiple_workers,
args=(s['concurrency'],
s['repeat'],
self._requests))
processes += [process]
# Start all of them...
for p in processes:
p.start()
# Now wait for doneness
for p in processes:
p.join()