-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
runners.py
1413 lines (1217 loc) Β· 62.7 KB
/
runners.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import functools
import json
import logging
import os
import re
import socket
import sys
import time
import traceback
from abc import abstractmethod
from collections import defaultdict
from collections.abc import MutableMapping
from operator import (
itemgetter,
methodcaller,
)
from types import TracebackType
from typing import (
TYPE_CHECKING,
Dict,
Iterator,
List,
NoReturn,
ValuesView,
Set,
Optional,
Tuple,
Type,
Any,
cast,
Callable,
)
from uuid import uuid4
# @TODO: typing.Protocol is in python >= 3.8
try:
from typing import Protocol, TypedDict
except ImportError:
from typing_extensions import Protocol, TypedDict # type: ignore
import gevent
import greenlet
import psutil
from gevent.event import Event
from gevent.pool import Group
from . import User
from locust import __version__
from .dispatch import UsersDispatcher
from .exception import RPCError, RPCReceiveError, RPCSendError
from .log import greenlet_exception_logger
from .rpc import (
Message,
rpc,
)
from .stats import (
RequestStats,
StatsError,
setup_distributed_stats_event_listeners,
)
from . import argument_parser
if TYPE_CHECKING:
from .env import Environment
logger = logging.getLogger(__name__)
STATE_INIT, STATE_SPAWNING, STATE_RUNNING, STATE_CLEANUP, STATE_STOPPING, STATE_STOPPED, STATE_MISSING = [
"ready",
"spawning",
"running",
"cleanup",
"stopping",
"stopped",
"missing",
]
WORKER_REPORT_INTERVAL = 3.0
CPU_MONITOR_INTERVAL = 5.0
CPU_WARNING_THRESHOLD = 90
HEARTBEAT_INTERVAL = 1
HEARTBEAT_LIVENESS = 3
HEARTBEAT_DEAD_INTERNAL = -60
FALLBACK_INTERVAL = 5
CONNECT_TIMEOUT = 5
CONNECT_RETRY_COUNT = 60
greenlet_exception_handler = greenlet_exception_logger(logger)
class ExceptionDict(TypedDict):
count: int
msg: str
traceback: str
nodes: Set[str]
class Runner:
"""
Orchestrates the load test by starting and stopping the users.
Use one of the :meth:`create_local_runner <locust.env.Environment.create_local_runner>`,
:meth:`create_master_runner <locust.env.Environment.create_master_runner>` or
:meth:`create_worker_runner <locust.env.Environment.create_worker_runner>` methods on
the :class:`Environment <locust.env.Environment>` instance to create a runner of the
desired type.
"""
def __init__(self, environment: "Environment") -> None:
self.environment = environment
self.user_greenlets = Group()
self.greenlet = Group()
self.state = STATE_INIT
self.spawning_greenlet: Optional[gevent.Greenlet] = None
self.shape_greenlet: Optional[gevent.Greenlet] = None
self.shape_last_tick: Tuple[int, float] | Tuple[int, float, Optional[List[Type[User]]]] | None = None
self.current_cpu_usage: int = 0
self.cpu_warning_emitted: bool = False
self.worker_cpu_warning_emitted: bool = False
self.current_memory_usage: int = 0
self.greenlet.spawn(self.monitor_cpu_and_memory).link_exception(greenlet_exception_handler)
self.exceptions: Dict[int, ExceptionDict] = {}
# Because of the way the ramp-up/ramp-down is implemented, target_user_classes_count
# is only updated at the end of the ramp-up/ramp-down.
# See https://github.com/locustio/locust/issues/1883#issuecomment-919239824 for context.
self.target_user_classes_count: Dict[str, int] = {}
# target_user_count is set before the ramp-up/ramp-down occurs.
self.target_user_count: int = 0
self.custom_messages: Dict[str, Callable] = {}
self._users_dispatcher: Optional[UsersDispatcher] = None
# set up event listeners for recording requests
def on_request_success(request_type, name, response_time, response_length, **_kwargs):
self.stats.log_request(request_type, name, response_time, response_length)
def on_request_failure(request_type, name, response_time, response_length, exception, **_kwargs):
self.stats.log_request(request_type, name, response_time, response_length)
self.stats.log_error(request_type, name, exception)
# temporarily set log level to ignore warnings to suppress deprecation message
loglevel = logging.getLogger().level
logging.getLogger().setLevel(logging.ERROR)
self.environment.events.request_success.add_listener(on_request_success)
self.environment.events.request_failure.add_listener(on_request_failure)
logging.getLogger().setLevel(loglevel)
self.connection_broken = False
self.final_user_classes_count: Dict[str, int] = {} # just for the ratio report, fills before runner stops
# register listener that resets stats when spawning is complete
def on_spawning_complete(user_count: int) -> None:
self.update_state(STATE_RUNNING)
if environment.reset_stats:
logger.info("Resetting stats\n")
self.stats.reset_all()
self.environment.events.spawning_complete.add_listener(on_spawning_complete)
def __del__(self) -> None:
# don't leave any stray greenlets if runner is removed
if self.greenlet and len(self.greenlet) > 0:
self.greenlet.kill(block=False)
@property
def user_classes(self) -> List[Type[User]]:
return self.environment.user_classes
@property
def user_classes_by_name(self) -> Dict[str, Type[User]]:
return self.environment.user_classes_by_name
@property
def stats(self) -> RequestStats:
return self.environment.stats
@property
def errors(self) -> Dict[str, StatsError]:
return self.stats.errors
@property
def user_count(self) -> int:
"""
:returns: Number of currently running users
"""
return len(self.user_greenlets)
@property
def user_classes_count(self) -> Dict[str, int]:
"""
:returns: Number of currently running users for each user class
"""
user_classes_count = {user_class.__name__: 0 for user_class in self.user_classes}
for user_greenlet in self.user_greenlets:
try:
user = user_greenlet.args[0]
except IndexError:
# TODO: Find out why args is sometimes empty. In gevent code,
# the supplied args are cleared in the gevent.greenlet.Greenlet.__free,
# so it seems a good place to start investigating. My suspicion is that
# the supplied args are emptied whenever the greenlet is dead, so we can
# simply ignore the greenlets with empty args.
logger.debug(
"ERROR: While calculating number of running users, we encountered a user that didn't have proper args %s (user_greenlet.dead=%s)",
user_greenlet,
user_greenlet.dead,
)
continue
user_classes_count[user.__class__.__name__] += 1
return user_classes_count
def update_state(self, new_state: str) -> None:
"""
Updates the current state
"""
# I (cyberwiz) commented out this logging, because it is too noisy even for debug level
# Uncomment it if you are specifically debugging state transitions
# logger.debug("Updating state to '%s', old state was '%s'" % (new_state, self.state))
self.state = new_state
def cpu_log_warning(self) -> bool:
"""Called at the end of the test"""
if self.cpu_warning_emitted:
logger.warning(
"CPU usage was too high at some point during the test! See https://docs.locust.io/en/stable/running-distributed.html for how to distribute the load over multiple CPU cores or machines"
)
return self.cpu_warning_emitted
def spawn_users(self, user_classes_spawn_count: Dict[str, int], wait: bool = False):
if self.state == STATE_INIT or self.state == STATE_STOPPED:
self.update_state(STATE_SPAWNING)
logger.debug(
"Spawning additional %s (%s already running)..."
% (json.dumps(user_classes_spawn_count), json.dumps(self.user_classes_count))
)
def spawn(user_class: str, spawn_count: int) -> List[User]:
n = 0
new_users: List[User] = []
while n < spawn_count:
new_user = self.user_classes_by_name[user_class](self.environment)
new_user.start(self.user_greenlets)
new_users.append(new_user)
n += 1
if n % 10 == 0 or n == spawn_count:
logger.debug("%i users spawned" % self.user_count)
logger.debug("All users of class %s spawned" % user_class)
return new_users
new_users: List[User] = []
for user_class, spawn_count in user_classes_spawn_count.items():
new_users += spawn(user_class, spawn_count)
if wait:
self.user_greenlets.join()
logger.info("All users stopped\n")
return new_users
def stop_users(self, user_classes_stop_count: Dict[str, int]) -> None:
async_calls_to_stop = Group()
stop_group = Group()
for user_class, stop_count in user_classes_stop_count.items():
if self.user_classes_count[user_class] == 0:
continue
to_stop: List[greenlet.greenlet] = []
for user_greenlet in self.user_greenlets:
if len(to_stop) == stop_count:
break
try:
user = user_greenlet.args[0]
except IndexError:
logger.error(
"While stopping users, we encountered a user that didn't have proper args %s", user_greenlet
)
continue
if type(user) == self.user_classes_by_name[user_class]:
to_stop.append(user)
if not to_stop:
continue
while True:
user_to_stop: User = to_stop.pop()
logger.debug("Stopping %s" % user_to_stop.greenlet.name)
if user_to_stop.greenlet is greenlet.getcurrent():
# User called runner.quit(), so don't block waiting for killing to finish
user_to_stop.group.killone(user_to_stop.greenlet, block=False)
elif self.environment.stop_timeout:
async_calls_to_stop.add(gevent.spawn_later(0, user_to_stop.stop, force=False))
stop_group.add(user_to_stop.greenlet)
else:
async_calls_to_stop.add(gevent.spawn_later(0, user_to_stop.stop, force=True))
if not to_stop:
break
async_calls_to_stop.join()
if not stop_group.join(timeout=self.environment.stop_timeout):
logger.info(
"Not all users finished their tasks & terminated in %s seconds. Stopping them..."
% self.environment.stop_timeout
)
stop_group.kill(block=True)
logger.debug(
"%g users have been stopped, %g still running", sum(user_classes_stop_count.values()), self.user_count
)
def monitor_cpu_and_memory(self) -> NoReturn:
process = psutil.Process()
while True:
self.current_cpu_usage = process.cpu_percent()
self.current_memory_usage = process.memory_info().rss
if self.current_cpu_usage > CPU_WARNING_THRESHOLD:
self.environment.events.cpu_warning.fire(environment=self.environment, cpu_usage=self.current_cpu_usage)
if not self.cpu_warning_emitted:
logging.warning(
f"CPU usage above {CPU_WARNING_THRESHOLD}%! This may constrain your throughput and may even give inconsistent response time measurements! See https://docs.locust.io/en/stable/running-distributed.html for how to distribute the load over multiple CPU cores or machines"
)
self.cpu_warning_emitted = True
gevent.sleep(CPU_MONITOR_INTERVAL)
@abstractmethod
def start(
self, user_count: int, spawn_rate: float, wait: bool = False, user_classes: Optional[List[Type[User]]] = None
) -> None:
...
def start_shape(self) -> None:
"""
Start running a load test with a custom LoadTestShape specified in the :meth:`Environment.shape_class <locust.env.Environment.shape_class>` parameter.
"""
if self.shape_greenlet:
logger.info("There is an ongoing shape test running. Editing is disabled")
return
logger.info("Shape test starting. User count and spawn rate are ignored for this type of load test")
self.update_state(STATE_INIT)
self.shape_greenlet = self.greenlet.spawn(self.shape_worker)
self.shape_greenlet.link_exception(greenlet_exception_handler)
if self.environment.shape_class is not None:
self.environment.shape_class.reset_time()
def shape_worker(self) -> None:
logger.info("Shape worker starting")
while self.state == STATE_INIT or self.state == STATE_SPAWNING or self.state == STATE_RUNNING:
current_tick = self.environment.shape_class.tick() if self.environment.shape_class is not None else None
if current_tick is None:
logger.info("Shape test stopping")
if self.environment.parsed_options and self.environment.parsed_options.headless:
self.quit()
else:
self.stop()
self.shape_greenlet = None
self.shape_last_tick = None
return
elif self.shape_last_tick == current_tick:
gevent.sleep(1)
else:
if len(current_tick) == 2:
user_count, spawn_rate = current_tick # type: ignore
user_classes = None
else:
user_count, spawn_rate, user_classes = current_tick # type: ignore
logger.info("Shape test updating to %d users at %.2f spawn rate" % (user_count, spawn_rate))
# TODO: This `self.start()` call is blocking until the ramp-up is completed. This can leads
# to unexpected behaviours such as the one in the following example:
# A load test shape has the following stages:
# stage 1: (user_count=100, spawn_rate=1) for t < 50s
# stage 2: (user_count=120, spawn_rate=1) for t < 100s
# stage 3: (user_count=130, spawn_rate=1) for t < 120s
# Because the first stage will take 100s to complete, the second stage
# will be skipped completely because the shape worker will be blocked
# at the `self.start()` of the first stage.
# Of course, this isn't a problem if the load test shape is well-defined.
# We should probably use a `gevent.timeout` with a duration a little over
# `(user_count - prev_user_count) / spawn_rate` in order to limit the runtime
# of each load test shape stage.
self.start(user_count=user_count, spawn_rate=spawn_rate, user_classes=user_classes)
self.shape_last_tick = current_tick
def stop(self) -> None:
"""
Stop a running load test by stopping all running users
"""
if self.state == STATE_STOPPED:
return
logger.debug("Stopping all users")
self.environment.events.test_stopping.fire(environment=self.environment)
self.final_user_classes_count = {**self.user_classes_count}
self.update_state(STATE_CLEANUP)
# if we are currently spawning users we need to kill the spawning greenlet first
if self.spawning_greenlet and not self.spawning_greenlet.ready():
self.spawning_greenlet.kill(block=True)
if self.environment.shape_class is not None and self.shape_greenlet is not greenlet.getcurrent():
# If the test was not started yet and locust is
# stopped/quit, shape_greenlet will be None.
if self.shape_greenlet is not None:
self.shape_greenlet.kill(block=True)
self.shape_greenlet = None
self.shape_last_tick = None
self.stop_users(self.user_classes_count)
self._users_dispatcher = None
self.update_state(STATE_STOPPED)
self.cpu_log_warning()
self.environment.events.test_stop.fire(environment=self.environment)
def quit(self) -> None:
"""
Stop any running load test and kill all greenlets for the runner
"""
self.stop()
self.greenlet.kill(block=True)
def log_exception(self, node_id: str, msg: str, formatted_tb: str) -> None:
key = hash(formatted_tb)
row = self.exceptions.setdefault(key, {"count": 0, "msg": msg, "traceback": formatted_tb, "nodes": set()})
row["count"] += 1
row["nodes"].add(node_id)
self.exceptions[key] = row
def register_message(self, msg_type: str, listener: Callable) -> None:
"""
Register a listener for a custom message from another node
:param msg_type: The type of the message to listen for
:param listener: The function to execute when the message is received
"""
self.custom_messages[msg_type] = listener
class LocalRunner(Runner):
"""
Runner for running single process load test
"""
# always set to 0 for LocalRunner
worker_index = 0
def __init__(self, environment) -> None:
"""
:param environment: Environment instance
"""
super().__init__(environment)
# Only when running in standalone mode (non-distributed)
self._local_worker_node = WorkerNode(id="local")
self._local_worker_node.user_classes_count = self.user_classes_count
# register listener that's logs the exception for the local runner
def on_user_error(user_instance, exception, tb):
formatted_tb = "".join(traceback.format_tb(tb))
self.log_exception("local", str(exception), formatted_tb)
self.environment.events.user_error.add_listener(on_user_error)
def _start(self, user_count: int, spawn_rate: float, wait: bool = False, user_classes: list = None) -> None:
"""
Start running a load test
:param user_count: Total number of users to start
:param spawn_rate: Number of users to spawn per second
:param wait: If True calls to this method will block until all users are spawned.
If False (the default), a greenlet that spawns the users will be
started and the call to this method will return immediately.
:param user_classes: The user classes to be dispatched, None indicates to use the classes the dispatcher was
invoked with.
"""
self.target_user_count = user_count
if self.state != STATE_RUNNING and self.state != STATE_SPAWNING:
self.stats.clear_all()
self.exceptions = {}
self.cpu_warning_emitted = False
self.worker_cpu_warning_emitted = False
self.environment._filter_tasks_by_tags()
self.environment.events.test_start.fire(environment=self.environment)
if wait and user_count - self.user_count > spawn_rate:
raise ValueError("wait is True but the amount of users to add is greater than the spawn rate")
for user_class in self.user_classes:
if self.environment.host:
user_class.host = self.environment.host
if self.state != STATE_INIT and self.state != STATE_STOPPED:
self.update_state(STATE_SPAWNING)
if self._users_dispatcher is None:
self._users_dispatcher = UsersDispatcher(
worker_nodes=[self._local_worker_node], user_classes=self.user_classes
)
logger.info("Ramping to %d users at a rate of %.2f per second" % (user_count, spawn_rate))
cast(UsersDispatcher, self._users_dispatcher).new_dispatch(user_count, spawn_rate, user_classes)
try:
for dispatched_users in self._users_dispatcher:
user_classes_spawn_count: Dict[str, int] = {}
user_classes_stop_count: Dict[str, int] = {}
user_classes_count = dispatched_users[self._local_worker_node.id]
logger.debug("Ramping to %s" % _format_user_classes_count_for_log(user_classes_count))
for user_class_name, user_class_count in user_classes_count.items():
if self.user_classes_count[user_class_name] > user_class_count:
user_classes_stop_count[user_class_name] = (
self.user_classes_count[user_class_name] - user_class_count
)
elif self.user_classes_count[user_class_name] < user_class_count:
user_classes_spawn_count[user_class_name] = (
user_class_count - self.user_classes_count[user_class_name]
)
if wait:
# spawn_users will block, so we need to call stop_users first
self.stop_users(user_classes_stop_count)
self.spawn_users(user_classes_spawn_count, wait)
else:
# call spawn_users before stopping the users since stop_users
# can be blocking because of the stop_timeout
self.spawn_users(user_classes_spawn_count, wait)
self.stop_users(user_classes_stop_count)
self._local_worker_node.user_classes_count = next(iter(dispatched_users.values()))
except KeyboardInterrupt:
# TODO: Find a cleaner way to handle that
# We need to catch keyboard interrupt. Otherwise, if KeyboardInterrupt is received while in
# a gevent.sleep inside the dispatch_users function, locust won't gracefully shutdown.
self.quit()
logger.info(f"All users spawned: {_format_user_classes_count_for_log(self.user_classes_count)}")
self.target_user_classes_count = self.user_classes_count
self.environment.events.spawning_complete.fire(user_count=sum(self.target_user_classes_count.values()))
def start(
self, user_count: int, spawn_rate: float, wait: bool = False, user_classes: Optional[List[Type[User]]] = None
) -> None:
if spawn_rate > 100:
logger.warning(
"Your selected spawn rate is very high (>100), and this is known to sometimes cause issues. Do you really need to ramp up that fast?"
)
if self.spawning_greenlet:
# kill existing spawning_greenlet before we start a new one
self.spawning_greenlet.kill(block=True)
self.spawning_greenlet = self.greenlet.spawn(
lambda: self._start(user_count, spawn_rate, wait=wait, user_classes=user_classes)
)
self.spawning_greenlet.link_exception(greenlet_exception_handler)
def stop(self) -> None:
if self.state == STATE_STOPPED:
return
super().stop()
def send_message(self, msg_type: str, data: Optional[Any] = None) -> None:
"""
Emulates internodal messaging by calling registered listeners
:param msg_type: The type of the message to emulate sending
:param data: Optional data to include
"""
logger.debug("Running locally: sending %s message to self" % msg_type)
if msg_type in self.custom_messages:
listener = self.custom_messages[msg_type]
msg = Message(msg_type, data, "local")
listener(environment=self.environment, msg=msg)
else:
logger.warning(f"Unknown message type received: {msg_type}")
class DistributedRunner(Runner):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
setup_distributed_stats_event_listeners(self.environment.events, self.stats)
class WorkerNode:
def __init__(self, id: str, state=STATE_INIT, heartbeat_liveness=HEARTBEAT_LIVENESS) -> None:
self.id: str = id
self.state = state
self.heartbeat = heartbeat_liveness
self.cpu_usage: int = 0
self.cpu_warning_emitted = False
self.memory_usage: int = 0
# The reported users running on the worker
self.user_classes_count: Dict[str, int] = {}
@property
def user_count(self) -> int:
return sum(self.user_classes_count.values())
class WorkerNodes(MutableMapping):
def __init__(self):
self._worker_nodes: Dict[str, WorkerNode] = {}
def get_by_state(self, state) -> List[WorkerNode]:
return [c for c in self.values() if c.state == state]
@property
def all(self) -> ValuesView[WorkerNode]:
return self.values()
@property
def ready(self) -> List[WorkerNode]:
return self.get_by_state(STATE_INIT)
@property
def spawning(self) -> List[WorkerNode]:
return self.get_by_state(STATE_SPAWNING)
@property
def running(self) -> List[WorkerNode]:
return self.get_by_state(STATE_RUNNING)
@property
def missing(self) -> List[WorkerNode]:
return self.get_by_state(STATE_MISSING)
def __setitem__(self, k: str, v: WorkerNode) -> None:
self._worker_nodes[k] = v
def __delitem__(self, k: str) -> None:
del self._worker_nodes[k]
def __getitem__(self, k: str) -> WorkerNode:
return self._worker_nodes[k]
def __len__(self) -> int:
return len(self._worker_nodes)
def __iter__(self) -> Iterator[str]:
return iter(list(self._worker_nodes.keys()))
class MasterRunner(DistributedRunner):
"""
Runner used to run distributed load tests across multiple processes and/or machines.
MasterRunner doesn't spawn any user greenlets itself. Instead it expects
:class:`WorkerRunners <WorkerRunner>` to connect to it, which it will then direct
to start and stop user greenlets. Stats sent back from the
:class:`WorkerRunners <WorkerRunner>` will aggregated.
"""
def __init__(self, environment, master_bind_host, master_bind_port):
"""
:param environment: Environment instance
:param master_bind_host: Host/interface to use for incoming worker connections
:param master_bind_port: Port to use for incoming worker connections
"""
super().__init__(environment)
self.worker_cpu_warning_emitted = False
self.master_bind_host = master_bind_host
self.master_bind_port = master_bind_port
self.spawn_rate: float = 0.0
self.spawning_completed = False
self.worker_indexes: dict[str, int] = {}
self.worker_index_max = 0
self.clients = WorkerNodes()
try:
self.server = rpc.Server(master_bind_host, master_bind_port)
except RPCError as e:
if e.args[0] == "Socket bind failure: Address already in use":
port_string = (
master_bind_host + ":" + str(master_bind_port) if master_bind_host != "*" else str(master_bind_port)
)
logger.error(
f"The Locust master port ({port_string}) was busy. Close any applications using that port - perhaps an old instance of Locust master is still running? ({e.args[0]})"
)
sys.exit(1)
else:
raise
self._users_dispatcher: Optional[UsersDispatcher] = None
self.greenlet.spawn(self.heartbeat_worker).link_exception(greenlet_exception_handler)
self.greenlet.spawn(self.client_listener).link_exception(greenlet_exception_handler)
# listener that gathers info on how many users the worker has spawned
def on_worker_report(client_id: str, data: Dict[str, Any]) -> None:
if client_id not in self.clients:
logger.info("Discarded report from unrecognized worker %s", client_id)
return
self.clients[client_id].user_classes_count = data["user_classes_count"]
self.environment.events.worker_report.add_listener(on_worker_report)
# register listener that sends quit message to worker nodes
def on_quitting(environment: "Environment", **kw):
self.quit()
self.environment.events.quitting.add_listener(on_quitting)
def rebalancing_enabled(self) -> bool:
return self.environment.parsed_options is not None and cast(
bool, self.environment.parsed_options.enable_rebalancing
)
def get_worker_index(self, client_id):
"""
Get the worker index for the specified client ID;
this is a deterministic 0-based ordinal number and guaranteed to not change
while Master is alive.
"""
if client_id in self.worker_indexes:
return self.worker_indexes[client_id]
index = self.worker_index_max
self.worker_indexes[client_id] = index
self.worker_index_max += 1
return index
@property
def user_count(self) -> int:
return sum([c.user_count for c in self.clients.values()])
def cpu_log_warning(self) -> bool:
warning_emitted = Runner.cpu_log_warning(self)
if self.worker_cpu_warning_emitted:
logger.warning("CPU usage threshold was exceeded on workers during the test!")
warning_emitted = True
return warning_emitted
def start(
self, user_count: int, spawn_rate: float, wait=False, user_classes: Optional[List[Type[User]]] = None
) -> None:
self.spawning_completed = False
self.target_user_count = user_count
num_workers = len(self.clients.ready) + len(self.clients.running) + len(self.clients.spawning)
if not num_workers:
logger.warning("You can't start a distributed test before at least one worker processes has connected")
return
for user_class in self.user_classes:
if self.environment.host:
user_class.host = self.environment.host
self.spawn_rate = spawn_rate
if self._users_dispatcher is None:
self._users_dispatcher = UsersDispatcher(
worker_nodes=list(self.clients.values()), user_classes=self.user_classes
)
logger.info(
"Sending spawn jobs of %d users at %.2f spawn rate to %d ready workers"
% (user_count, spawn_rate, num_workers)
)
worker_spawn_rate = float(spawn_rate) / (num_workers or 1)
if worker_spawn_rate > 100:
logger.warning(
"Your selected spawn rate is very high (>100/worker), and this is known to sometimes cause issues. Do you really need to ramp up that fast?"
)
if self.state != STATE_RUNNING and self.state != STATE_SPAWNING:
self.stats.clear_all()
self.exceptions = {}
self.environment._filter_tasks_by_tags()
self.environment.events.test_start.fire(environment=self.environment)
if self.environment.shape_class:
self.environment.shape_class.reset_time()
self.update_state(STATE_SPAWNING)
self._users_dispatcher.new_dispatch(
target_user_count=user_count, spawn_rate=spawn_rate, user_classes=user_classes
)
try:
for dispatched_users in self._users_dispatcher:
dispatch_greenlets = Group()
for worker_node_id, worker_user_classes_count in dispatched_users.items():
data = {
"timestamp": time.time(),
"user_classes_count": worker_user_classes_count,
"host": self.environment.host,
"stop_timeout": self.environment.stop_timeout,
"parsed_options": vars(self.environment.parsed_options)
if self.environment.parsed_options
else {},
}
dispatch_greenlets.add(
gevent.spawn_later(
0,
self.server.send_to_client,
Message("spawn", data, worker_node_id),
)
)
dispatched_user_count = sum(map(sum, map(methodcaller("values"), dispatched_users.values())))
logger.debug(
"Sending spawn messages for %g total users to %i worker(s)",
dispatched_user_count,
len(dispatch_greenlets),
)
dispatch_greenlets.join()
logger.debug(
f"Currently spawned users: {_format_user_classes_count_for_log(self.reported_user_classes_count)}"
)
self.target_user_classes_count = _aggregate_dispatched_users(dispatched_users)
except KeyboardInterrupt:
# TODO: Find a cleaner way to handle that
# We need to catch keyboard interrupt. Otherwise, if KeyboardInterrupt is received while in
# a gevent.sleep inside the dispatch_users function, locust won't gracefully shutdown.
self.quit()
# Wait a little for workers to report their users to the master
# so that we can give an accurate log message below and fire the `spawning_complete` event
# when the user count is really at the desired value.
timeout = gevent.Timeout(self._wait_for_workers_report_after_ramp_up())
timeout.start()
msg_prefix = "All users spawned"
try:
while self.user_count != self.target_user_count:
gevent.sleep(0.01)
except gevent.Timeout:
msg_prefix = (
"Spawning is complete and report waittime is expired, but not all reports received from workers"
)
finally:
timeout.cancel()
self.environment.events.spawning_complete.fire(user_count=sum(self.target_user_classes_count.values()))
self.spawning_completed = True
logger.info(f"{msg_prefix}: {_format_user_classes_count_for_log(self.reported_user_classes_count)}")
@functools.lru_cache()
def _wait_for_workers_report_after_ramp_up(self) -> float:
"""
The amount of time to wait after a ramp-up in order for all the workers to report their state
to the master. If not supplied by the user, it is 1000ms by default. If the supplied value is a number,
it is taken as-is. If the supplied value is a pattern like "some_number * WORKER_REPORT_INTERVAL",
the value will be "some_number * WORKER_REPORT_INTERVAL". The most sensible value would be something
like "1.25 * WORKER_REPORT_INTERVAL". However, some users might find it too high, so it is left
to a relatively small value of 1000ms by default.
"""
locust_wait_for_workers_report_after_ramp_up = os.getenv("LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP")
if locust_wait_for_workers_report_after_ramp_up is None:
return 1.0
match = re.search(
r"^(?P<coeff>(\d+)|(\d+\.\d+))[ ]*\*[ ]*WORKER_REPORT_INTERVAL$",
locust_wait_for_workers_report_after_ramp_up,
)
if match is None:
assert float(locust_wait_for_workers_report_after_ramp_up) >= 0
return float(locust_wait_for_workers_report_after_ramp_up)
else:
return float(match.group("coeff")) * WORKER_REPORT_INTERVAL
def stop(self, send_stop_to_client: bool = True) -> None:
if self.state not in [STATE_INIT, STATE_STOPPED, STATE_STOPPING]:
logger.debug("Stopping...")
self.environment.events.test_stopping.fire(environment=self.environment)
self.final_user_classes_count = {**self.reported_user_classes_count}
self.update_state(STATE_STOPPING)
if (
self.environment.shape_class is not None
and self.shape_greenlet is not None
and self.shape_greenlet is not greenlet.getcurrent()
):
self.shape_greenlet.kill(block=True)
self.shape_greenlet = None
self.shape_last_tick = None
self._users_dispatcher = None
if send_stop_to_client:
for client in self.clients.all:
logger.debug("Sending stop message to worker %s" % client.id)
self.server.send_to_client(Message("stop", None, client.id))
# Give an additional 60s for all workers to stop
timeout = gevent.Timeout(self.environment.stop_timeout + 60)
timeout.start()
try:
while self.user_count != 0:
gevent.sleep(1)
except gevent.Timeout:
logger.error("Timeout waiting for all workers to stop")
finally:
timeout.cancel()
self.environment.events.test_stop.fire(environment=self.environment)
def quit(self) -> None:
self.stop(send_stop_to_client=False)
logger.debug("Quitting...")
for client in self.clients.all:
logger.debug("Sending quit message to worker %s (index %s)" % (client.id, self.get_worker_index(client.id)))
self.server.send_to_client(Message("quit", None, client.id))
gevent.sleep(0.5) # wait for final stats report from all workers
self.greenlet.kill(block=True)
def check_stopped(self) -> None:
if (
not self.state == STATE_INIT
and not self.state == STATE_STOPPED
and (
(
self.state == STATE_STOPPING
and all(
map(
lambda x: x.state == STATE_INIT,
self.clients.all,
)
)
)
)
or all(
map(
lambda x: x.state not in (STATE_RUNNING, STATE_SPAWNING, STATE_INIT),
self.clients.all,
)
)
):
self.update_state(STATE_STOPPED)
def heartbeat_worker(self) -> NoReturn:
while True:
gevent.sleep(HEARTBEAT_INTERVAL)
if self.connection_broken:
self.reset_connection()
continue
missing_clients_to_be_removed = []
for client in self.clients.all:
# if clients goes missing for more than HEARTBEAT_DEAD_INTERNAL then add them to be removed list
if client.state == STATE_MISSING and client.heartbeat <= HEARTBEAT_DEAD_INTERNAL:
missing_clients_to_be_removed.append(client.id)
if client.heartbeat < 0 and client.state != STATE_MISSING:
logger.info(f"Worker {str(client.id)} failed to send heartbeat, setting state to missing.")
client.state = STATE_MISSING
client.user_classes_count = {}
if self._users_dispatcher is not None:
self._users_dispatcher.remove_worker(client)
if self.rebalancing_enabled() and self.state == STATE_RUNNING and self.spawning_completed:
self.start(self.target_user_count, self.spawn_rate)
if self.worker_count <= 0:
logger.info("The last worker went missing, stopping test.")
self.stop()
self.check_stopped()
else:
client.heartbeat -= 1
# if there are any missing clients to be removed then remove them and trigger rebalance.
if len(missing_clients_to_be_removed) > 0:
for to_remove_client_id in missing_clients_to_be_removed:
if self.clients.get(to_remove_client_id) is not None:
del self.clients[to_remove_client_id]
if self.state == STATE_RUNNING or self.state == STATE_SPAWNING:
# _users_dispatcher is set to none so that during redistribution the dead clients are not picked, alternative is to call self.stop() before start
self._users_dispatcher = None
# trigger redistribution after missing cclient removal
self.start(user_count=self.target_user_count, spawn_rate=self.spawn_rate)
def reset_connection(self) -> None:
logger.info("Resetting RPC server and all worker connections.")
try:
self.server.close(linger=0)
self.server = rpc.Server(self.master_bind_host, self.master_bind_port)
self.connection_broken = False
except RPCError as e:
logger.error(f"Temporary failure when resetting connection: {e}, will retry later.")
def client_listener(self) -> NoReturn:
while True:
try:
client_id, msg = self.server.recv_from_client()
except RPCReceiveError as e:
client_id = e.addr
if client_id and client_id in self.clients:
logger.error(f"RPCError when receiving from client: {e}. Will reset client {client_id}.")
try:
self.server.send_to_client(Message("reconnect", None, client_id))
except Exception as error:
logger.error(f"Error sending reconnect message to worker: {error}. Will reset RPC server.")
self.connection_broken = True
gevent.sleep(FALLBACK_INTERVAL)