tuoheng_algN/vodsdk/test/cpu/test.py

64 lines
1.7 KiB
Python

import platform
import subprocess
import fileinput
def get_mac_cpu_speed():
commond = 'system_profiler SPHardwareDataType | grep "Processor Speed" | cut -d ":" -f2'
proc = subprocess.Popen([commond], shell=True, stdout=subprocess.PIPE)
output = proc.communicate()[0]
output = output.decode() # bytes 转str
speed = output.lstrip().rstrip('\n')
return speed
def get_linux_cpu_speed():
for line in fileinput.input('/proc/cpuinfo'):
if 'MHz' in line:
value = line.split(':')[1].strip()
value = float(value)
speed = round(value / 1024, 1)
return "{speed} GHz".format(speed=speed)
def get_windows_cpu_speed():
import winreg
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"HARDWARE\DESCRIPTION\System\CentralProcessor\0")
speed, type = winreg.QueryValueEx(key, "~MHz")
speed = round(float(speed)/1024, 1)
return "{speed} GHz".format(speed=speed)
def get_cpu_speed():
osname = platform.system() # 获取操作系统的名称
speed = ''
if osname == "Darwin":
speed = get_mac_cpu_speed()
if osname == "Linux":
speed = get_linux_cpu_speed()
if osname in ["Windows", "Win32"]:
speed = get_windows_cpu_speed()
return speed
# print(get_cpu_speed())
import psutil
# CPU逻辑数量
# print(psutil.cpu_count())
# CPU物理核心
# print(psutil.cpu_count(logical=False))
# print(psutil.cpu_percent(interval=1, percpu=True))
#
# print(psutil.virtual_memory())
# print(psutil.virtual_memory().percent)#获取内存使用率
# print(psutil.swap_memory())
# print(psutil.disk_partitions()) # 磁盘分区信息
# print(psutil.disk_usage('/')) # 磁盘使用情况
# print(psutil.disk_io_counters()) # 磁盘IO
print(len(psutil.pids()))