profile-process.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #! /usr/bin/env python3
  2. import sys
  3. import subprocess
  4. import time
  5. import psutil
  6. import matplotlib
  7. matplotlib.use('Agg')
  8. import matplotlib.pyplot as plt
  9. from matplotlib.backends.backend_pdf import PdfPages
  10. def log_performance():
  11. def limit(n, limit):
  12. if n > limit:
  13. return limit
  14. else:
  15. return n
  16. # Interval for data collection (in seconds)
  17. probe_interval = 1
  18. i = 0
  19. x = []
  20. # Memory
  21. stats_rss = []
  22. stats_vms = []
  23. # CPU
  24. stats_usr = []
  25. stats_sys = []
  26. stats_cpu = []
  27. # Disk
  28. stats_io_r = []
  29. stats_io_w = []
  30. stats_io_r_b = []
  31. stats_io_w_b = []
  32. # proc = subprocess.Popen("./CLI.py")
  33. proc = subprocess.Popen(sys.argv[1:])
  34. p = psutil.Process(proc.pid)
  35. start_time = time.time()
  36. while proc is None or proc.poll() is None:
  37. m = p.memory_info()
  38. c = p.cpu_times()
  39. io = p.io_counters()
  40. x.append(i)
  41. stats_rss.append(m[0] / 10 ** 6)
  42. stats_vms.append(m[1] / 10 ** 6)
  43. stats_usr.append(c[0])
  44. stats_sys.append(c[1])
  45. stats_cpu.append(limit(p.cpu_percent(interval=None), 100))
  46. stats_io_r.append(io[0])
  47. stats_io_w.append(io[1])
  48. stats_io_r_b.append(io[2])
  49. stats_io_w_b.append(io[3])
  50. time.sleep(probe_interval)
  51. i += probe_interval
  52. print("=== MEASUREMENT RECORDED " + str(time.time() - start_time) + " seconds.")
  53. # Define style for plots
  54. f, axs = plt.subplots(4, 1, figsize=(8, 12))
  55. plt.rcParams["font.family"] = "monospace"
  56. plt.rcParams["font.size"] = 8.5
  57. plt.rcParams["legend.fontsize"] = 8.0
  58. # plt.autoscale(enable=True, axis='both')
  59. f.subplots_adjust(hspace=0.5)
  60. pp = PdfPages('multipage.pdf')
  61. # f.add_subplot(4,1,2)
  62. plt.subplot(411)
  63. plt.grid(True)
  64. plt.plot(x, stats_cpu, label="CPU Usage")
  65. # plt.plot(x, stats_usr, label="User")
  66. # plt.plot(x, stats_sys, label="System")
  67. plt.xlabel("Execution time (seconds)")
  68. plt.ylabel("CPU Load (%)")
  69. plt.legend(loc="upper left")
  70. # plt.savefig("profile_cpu.png")
  71. # f.add_subplot(4,1,1)
  72. plt.subplot(412)
  73. plt.grid(True)
  74. plt.plot(x, stats_rss, label="RSS")
  75. plt.plot(x, stats_vms, label="VMS")
  76. plt.xlabel("Execution time (seconds)")
  77. plt.ylabel("Memory Usage (MBytes)")
  78. plt.legend(loc="upper left")
  79. # plt.savefig("profile_memory.png")
  80. # f.add_subplot(4,1,3)
  81. plt.subplot(413)
  82. plt.grid(True)
  83. plt.plot(x, stats_io_r, label="IO Reads")
  84. # plt.plot(x, stats_io_w, label="IO Writes")
  85. # plt.plot(x, stats_io_r_b, label="IO Reads Bytes")
  86. # plt.plot(x, stats_io_w_b, label="IO Writes Bytes")
  87. # plt.xlabel("Execution time (seconds)")
  88. plt.xlabel("Execution time (seconds)")
  89. plt.ylabel("I/O reads counter")
  90. # plt.ylabel("IO Profile")
  91. plt.legend(loc="upper left")
  92. # f.add_subplot(4,1,4)
  93. plt.subplot(414)
  94. plt.grid(True)
  95. plt.plot(x, stats_io_w, label="IO Writes")
  96. plt.xlabel("Execution time (seconds)")
  97. plt.ylabel("I/O writes counter")
  98. plt.legend(loc="upper left")
  99. plt.savefig(pp, format='pdf')
  100. pp.close()
  101. sys.exit(proc.returncode)
  102. log_performance()