Skip to content

Instantly share code, notes, and snippets.

@nfnty
Last active July 5, 2022 13:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfnty/f49435846e23b909ba166138d2571e1b to your computer and use it in GitHub Desktop.
Save nfnty/f49435846e23b909ba166138d2571e1b to your computer and use it in GitHub Desktop.
evdev latency tester
#!/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()
@nfnty
Copy link
Author

nfnty commented Aug 7, 2016

Instructions

  1. Install python package evdev https://pypi.python.org/pypi/evdev
  2. Add user to group input and relogin
  3. Run script with python3 (replace XXX 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]
  4. Mash buttons like crazy
  5. Ctrl-C to finish

Results

All times are in seconds. It's extremely hard to get consistent results with --press

Kinesis Advantage
--press

Code:     37
Key:      KEY_K
Times:    [0.055956363677978516, 0.0319514274597168, 0.03192853927612305, 0.02394247055053711, 0.0319366455078125, 0.039994001388549805, 0.03990745544433594, 0.04794573783874512, 0.04794478416442871, 0.039955854415893555, 0.0399320125579834, 0.05592536926269531, 0.05594229698181152, 0.05593585968017578, 0.03996729850769043, 0.031961679458618164, 0.039954185485839844, 0.0559544563293457, 0.0560755729675293, 0.04794049263000488, 0.039969682693481445, 0.04793739318847656, 0.05599355697631836, 0.031986236572265625, 0.01594090461730957, 0.03994154930114746, 0.06393647193908691, 0.023947477340698242, 0.01593637466430664, 0.055937767028808594, 0.05593276023864746, 0.007935285568237305, 0.023985862731933594, 0.047914743423461914, 0.016020774841308594, 0.0559389591217041, 0.05593609809875488, 0.05591464042663574]
Max:      0.063936
Min:      0.007935
Mean:     0.041636
Median:   0.039982
Stdev:    0.014423
Variance: 0.000208

--release

Code:     37
Key:      KEY_K
Times:    [0.02403426170349121, 0.015987873077392578, 0.015992164611816406, 0.01598834991455078, 0.01589226722717285, 0.015957117080688477, 0.015887022018432617, 0.007988691329956055, 0.007906675338745117, 0.00798654556274414, 0.01588582992553711, 0.007906198501586914, 0.015938758850097656, 0.007907867431640625, 0.015934228897094727, 0.007963895797729492, 0.00788736343383789, 0.015890836715698242, 0.007990837097167969, 0.007992029190063477, 0.015990734100341797, 0.007898569107055664]
Max:      0.024034
Min:      0.007887
Mean:     0.012673
Median:   0.015886
Stdev:    0.004732
Variance: 0.000022

No-name rubber dome
--press

Code:     37
Key:      KEY_K
Times:    [0.023986339569091797, 0.03206300735473633, 0.0319364070892334, 0.03987598419189453, 0.023916244506835938, 0.03189969062805176, 0.0319371223449707, 0.03991222381591797, 0.03994488716125488, 0.04797530174255371, 0.03992509841918945, 0.0479578971862793, 0.03994250297546387, 0.015959978103637695, 0.023929595947265625, 0.04794001579284668, 0.05608081817626953, 0.03999638557434082, 0.03190183639526367, 0.03992867469787598, 0.039885520935058594, 0.04794120788574219, 0.04795074462890625, 0.047922372817993164, 0.03992056846618652, 0.04795122146606445, 0.04793095588684082, 0.04792141914367676, 0.0559079647064209, 0.11995697021484375, 0.047927141189575195, 0.04002547264099121]
Max:      0.119957
Min:      0.015960
Mean:     0.042445
Median:   0.039944
Stdev:    0.017058
Variance: 0.000291

--release

Code:     37
Key:      KEY_K
Times:    [0.040041208267211914, 0.04005742073059082, 0.13602662086486816, 0.06406569480895996, 0.048058509826660156, 0.04800868034362793, 0.015996932983398438, 0.015928030014038086, 0.024091720581054688, 0.032039642333984375, 0.0240938663482666, 0.01594829559326172, 0.031978607177734375, 0.03208017349243164, 0.024080514907836914, 0.024001359939575195, 0.023950815200805664, 0.024002552032470703, 0.03997302055358887, 0.040038108825683594, 0.032037973403930664, 0.02403402328491211, 0.03199338912963867, 0.04800915718078613, 0.0879526138305664, 0.040052175521850586, 0.048160552978515625, 0.039977073669433594, 0.040026187896728516, 0.04004859924316406, 0.04806089401245117, 0.05604910850524902]
Max:      0.136027
Min:      0.015928
Mean:     0.040027
Median:   0.039975
Stdev:    0.023083
Variance: 0.000533

No-name made of only rubber
--press

Code:     37
Key:      KEY_K
Times:    [0.023886442184448242, 0.03205227851867676, 0.0400390625, 0.05595517158508301, 0.07188773155212402, 0.06393289566040039, 0.03999733924865723, 0.04794597625732422, 0.04787087440490723, 0.03989410400390625, 0.03190445899963379, 0.047931671142578125, 0.04793143272399902, 0.06397867202758789, 0.0639350414276123, 0.06398844718933105, 0.06392407417297363, 0.07191848754882812, 0.0639495849609375, 0.06399989128112793, 0.05593991279602051, 0.0558779239654541, 0.0399327278137207, 0.023931503295898438, 0.07194375991821289, 0.07190871238708496, 0.0639493465423584, 0.06395554542541504, 0.07194304466247559, 0.07199549674987793, 0.07207918167114258, 0.039933204650878906, 0.015917539596557617, 0.06394386291503906, 0.0639197826385498, 0.08794546127319336, 0.07195711135864258, 0.055934906005859375, 0.055890798568725586, 0.07193517684936523, 0.056067705154418945, 0.03198719024658203, 0.024005889892578125, 0.055968284606933594, 0.0479435920715332, 0.05590629577636719, 0.06393885612487793, 0.06393742561340332]
Max:      0.087945
Min:      0.015918
Mean:     0.054948
Median:   0.056018
Stdev:    0.016053
Variance: 0.000258

--release

Code:     37
Key:      KEY_K
Times:    [0.015906095504760742, 0.04802131652832031, 0.04006671905517578, 0.040044307708740234, 0.03213977813720703, 0.0960536003112793, 0.05604743957519531, 0.04799318313598633, 0.040055036544799805, 0.03200578689575195, 0.02410411834716797, 0.015900135040283203, 0.023975372314453125, 0.032036781311035156, 0.024061203002929688, 0.031981468200683594, 0.01595759391784668, 0.032067060470581055, 0.0400547981262207, 0.016032934188842773, 0.01599860191345215, 0.024011850357055664, 0.040064096450805664, 0.04005289077758789, 0.04006242752075195, 0.0320436954498291, 0.024013280868530273, 0.03206944465637207, 0.024073362350463867, 0.024062156677246094, 0.03998827934265137, 0.02406454086303711, 0.03197884559631348, 0.0320131778717041, 0.024046897888183594, 0.023973464965820312, 0.023952245712280273]
Max:      0.096054
Min:      0.015900
Mean:     0.032459
Median:   0.032006
Stdev:    0.014615
Variance: 0.000214

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment