Skip to content

Conversation

@glennmatthews
Copy link
Contributor

Discovered this while investigating startup performance of the nautobot-server command as a part of nautobot/nautobot#4292. I found that the netutils.utils.jinja2_convenience_function() function, which is called as a part of Nautobot's startup sequence, turned out to be surprisingly expensive to call, the reason being that jinja2_convenience_function() imports (but does not itself call) netutils.lib_helpers.get_napalm_getters(), which in turn automatically imports the entire NAPALM library.

Importing napalm takes anywhere from 0.5 to 1 second all by itself, and we don't actually need that library imported until we actually call get_napalm_getters(). My solution here is to simply move the import inside the get_napalm_getters() function so that it runs when the function is called, rather then simply when the function is initially imported. This makes jinja2_convenience_function() notably faster to call.

(edit on noticing CI failure - Why are there tests that CI includes that aren't run by invoke tests? I ran invoke tests successfully locally before pushing this branch up. I'll investigate, but that's a bit annoying. 😄 )

@itdependsnetworks
Copy link
Contributor

The reason is we run optional tests as these are optional dependencies. I am not sure this is the best pattern, but what we have done so far.

https://github.com/networktocode/netutils/blob/develop/.github/workflows/ci.yml#L207-L210

@glennmatthews glennmatthews merged commit eba8a55 into develop Feb 27, 2024
@glennmatthews glennmatthews deleted the u/glennmatthews-improve-jinja2_convenience_function-speed branch February 27, 2024 21:52
michalis1 pushed a commit to michalis1/netutils that referenced this pull request Feb 2, 2026
…APALM when called (networktocode#466)

* Improve performance of jinja2_convenience_function by not importing NAPALM unnecessarily

* Fix optional test as well
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants