sampling.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import timeit
  2. from about_time.human_duration import fn_human_duration
  3. from .utils import toolkit
  4. from ..core.configuration import config_handler
  5. from ..core.progress import __alive_bar
  6. human_duration = fn_human_duration(False)
  7. def overhead(total=None, *, calibrate=None, **options):
  8. number = 400 # timeit number of runs inside each repetition.
  9. repeat = 300 # timeit how many times to repeat the whole test.
  10. config = config_handler(disable=True, **options)
  11. with __alive_bar(config, total, calibrate=calibrate, _cond=__lock, _sampling=True) as loc:
  12. # the timing of the print_cells function increases proportionately with the
  13. # number of columns in the terminal, so I want a baseline here `VOID.cols == 0`.
  14. res = timeit.repeat('alive_repr()', repeat=repeat, number=number, globals=loc)
  15. return human_duration(min(res) / number)
  16. OVERHEAD_SAMPLING_GROUP = [
  17. ('definite', dict(total=1)),
  18. ('manual(b)', dict(total=1, manual=True)),
  19. ('manual(u)', dict(manual=True)),
  20. ('unknown', dict()),
  21. ]
  22. OVERHEAD_SAMPLING = [
  23. ('default', dict()),
  24. ('receipt', dict(receipt_text=True)),
  25. ('no spinner', dict(spinner=None)),
  26. ('no elapsed', dict(elapsed=False)),
  27. ('no monitor', dict(monitor=False)),
  28. ('no stats', dict(stats=False)),
  29. ('no bar', dict(bar=None)),
  30. ('only spinner', dict(bar=None, monitor=False, elapsed=False, stats=False)),
  31. ('only elapsed', dict(bar=None, spinner=None, monitor=False, stats=False)),
  32. ('only monitor', dict(bar=None, spinner=None, elapsed=False, stats=False)),
  33. ('only stats', dict(bar=None, spinner=None, monitor=False, elapsed=False)),
  34. ('only bar', dict(spinner=None, monitor=False, elapsed=False, stats=False)),
  35. ('none', dict(bar=None, spinner=None, monitor=False, elapsed=False, stats=False)),
  36. ]
  37. def overhead_sampling():
  38. print('warmup', end='', flush=True)
  39. for _ in range(5):
  40. print('.', end='', flush=True)
  41. overhead()
  42. print('\r', end='', flush=True)
  43. max_name = max(len(x) for x, _ in OVERHEAD_SAMPLING)
  44. print(f'{"":>{max_name}} | {" | ".join(g for g, _ in OVERHEAD_SAMPLING_GROUP)} |')
  45. for name, config in OVERHEAD_SAMPLING:
  46. print(f'{name:>{max_name}} ', end='', flush=True)
  47. for group, data in OVERHEAD_SAMPLING_GROUP:
  48. print(f'| {overhead(**data, **config):^{len(group)}} ', end='', flush=True)
  49. print('|')
  50. def __noop_p(_ignore):
  51. return 0
  52. class __lock:
  53. def __enter__(self):
  54. pass
  55. def __exit__(self, _type, value, traceback):
  56. pass
  57. if __name__ == '__main__':
  58. parser, run = toolkit('Estimates the alive_progress overhead per cycle on your system.')
  59. run(overhead_sampling)