2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-20 14:41:47 +00:00
|
|
|
import random
|
|
|
|
import psutil
|
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
STEP_SIZE = 3
|
2018-09-20 14:41:47 +00:00
|
|
|
SAMPLES = 300
|
|
|
|
SAMPLE_MAX = 500
|
2019-10-23 20:10:03 +00:00
|
|
|
CANVAS_SIZE = (300, 200)
|
2018-09-20 14:41:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
g_interval = .25
|
|
|
|
g_cpu_percent = 0
|
|
|
|
g_procs = None
|
|
|
|
g_exit = False
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-09-20 14:41:47 +00:00
|
|
|
def CPU_thread(args):
|
|
|
|
global g_interval, g_cpu_percent, g_procs, g_exit
|
|
|
|
|
|
|
|
while not g_exit:
|
|
|
|
try:
|
|
|
|
g_cpu_percent = psutil.cpu_percent(interval=g_interval)
|
|
|
|
g_procs = psutil.process_iter()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-09-20 14:41:47 +00:00
|
|
|
def main():
|
|
|
|
global g_exit, g_response_time
|
|
|
|
# start ping measurement thread
|
|
|
|
|
2019-12-24 23:52:47 +00:00
|
|
|
sg.theme('Black')
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.set_options(element_padding=(0, 0))
|
2018-09-20 14:41:47 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [
|
|
|
|
[sg.Quit(button_color=('white', 'black')),
|
|
|
|
sg.Text('', pad=((100, 0), 0), font='Any 15', key='output')],
|
|
|
|
[sg.Graph(CANVAS_SIZE, (0, 0), (SAMPLES, SAMPLE_MAX),
|
|
|
|
background_color='black', key='graph')]
|
|
|
|
]
|
2018-09-20 14:41:47 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('CPU Graph', layout,
|
|
|
|
grab_anywhere=True, keep_on_top=True,
|
|
|
|
background_color='black', no_titlebar=True,
|
|
|
|
use_default_focus=False)
|
2018-09-20 14:41:47 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
graph = window['graph']
|
|
|
|
output = window['output']
|
2018-09-20 14:41:47 +00:00
|
|
|
# start cpu measurement thread
|
2019-10-23 20:10:03 +00:00
|
|
|
thread = Thread(target=CPU_thread, args=(None,))
|
2018-09-20 14:41:47 +00:00
|
|
|
thread.start()
|
|
|
|
|
|
|
|
last_cpu = i = 0
|
|
|
|
prev_x, prev_y = 0, 0
|
|
|
|
while True: # the Event Loop
|
2020-05-07 10:22:59 +00:00
|
|
|
event, values = window.read(timeout=500)
|
2019-10-23 20:10:03 +00:00
|
|
|
if event in ('Quit', None): # always give ths user a way out
|
2018-09-20 14:41:47 +00:00
|
|
|
break
|
|
|
|
# do CPU measurement and graph it
|
|
|
|
current_cpu = int(g_cpu_percent*10)
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-09-20 14:41:47 +00:00
|
|
|
if current_cpu == last_cpu:
|
|
|
|
continue
|
2019-10-23 20:10:03 +00:00
|
|
|
# show current cpu usage at top
|
|
|
|
output.update(current_cpu/10)
|
|
|
|
|
2018-09-20 14:41:47 +00:00
|
|
|
if current_cpu > SAMPLE_MAX:
|
|
|
|
current_cpu = SAMPLE_MAX
|
|
|
|
new_x, new_y = i, current_cpu
|
2019-10-23 20:10:03 +00:00
|
|
|
|
2018-09-20 14:41:47 +00:00
|
|
|
if i >= SAMPLES:
|
2019-10-23 20:10:03 +00:00
|
|
|
# shift graph over if full of data
|
|
|
|
graph.move(-STEP_SIZE, 0)
|
2018-09-20 14:41:47 +00:00
|
|
|
prev_x = prev_x - STEP_SIZE
|
2019-10-23 20:10:03 +00:00
|
|
|
graph.draw_line((prev_x, prev_y), (new_x, new_y), color='white')
|
2018-09-20 14:41:47 +00:00
|
|
|
prev_x, prev_y = new_x, new_y
|
|
|
|
i += STEP_SIZE if i < SAMPLES else 0
|
|
|
|
last_cpu = current_cpu
|
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
g_exit = True
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|
|
|
|
|
|
|
|
|
2018-09-20 14:41:47 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|