python的psutil模块实现PC端内存监控

import psutil
import sys
import os


def get_pid_list():
    pid_list = []
    for i in psutil.pids():  # 用PID遍历本机应用
        try:  # 跳过无name进程的报错
            pro = psutil.Process(i)
            if exe_name in pro.name():
                pid_list.append(pro)
        except:
            pass
    return pid_list

def get_men():
    list1 = []
    for i in get_pid_list():
        list1.append(i.memory_full_info().uss/1024/1024)
    return list1

def get_cpu():
    list1 = []
    for i in get_pid_list():
        list1.append(i.cpu_percent())
    return list1

def get_threads():
    list1 = []
    for i in get_pid_list():
        list1.append(i.num_threads())
    return list1

def get_rows():
    a = len(get_pid_list())
    men = ''
    cpu = ''
    threads = ''
    for i in range(1,a+1):
        men += f' men{str(i)}'
        cpu += f' cpu{str(i)}'
        threads += f' threads{str(i)}'
    return 'time' + men + ' men_total' + cpu + 'cpu_tatol '+ threads + ' threads_total'

def get_timestamp():
    import time
    return str(time.time())

def list_to_str(l):
    ll = ''
    for i in l:
        ll += (' ' + str(i))
    return ll

def write_to_txt(file):
    # os.chdir(r'D:\adb')
    if not os.path.exists(file):  # 文件不存在就新建,并写入标签
        with open(file,mode='w') as op:
            op.write(get_rows())
    with open(file, 'a+') as op:  # 逐行写入性能数据
        # time.sleep(1)
        op.write('\n' + get_timestamp() + list_to_str(get_men()) + ' ' + str(sum(get_men())) + list_to_str(get_cpu()) + ' ' + str(sum(get_cpu())) + list_to_str(get_threads()) + ' ' +str(sum(get_threads())))

if __name__ == '__main__':
    while True:
        exe_name = sys.argv[2]  # sys.argv 接受外部参数
        write_to_txt(sys.argv[1])



 

import datetime
import subprocess
import sys
import time
from matplotlib import pyplot as plt
import numpy


def get_timestamp():
    return time.strftime("%Y%m%d%H%M%S", time.localtime())


def test_name(file_name):
    exe_name = 'chrome.exe'
    p = subprocess.Popen(
        r'python C:\Users\86181\PycharmProjects\zidong\get_data.py ' + file_name + ' ' + exe_name)  # cmd命令执行
    return p


def plot_1(file_name):
    data1 = numpy.loadtxt(file_name, skiprows=1, unpack=1)  
# 逐行读取TXT为多维列表,跳过第一行,行列倒置
    with open(file_name, 'r') as op:  # 读取第一行
        label = op.readline().strip().split(' ')
    for i in range(1, len(label)):
        plt.plot(data1[0], data1[i])
        [a, b] = numpy.polyfit(data1[0], data1[i], 1)  # 数据拟合
        new_y = []
        for k in data1[0]:
            new_y.append(a * k + b)
        plt.plot(data1[0], new_y)
        plt.xlabel('time')
        plt.ylabel(label[i])
        plt.title(label[i])
        plt.grid(True)
        plt.savefig(label[i] + '.png')


if __name__ == "__main__":
    file_name = get_timestamp() + '.txt'  
# 时间戳 + 方法名    sys._getframe().f_code.co_name
    p = test_name(file_name)
    # ?????
    time.sleep(10)
    p.kill()
    plot_1(file_name)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值