确定CPU利用率的时间

时间:2019-03-06 18:59:35

标签: python pandas numpy scipy linear-regression

我有兴趣了解系统的CPU使用率保持70%或更高的时间。我的样本数据如下所示。完整数据为here

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()

这给了我一个像 Peak

  • 它不是很准确,没有显示负峰。如何在这里提高准确性。
  • 我也如何找到峰的宽度。

我在这里毫无头绪。有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

以下不是基于熊猫的解决方案。目的是查看先前和当前的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)))

答案 1 :(得分:1)

另一种完整的答案:这种解决方案是通用的,无需在两个度量之间具有相同的时间增量

class progressBar extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint();


Radius corner =Radius.circular(8);

paint.color =Color.fromRGBO(0, 0, 0, 1);
canvas.drawRRect(RRect.fromRectAndRadius(Rect.fromLTWH(0, 0, 250, 14), corner), paint);

paint.color =Color.fromRGBO(191, 20, 28, 1);
canvas.drawRRect(RRect.fromRectAndRadius(Rect.fromLTWH(1, 2, 50, 10), corner), paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;

输出:

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')