Last active
July 5, 2022 13:25
-
-
Save nfnty/f49435846e23b909ba166138d2571e1b to your computer and use it in GitHub Desktop.
evdev latency tester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
''' evdev latency tester ''' | |
import argparse | |
import statistics | |
import evdev | |
def args_parse(arguments=None): | |
''' Parse arguments ''' | |
par0 = argparse.ArgumentParser(description='evdev latency tester') | |
method = par0.add_mutually_exclusive_group(required=True) | |
method.add_argument('--press', action='store_true', help='Measure Press') | |
method.add_argument('--release', action='store_true', help='Measure Release') | |
par0.add_argument('device', help='/dev/input/by-id/usb-05f3_0007-event-kbd') | |
return par0.parse_args(arguments) | |
KEY_PRESS = 1 | |
KEY_RELEASE = 0 | |
def keys_read(device): | |
''' Read keys ''' | |
keys = {} | |
try: | |
for event in device.read_loop(): | |
key = keys.setdefault(event.code, {'Times': [], 'Event': None, 'Time': None}) | |
if key['Event'] is None: | |
if ARGS.press: | |
if event.value != KEY_RELEASE: | |
continue | |
elif ARGS.release: | |
if event.value != KEY_PRESS: | |
continue | |
key['Event'] = event.value | |
key['Time'] = event.timestamp() | |
continue | |
elif key['Event'] == KEY_PRESS: | |
if event.value == KEY_PRESS: | |
continue | |
elif key['Event'] == KEY_RELEASE: | |
if event.value == KEY_RELEASE: | |
continue | |
key['Times'] += [event.timestamp() - key['Time']] | |
key.update({'Event': None, 'Time': None}) | |
except KeyboardInterrupt: | |
pass | |
return {key: value['Times'] for key, value in keys.items()} | |
def key_printable(code, times): | |
''' Transform key into printable string ''' | |
string = ( | |
'\n' | |
'Code: {0:d}\n' | |
'Key: {1:s}\n' | |
'Times: {2:s}\n' | |
'Max: {3:f}\n' | |
'Min: {4:f}\n' | |
'Mean: {5:f}\n' | |
'Median: {6:f}'.format( | |
code, | |
str(evdev.ecodes.keys.get(code)), | |
str(times), | |
max(times), | |
min(times), | |
statistics.mean(times), | |
statistics.median(times), | |
) | |
) | |
if len(times) >= 2: | |
string += ( | |
'\n' | |
'Stdev: {0:f}\n' | |
'Variance: {1:f}'.format( | |
statistics.stdev(times), | |
statistics.variance(times), | |
) | |
) | |
return string | |
def main(): | |
''' Main ''' | |
device = evdev.InputDevice(ARGS.device) | |
keys = keys_read(device) | |
for code, times in keys.items(): | |
if not times: | |
continue | |
print(key_printable(code, times)) | |
if __name__ == '__main__': | |
ARGS = args_parse() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions
input
and reloginpython3
(replaceXXX
with your device):python3 evdev_latency.py --press /dev/input/by-id/usb-XXX
to measure delta between [release, press]python3 evdev_latency.py --release /dev/input/by-id/usb-XXX
to measure delta between [press, release]Ctrl-C
to finishResults
All times are in seconds. It's extremely hard to get consistent results with
--press
Kinesis Advantage
--press
--release
No-name rubber dome
--press
--release
No-name made of only rubber
--press
--release