代码拉取完成,页面将自动刷新
import os
import time
import psutil
import matplotlib.pyplot as plt
def get_cpu_usage():
return psutil.cpu_percent(interval=1)
def get_memory_usage():
mem = psutil.virtual_memory()
return mem.percent
def get_disk_usage():
disk = psutil.disk_usage('/')
return disk.percent
def get_file_io_stats():
disk_io_counters = psutil.disk_io_counters()
read_bytes_per_sec = disk_io_counters.read_bytes / 1024 # 转换为 KB/s
write_bytes_per_sec = disk_io_counters.write_bytes / 1024
return read_bytes_per_sec, write_bytes_per_sec
cpu_usages = []
memory_usages = []
disk_usages = []
read_speeds = []
write_speeds = []
fig, axs = plt.subplots(2, 2, figsize=(12, 8))
fig.suptitle('System Performance Monitoring')
while True:
cpu_usage = get_cpu_usage()
memory_usage = get_memory_usage()
disk_usage = get_disk_usage()
read_speed, write_speed = get_file_io_stats()
cpu_usages.append(cpu_usage)
memory_usages.append(memory_usage)
disk_usages.append(disk_usage)
read_speeds.append(read_speed)
write_speeds.append(write_speed)
os.system('clear')
print(f"CPU Usage: {cpu_usage}%")
print(f"Memory Usage: {memory_usage}%")
print(f"Disk Usage: {disk_usage}%")
print(f"File Read Speed: {read_speed} KB/s")
print(f"File Write Speed: {write_speed} KB/s")
axs[0, 0].clear()
axs[0, 0].plot(cpu_usages, label='CPU Usage', color='b')
axs[0, 0].set_title('CPU Usage')
axs[0, 0].set_ylabel('Percentage (%)')
axs[0, 1].clear()
axs[0, 1].plot(memory_usages, label='Memory Usage', color='g')
axs[0, 1].set_title('Memory Usage')
axs[0, 1].set_ylabel('Percentage (%)')
axs[1, 0].clear()
axs[1, 0].plot(disk_usages, label='Disk Usage', color='r')
axs[1, 0].set_title('Disk Usage')
axs[1, 0].set_ylabel('Percentage (%)')
axs[1, 1].clear()
axs[1, 1].plot(read_speeds, label='Read Speed', color='c')
axs[1, 1].plot(write_speeds, label='Write Speed', color='m')
axs[1, 1].set_title('File I/O Speeds')
axs[1, 1].set_ylabel('KB/s')
plt.pause(1)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。