import csv
import psutil
from time import strftime, localtime, sleep
flag = 1
with open ( 'record.csv' , 'w' , newline= '' ) as csvfile:
writer = csv. writer( csvfile)
writer. writerow( [ "date" , "cpu" , "memory" ] )
while flag < 20 :
result = [ (
strftime( "%H:%M:%S" , localtime( ) ) ,
psutil. cpu_percent( 1 ) ,
psutil. virtual_memory( ) . percent,
) ]
print ( result)
writer. writerows( result)
flag += 1
sleep( 2 )
print ( "完成" )
[('19:51:23', 12.6, 65.6)]
[('19:51:26', 11.9, 65.5)]
[('19:51:29', 14.6, 66.4)]
[('19:51:32', 10.8, 66.4)]
[('19:51:35', 12.0, 65.9)]
[('19:51:38', 7.3, 66.0)]
[('19:51:41', 7.0, 66.3)]
[('19:51:44', 7.8, 63.8)]
[('19:51:47', 10.1, 63.8)]
[('19:51:50', 30.9, 64.8)]
[('19:51:53', 12.5, 66.3)]
[('19:51:56', 6.2, 66.1)]
[('19:51:59', 31.3, 68.4)]
[('19:52:02', 12.0, 68.9)]
[('19:52:05', 7.4, 68.8)]
[('19:52:08', 5.4, 68.3)]
[('19:52:11', 20.3, 68.3)]
[('19:52:14', 2.7, 67.9)]
[('19:52:17', 7.7, 67.8)]
完成
import pandas as pd
from pyecharts import Line
df= pd. read_csv( "record.csv" )
line = Line( title = "折线图" , width = 600 , height = 420 )
date= df[ "date" ] . tolist( )
cpu= df[ "cpu" ] . tolist( )
memory= df[ "memory" ] . tolist( )
print ( '统计信息:\n' , df. describe( ) )
line. add( name = "CPU利用率" , x_axis = date, y_axis = cpu,
line_width = 3 , line_color = 'red' , is_datazoom_show = True )
line. add( name = "内存利用率" , x_axis = date, y_axis = memory,
line_width = 2 , line_color = 'cyan' , is_datazoom_show = True )
line. render( '折线图示范.html' )
print ( "完成" )
cpu利用率平均值是:
12.131579
内存利用率平均值是:
66.594737
统计信息:
cpu memory
count 19.000000 19.000000
mean 12.131579 66.594737
std 7.741522 1.577084
min 2.700000 63.800000
25% 7.350000 65.750000
50% 10.800000 66.300000
75% 12.550000 68.100000
max 31.300000 68.900000
完成