Skip to content

Instantly share code, notes, and snippets.

@witalij-s
Created November 10, 2022 13:21
Show Gist options
  • Save witalij-s/eb754422ded4a62cb569d416a5c185a2 to your computer and use it in GitHub Desktop.
Save witalij-s/eb754422ded4a62cb569d416a5c185a2 to your computer and use it in GitHub Desktop.
Measure the CPU and memory consumption of your linux system with psutil
#!/usr/bin/env python
# Measure the CPU and memory of your linux system with psutil
#
# - Installation: The setup this system you need to run 'pip3 install psutil'
# - Usage: python3 performance.py
import psutil as PSUTIL
import time
# constants
TO_MB = 1/1000000
START_TIME = time.time()
# configure
TOTAL_MEASUREMENT_SECONDS = 2
# list of measurement data
cpu_usage_list = [] # %
memory_used_percent_list = [] # %
memory_used_list = [] # MB
memory_free_list = [] # MB
memory_cached_list = [] # MB
memory_buffers_list = [] # MB
memory_total_available = [] # MB
while (time.time() - START_TIME < TOTAL_MEASUREMENT_SECONDS) :
### CPU ###
cpu_usage_list.append(float(PSUTIL.cpu_percent(interval=1)))
### MEMORY ###
memory_check = PSUTIL.virtual_memory()
memory_used_percent_list.append(float(memory_check.percent))
# USED: memory used, calculated differently depending on the platform and designed
# for informational purposes only. total - free does not necessarily match used.
memory_used_list.append(float(memory_check.used)*TO_MB)
# FREE: memory not being used at all (zeroed) that is readily available; note that this
# doesn’t reflect the actual memory available (use available instead).
# total - used does not necessarily match free.
memory_free_list.append(float(memory_check.free)*TO_MB)
# CACHED: cache for various things.
memory_cached_list.append(float(memory_check.cached)*TO_MB)
# BUFFERS: cache for things like file system metadata.
memory_buffers_list.append(float(memory_check.buffers)*TO_MB)
# TOTAL: total physical memory (exclusive swap)
memory_total_available.append(float(memory_check.available)*TO_MB)
if int(time.time() - START_TIME)/TOTAL_MEASUREMENT_SECONDS*100 < 100:
print("Recording performance data. Progress: {}%".format(round(int(time.time() - START_TIME)/TOTAL_MEASUREMENT_SECONDS*100),2), end="\r")
else:
print("Recording performance data. Progress: 100%")
print("Finished.")
print("\nResults (over {} seconds):".format(TOTAL_MEASUREMENT_SECONDS))
print("|-----------------------------------|")
print("| Avg. CPU: | {:.2f} % |".format(sum(cpu_usage_list)/len(cpu_usage_list)))
print("| Avg. MEM used: | {:.2f} % |".format(sum(memory_used_percent_list)/len(memory_used_percent_list)))
print("| Avg. MEM free: | {:.2f} MB |".format(sum(memory_free_list)/len(memory_free_list)))
print("| Avg. MEM used: | {:.2f} MB |".format(sum(memory_used_list)/len(memory_used_list)))
print("| Avg. MEM cached: | {:.2f} MB |".format(sum(memory_cached_list)/len(memory_cached_list) + sum(memory_buffers_list)/len(memory_buffers_list)))
print("| Avg. MEM available: | {:.2f} MB |".format(sum(memory_total_available)/len(memory_total_available)))
print("|-----------------------------------|")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment