stress.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from __future__ import absolute_import
  2. import random
  3. import os
  4. import signal
  5. import sys
  6. from time import time, sleep
  7. from celery import Celery, group
  8. from celery.exceptions import TimeoutError
  9. from celery.five import range
  10. from celery.utils.debug import blockdetection
  11. # Should be run with workers running using these options:
  12. #
  13. # 1) celery -A stress worker -c 1 --maxtasksperchild=1
  14. # 2) celery -A stress worker -c 8 --maxtasksperchild=1
  15. #
  16. # 3) celery -A stress worker -c 1
  17. # 4) celery -A stress worker -c 8
  18. #
  19. # 5) celery -A stress worker --autoscale=8,0
  20. #
  21. # 6) celery -A stress worker --time-limit=1
  22. #
  23. # 7) celery -A stress worker -c1 --maxtasksperchild=1 -- celery.acks_late=1
  24. BIG = 'x' * 2 ** 20 * 8
  25. SMALL = 'e' * 1024
  26. celery = Celery(
  27. 'stress', broker='pyamqp://', backend='redis://',
  28. set_as_current=False,
  29. )
  30. celery.conf.update(
  31. CELERYD_PREFETCH_MULTIPLIER=1,
  32. )
  33. @celery.task
  34. def add(x, y):
  35. return x + y
  36. @celery.task
  37. def any_(*args, **kwargs):
  38. wait = kwargs.get('sleep')
  39. if wait:
  40. sleep(wait)
  41. @celery.task
  42. def exiting(status=0):
  43. sys.exit(status)
  44. @celery.task
  45. def kill(sig=signal.SIGKILL):
  46. os.kill(os.getpid(), sig)
  47. @celery.task
  48. def sleeping(i):
  49. sleep(i)
  50. @celery.task
  51. def segfault():
  52. import ctypes
  53. ctypes.memset(0, 0, 1)
  54. assert False, 'should not get here'
  55. class Stresstests(object):
  56. def __init__(self, app, block_timeout=30 * 60):
  57. self.app = app
  58. self.connerrors = self.app.connection().recoverable_connection_errors
  59. self.block_timeout = block_timeout
  60. def run(self, n=50):
  61. tests = [#self.manyshort,
  62. self.termbysig,
  63. self.bigtasks,
  64. self.smalltasks,
  65. self.revoketermfast,
  66. self.revoketermslow]
  67. for test in tests:
  68. self.runtest(test, n)
  69. def manyshort(self):
  70. self.join(group(add.s(i, i) for i in xrange(1000))())
  71. def runtest(self, fun, n=50):
  72. with blockdetection(self.block_timeout):
  73. t = time()
  74. i = 0
  75. failed = False
  76. print('-%s(%s)' % (fun.__name__, n))
  77. try:
  78. for i in range(n):
  79. print(i)
  80. fun()
  81. except Exception:
  82. failed = True
  83. raise
  84. finally:
  85. print('{0} {1} iterations in {2}s'.format(
  86. 'failed after' if failed else 'completed',
  87. i + 1, time() - t,
  88. ))
  89. def termbysig(self):
  90. self._evil_groupmember(kill)
  91. def termbysegfault(self):
  92. self._evil_groupmember(segfault)
  93. def _evil_groupmember(self, evil_t):
  94. g1 = group(add.s(2, 2), evil_t.s(), add.s(4, 4), add.s(8, 8))
  95. g2 = group(add.s(3, 3), add.s(5, 5), evil_t.s(), add.s(7, 7))
  96. self.join(g1(), timeout=10)
  97. self.join(g2(), timeout=10)
  98. def bigtasks(self, wait=None):
  99. self._revoketerm(wait, False, False, BIG)
  100. def smalltasks(self, wait=None):
  101. self._revoketerm(wait, False, False, SMALL)
  102. def revoketermfast(self, wait=None):
  103. self._revoketerm(wait, True, False, SMALL)
  104. def revoketermslow(self, wait=5):
  105. self._revoketerm(wait, True, True, BIG)
  106. def _revoketerm(self, wait=None, terminate=True,
  107. joindelay=True, data=BIG):
  108. g = group(any_.s(data, sleep=wait) for i in range(8))
  109. r = g()
  110. if terminate:
  111. if joindelay:
  112. sleep(random.choice(range(4)))
  113. r.revoke(terminate=True)
  114. self.join(r, timeout=100)
  115. def join(self, r, **kwargs):
  116. while 1:
  117. try:
  118. return r.get(propagate=False, **kwargs)
  119. except TimeoutError as exc:
  120. print('join timed out: %s' % (exc, ))
  121. except self.connerrors as exc:
  122. print('join: connection lost: %r' % (exc, ))
  123. if __name__ == '__main__':
  124. s = Stresstests(celery)
  125. s.run()