perf.py 4.7KB

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