profile-process.py 3.1 KB

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