bench_worker.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from __future__ import print_function
  2. import os
  3. import sys
  4. import time
  5. os.environ.update(
  6. NOSETPS='yes',
  7. USE_FAST_LOCALS='yes',
  8. )
  9. import anyjson
  10. JSONIMP = os.environ.get('JSONIMP')
  11. if JSONIMP:
  12. anyjson.force_implementation(JSONIMP)
  13. print('anyjson implementation: {0!r}'.format(anyjson.implementation.name))
  14. from celery import Celery, group
  15. from celery.five import range
  16. DEFAULT_ITS = 40000
  17. BROKER_TRANSPORT = os.environ.get('BROKER', 'librabbitmq')
  18. if hasattr(sys, 'pypy_version_info'):
  19. BROKER_TRANSPORT = 'pyamqp'
  20. app = Celery('bench_worker')
  21. app.conf.update(
  22. BROKER_TRANSPORT=BROKER_TRANSPORT,
  23. BROKER_POOL_LIMIT=10,
  24. CELERYD_POOL='solo',
  25. CELERYD_PREFETCH_MULTIPLIER=0,
  26. CELERY_DISABLE_RATE_LIMITS=True,
  27. CELERY_DEFAULT_DELIVERY_MODE=1,
  28. CELERY_QUEUES={
  29. 'bench.worker': {
  30. 'exchange': 'bench.worker',
  31. 'routing_key': 'bench.worker',
  32. 'no_ack': True,
  33. 'exchange_durable': False,
  34. 'queue_durable': False,
  35. 'auto_delete': True,
  36. }
  37. },
  38. CELERY_TASK_SERIALIZER='json',
  39. CELERY_DEFAULT_QUEUE='bench.worker',
  40. CELERY_BACKEND=None,
  41. ),
  42. def tdiff(then):
  43. return time.time() - then
  44. @app.task(cur=0, time_start=None, queue='bench.worker', bare=True)
  45. def it(_, n):
  46. i = it.cur # use internal counter, as ordering can be skewed
  47. # by previous runs, or the broker.
  48. if i and not i % 5000:
  49. print('({0} so far: {1}s)'.format(i, tdiff(it.subt)), file=sys.stderr)
  50. it.subt = time.time()
  51. if not i:
  52. it.subt = it.time_start = time.time()
  53. elif i == n - 1:
  54. total = tdiff(it.time_start)
  55. print('({0} so far: {1}s)'.format(i, tdiff(it.subt)), file=sys.stderr)
  56. print('-- process {0} tasks: {1}s total, {2} tasks/s} '.format(
  57. n, total, n / (total + .0),
  58. ))
  59. sys.exit()
  60. it.cur += 1
  61. def bench_apply(n=DEFAULT_ITS):
  62. time_start = time.time()
  63. group(it.s(i, n) for i in range(n))()
  64. print('-- apply {0} tasks: {1}s'.format(n, time.time() - time_start))
  65. def bench_work(n=DEFAULT_ITS, loglevel='CRITICAL'):
  66. loglevel = os.environ.get('BENCH_LOGLEVEL') or loglevel
  67. if loglevel:
  68. app.log.setup_logging_subsystem(loglevel=loglevel)
  69. worker = app.WorkController(concurrency=15,
  70. queues=['bench.worker'])
  71. try:
  72. print('STARTING WORKER')
  73. worker.start()
  74. except SystemExit:
  75. raise
  76. assert sum(worker.state.total_count.values()) == n + 1
  77. def bench_both(n=DEFAULT_ITS):
  78. bench_apply(n)
  79. bench_work(n)
  80. def main(argv=sys.argv):
  81. n = DEFAULT_ITS
  82. if len(argv) < 2:
  83. print('Usage: {0} [apply|work|both] [n=20k]'.format(
  84. os.path.basename(argv[0]),
  85. ))
  86. return sys.exit(1)
  87. try:
  88. try:
  89. n = int(argv[2])
  90. except IndexError:
  91. pass
  92. return {'apply': bench_apply,
  93. 'work': bench_work,
  94. 'both': bench_both}[argv[1]](n=n)
  95. except:
  96. raise
  97. if __name__ == '__main__':
  98. main()