perf.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """
  2. Produce performance measurement
  3. """
  4. import argparse
  5. import multiprocessing
  6. import os
  7. import time
  8. from collections import OrderedDict
  9. from random import randint
  10. from sandbox.perf.simulation import ComputeSubject
  11. from synergine2.config import Config
  12. from synergine2.cycle import CycleManager
  13. from synergine2.log import SynergineLogger
  14. from synergine2.simulation import Simulation, Subjects
  15. def simulate(complexity, subject_count, cycle_count, cores):
  16. config = Config(dict(complexity=complexity, core=dict(use_x_cores=cores)))
  17. simulation = Simulation(config)
  18. subjects = Subjects(simulation=simulation)
  19. for i in range(subject_count):
  20. subjects.append(ComputeSubject(
  21. config=config,
  22. simulation=simulation,
  23. data=[randint(0, 1000) for i in range(1000)]
  24. ))
  25. simulation.subjects = subjects
  26. cycle_manager = CycleManager(
  27. config,
  28. SynergineLogger('perf'),
  29. simulation=simulation,
  30. )
  31. for j in range(cycle_count):
  32. cycle_manager.next()
  33. def main():
  34. parser = argparse.ArgumentParser(description='Perf measures')
  35. parser.add_argument(
  36. '--max_cores',
  37. type=int,
  38. default=0,
  39. help='number of used cores',
  40. )
  41. args = parser.parse_args()
  42. host_cores = multiprocessing.cpu_count()
  43. retry = 3
  44. cycles = 10
  45. subject_counts = [100, 500]
  46. complexities = [100, 2000]
  47. max_cores = args.max_cores or host_cores
  48. results = []
  49. datas = OrderedDict()
  50. for core_i in range(max_cores):
  51. # if core_i == 0:
  52. # continue
  53. core_count = core_i + 1
  54. for subject_count in subject_counts:
  55. for complexity in complexities:
  56. print('COMPLEXITY: {}, SUBJECTS: {}, CORES: {}'.format(
  57. complexity,
  58. subject_count,
  59. core_count,
  60. ), end='')
  61. durations = []
  62. for try_i in range(retry):
  63. start_time = time.time()
  64. simulate(complexity, subject_count, cycles, core_count)
  65. durations.append(time.time() - start_time)
  66. duration = min(durations)
  67. result = {
  68. 'cores': core_count,
  69. 'complexity': complexity,
  70. 'subject_count': subject_count,
  71. 'cycles': cycles,
  72. 'duration': duration,
  73. 'duration_cycle': duration / cycles,
  74. 'duration_subj_complex': (duration / cycles) / (subject_count * complexity),
  75. }
  76. results.append(result)
  77. print(': {}s, {}s/c, {}s/C'.format(
  78. result['duration'],
  79. result['duration_cycle'],
  80. result['duration_subj_complex'],
  81. ))
  82. datas.setdefault(complexity, {}).setdefault(subject_count, {})[core_count] = result['duration_cycle']
  83. for d_complexity, c_values in sorted(datas.items(), key=lambda e: int(e[0])):
  84. data_file_name = 'DATA_{}'.format(str(d_complexity))
  85. try:
  86. os.unlink(data_file_name)
  87. except FileNotFoundError:
  88. pass
  89. with open(data_file_name, 'w+') as file:
  90. file.writelines(['# (COMPLEXITY {}) SUBJECTS CORES_{}\n'.format(
  91. str(d_complexity),
  92. ' CORES_'.join(map(str, range(1, max_cores+1))),
  93. )])
  94. for d_subject_count, d_cores in c_values.items():
  95. line = '{} {}\n'.format(
  96. str(d_subject_count),
  97. ' '.join(map(str, d_cores.values())),
  98. )
  99. file.writelines([line])
  100. """
  101. subj_core = []
  102. for subj, core_v in c_values.items():
  103. for core_nb in core_v.keys():
  104. subj_core.append((subj, core_nb))
  105. file.writelines(['# (COMPLEXITY {}) SUBJECTS CORES_{}\n'.format(
  106. str(d_complexity),
  107. ' '.join([
  108. 'SUBJ_{}_COR_{}'.format(
  109. subj, core_nb,
  110. ) for subj, core_nb in subj_core
  111. ])
  112. )])
  113. """
  114. for d_complexity, c_values in datas.items():
  115. print('')
  116. print('gnuplot -p -e "set title \\"COMPLEXITY_{}\\"; plot {}"'.format(
  117. str(d_complexity),
  118. ','.join([
  119. ' \'DATA_{}\' using 1:{} title \'CORE_{}\' with lines'.format(
  120. d_complexity,
  121. d_core+1,
  122. d_core,
  123. ) for d_core in range(1, max_cores+1)
  124. ])
  125. ))
  126. if __name__ == '__main__':
  127. main()