繁体   English   中英

确定CPU利用率的时间

[英]Determining time for CPU Utilization

我有兴趣了解我的系统的CPU使用率保持在70%或更高的水平。 我的示例数据如下所示。 完整的数据在这里

Time                    CPUDemandPercentage
2019-03-06 03:55:00     40.17
2019-03-06 14:15:00     77.33
2019-03-06 14:20:00     79.66

为了实现我想要的东西,我已经探索了以下事情。 我试图:

  • 确定峰值位置
  • 确定峰宽
import numpy as np
import matplotlib.pyplot as plt 
import scipy.signal
from pandas import read_csv
data=read_csv('data.csv',header=0,usecols=["CPUDemandPercentage"])
y = np.array(data['CPUDemandPercentage'])
indexes = scipy.signal.find_peaks_cwt(y, np.arange(1, 4))
plt.plot(indexes, y[indexes], "xr"); plt.plot(y); plt.legend(['Peaks'])
plt.show()

这给了我一个图表 峰

  • 它不是很准确,没有显示负峰值。 我怎样才能在这里提高准确性。
  • 另外我如何找到峰的宽度。

我在这里没有线索。 有人能帮我吗。

以下不是基于熊猫的解决方案。 我们的想法是查看先前和当前的cpu级别,如果它们“足够高”,则增加计数器

import csv

# Assuming delta time between rows is 5 minutes

DELTA_T = 5


def get_cpu_time_above_pct(pct):
    time_above_pct = 0
    previous_cpu_level = None
    with open('cpu.csv', 'rb') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            current_cpu_level = float(row[1])
            if previous_cpu_level is not None and
               current_cpu_level >= pct and
               previous_cpu_level >= pct:
                   time_above_pct += DELTA_T
            previous_cpu_level = current_cpu_level

    return time_above_pct


print('CPU Time above 70\% : {} minutes'.format(get_cpu_time_above_pct(70)))

另一个答案是完整的熊猫:这个解决方案是通用的,不需要有相同的timedelta措施

df['Time']=df['Time'].apply((lambda x: pd.to_datetime(x)))
df['TimeDelta'] = df['Time'].shift(-1) - df['Time']
filter = df['CPUDemandPercentage'] >= 70.0
df['changes'] = [(x,y) for x,y in zip(filter , filter.shift(-1))]
result  = df[df['changes']==(True,True)]['TimeDelta'].sum()

print(f'TimeCPU>=70%: {result} or {result.total_seconds()/60} minutes')

输出:

TimeCPU>70%: 0 days 03:10:00 or 190.0 minutes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM