我有兴趣了解我的系统的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()
我在这里没有线索。有人能帮我吗。
另一个完整的熊猫答案:这个解决方案是通用的,不需要在度量之间有相同的时间差
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
以下不是基于熊猫的解决方案。我们的想法是查看先前和当前的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)))