bench_worker.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from __future__ import print_function
  2. import os
  3. import sys
  4. os.environ.update(
  5. NOSETPS='yes',
  6. USE_FAST_LOCALS='yes',
  7. )
  8. import anyjson # noqa
  9. JSONIMP = os.environ.get('JSONIMP')
  10. if JSONIMP:
  11. anyjson.force_implementation(JSONIMP)
  12. print('anyjson implementation: {0!r}'.format(anyjson.implementation.name))
  13. from celery import Celery, group # noqa
  14. from celery.five import range # noqa
  15. from kombu.five import monotonic # noqa
  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_DEFAULT_DELIVERY_MODE=1,
  27. CELERY_QUEUES={
  28. 'bench.worker': {
  29. 'exchange': 'bench.worker',
  30. 'routing_key': 'bench.worker',
  31. 'no_ack': True,
  32. 'exchange_durable': False,
  33. 'queue_durable': False,
  34. 'auto_delete': True,
  35. }
  36. },
  37. CELERY_TASK_SERIALIZER='json',
  38. CELERY_DEFAULT_QUEUE='bench.worker',
  39. CELERY_BACKEND=None,
  40. ),
  41. def tdiff(then):
  42. return monotonic() - then
  43. @app.task(cur=0, time_start=None, queue='bench.worker', bare=True)
  44. def it(_, n):
  45. # use internal counter, as ordering can be skewed
  46. # by previous runs, or the broker.
  47. i = it.cur
  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 = monotonic()
  51. if not i:
  52. it.subt = it.time_start = monotonic()
  53. elif i > n - 2:
  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. import os
  60. os._exit()
  61. it.cur += 1
  62. def bench_apply(n=DEFAULT_ITS):
  63. time_start = monotonic()
  64. task = it._get_current_object()
  65. with app.producer_or_acquire() as producer:
  66. [task.apply_async((i, n), producer=producer) for i in range(n)]
  67. print('-- apply {0} tasks: {1}s'.format(n, monotonic() - time_start))
  68. def bench_work(n=DEFAULT_ITS, loglevel='CRITICAL'):
  69. loglevel = os.environ.get('BENCH_LOGLEVEL') or loglevel
  70. if loglevel:
  71. app.log.setup_logging_subsystem(loglevel=loglevel)
  72. worker = app.WorkController(concurrency=15,
  73. queues=['bench.worker'])
  74. try:
  75. print('STARTING WORKER')
  76. worker.start()
  77. except SystemExit:
  78. raise
  79. assert sum(worker.state.total_count.values()) == n + 1
  80. def bench_both(n=DEFAULT_ITS):
  81. bench_apply(n)
  82. bench_work(n)
  83. def main(argv=sys.argv):
  84. n = DEFAULT_ITS
  85. if len(argv) < 2:
  86. print('Usage: {0} [apply|work|both] [n=20k]'.format(
  87. os.path.basename(argv[0]),
  88. ))
  89. return sys.exit(1)
  90. try:
  91. try:
  92. n = int(argv[2])
  93. except IndexError:
  94. pass
  95. return {'apply': bench_apply,
  96. 'work': bench_work,
  97. 'both': bench_both}[argv[1]](n=n)
  98. except:
  99. raise
  100. if __name__ == '__main__':
  101. main()