Many requests in sequence are slow due to repeated basic authentication
jobec opened this issue · 5 comments
When doing a lot of requests in sequence, the total time it takes for all requests is rather slow. e.g. fetching a list of devices matching a regex and then for each of them fetching their neighbors. I'm guessing this is because every request contains it's own authentication header forcing NetMRI to authenticate again.
When initializing the class a call should be made to the authenticate API (/api/authenticate), cache the cookie and use it for subsequent requests which should speed things up.
Yes, that is a good point. That is especially important if you use external auth.
We authenticate against AD through LDAP.
To give some figures: Fetching 10 devices and then fetching their neighbors takes 19 seconds against a virtual netmri with a low amount of managed devices from within pycharm and over a 1Gbps WAN link.
One thing to try before this gets implemented. On some API calls, you will see that it accepts an "include" parameter. This will allow you to get everything in one request. So, passing an include value of "device" to the interfaces/index method will return another item in the dictionary called "device" which will contain an array of devices corresponding to the interfaces returned by the main query (you still need to stitch them together via DeviceID).
You can list multiple values in the include. For example, "device,aggr_interface" for interfaces would include both the device like above, and also the port channel interface for any interfaces in the main query that are members of a link aggregation.
For my use case this doesn't work.
Below is a snippet of code that takes ~19 seconds. Or 2 seconds per request.
end_hosts = netmri.api_request('devices/find', {'op_DeviceName': 'like',
'val_c_DeviceName': end_host + '%',
'limit': 10})
for end_host in end_hosts['devices']:
neighbor = netmri.api_request('neighbors/index',
{'DeviceID': [end_host['DeviceID']],
'include': ['neighbor_device',
'neighbor_interface']})
In my fork (still local on my workstation) I rewrote things to authenticate first and cache the cookie and then only the authentication step takes a couple of seconds and the other requests take about ~0.3 seconds.
Merged. Though we should bump the version now, and update netmri-toolkit examples for the new init signature.