Lejoni icon

Python Script to gauge average CPU usage over time.

Lejoni | PRO | 03/13/20 11:09:57 AM UTC | -1 ⭐ | 306 👁️ | Never ⏰ | []
Python |

870 B

|

None

|

0 👍

/

1 👎

#!/usr/bin/env python3
from psutil import cpu_percent as cpu
from sys import argv
 
if len(argv) > 1:
    cycles = int(argv[1])
else:
    cycles = 10
 
total_load_list = []
def usage():
    for i in range(cycles):
        load_list = cpu(interval=0.5, percpu=True)
        for v in range(len(load_list)):
            if len(load_list) > len(total_load_list):
                total_load_list.append(load_list[v])
            else:
                total_load_list[v] = total_load_list[v] + load_list[v]
        for j in range(len(load_list)):
            print(str("CPU"+str(j)+":").ljust(7)+str(load_list[j]).rjust(4)+"%")
    
    print("Total Avrage over "+str(cycles)+" cycles.")
    for c in range(len(total_load_list)):
        print(str("CPU"+str(c)+":").ljust(7)+str(round(total_load_list[c]/cycles,1)).rjust(4)+"%")
 
 
if __name__ == '__main__':
    usage()

Comments