perf.py 4.6KB

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