perf.py 4.8KB

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