-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapm-tracker.py
More file actions
58 lines (52 loc) · 1.71 KB
/
apm-tracker.py
File metadata and controls
58 lines (52 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import asyncio
from asyncio import events
import datetime
import sys
from pynput.mouse import Listener as MouseListener
from pynput.keyboard import Listener as KeyboardListener
start_time = datetime.datetime.now()
total_events = 0
last_minute = []
events_last_second = 0
running_avg = 0
def on_press(key):
# print(f'Key pressed: {key}')
global events_last_second
events_last_second += 1
def on_click(x, y, button, pressed):
if not pressed:
# print(f'Mouse button clicked: {button}')
global events_last_second
events_last_second += 1
async def apm_tracker():
while True:
await asyncio.sleep(1)
cur = datetime.datetime.now()
global total_events, events_last_second, last_minute
if len(last_minute) < 60:
last_minute.append(events_last_second)
else:
last_minute = last_minute[1:] + [events_last_second]
events_last_minute = sum(last_minute)
total_events += events_last_second
sys.stdout.write("\033[K")
min = (cur - start_time).total_seconds()/60
avg = (total_events/min)
instant_apm = events_last_second*60
print(f'total events: {int(avg)}, {events_last_minute}, {instant_apm}', end='\r')
events_last_second = 0
if __name__ == '__main__':
try:
kbl = KeyboardListener(on_release=on_press)
mbl = MouseListener(on_click=on_click)
kbl.start()
mbl.start()
asyncio.run(apm_tracker())
kbl.join()
mbl.join()
except KeyboardInterrupt:
print('\nFinishing!')
cur = datetime.datetime.now()
min = (cur - start_time).total_seconds()/60
avg = (total_events/min)
print(f'AVG APM: {avg}')