-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rendering config template is slow #15033
Replies: 1 comment · 7 replies
-
Every time you call In order to speed it up, you could do a bulk query to get all the required templates in one go:
then for each device d, use the config template For completeness, for devices which don't have their own config template defined you'd fallback to the template for the role or platform. See the logic in get_config_template. |
Beta Was this translation helpful? Give feedback.
All reactions
-
I added your 3 suggested lines at the beginning, and then added this function: def preload():
for device in devices:
tid[device.config_template_id].render({"device": device}) Running |
Beta Was this translation helpful? Give feedback.
All reactions
-
Then maybe config_template.render is doing a SQL query. I see in
Or maybe the template itself is accessing some attribute on 'device' which requires a database lookup. If you want to know, then turn on SQL logging. |
Beta Was this translation helpful? Give feedback.
All reactions
-
I did some more investigation and found this issue is mostly unrelated to the database. There is a small performance gain by pre-loading the templates as you suggested before. But the bigger difference is that Netbox import timeit
import jinja2
import cProfile
import pstats
d = Device.objects.get(id=8)
ct = ConfigTemplate.objects.get(id=2)
templateEnv = jinja2.Environment(loader=jinja2.BaseLoader)
template = templateEnv.from_string(ct.template_code)
t1 = timeit.Timer("ct.render({'device': d})", globals=globals())
t2 = timeit.Timer("template.render({'device': d})", globals=globals())
cProfile.runctx("t1.timeit(100)", globals(), locals(), 'results')
pstats.Stats('results').sort_stats(pstats.SortKey.CUMULATIVE).print_stats()
cProfile.runctx("t2.timeit(100)", globals(), locals(), 'results')
pstats.Stats('results').sort_stats(pstats.SortKey.CUMULATIVE).print_stats() jinja: 10810 function calls (10610 primitive calls) in 0.006 seconds
netbox: 564463 function calls (529308 primitive calls) in 0.329 seconds
|
Beta Was this translation helpful? Give feedback.
All reactions
-
@jeremystretch - any comments on the discovery re template rendering performance discussed here? |
Beta Was this translation helpful? Give feedback.
All reactions
-
Do I have any thoughts on this thread from February 2024, on which I've never commented? No, I don't. Please don't ping me for random asks. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I started building a script that renders the config template for all devices every time any device is modified. I notice this is fairly slow, so I built a simple test with jinja2 and found that it's about 90x faster. I'm curious if there's something I can do to improve this? I like the simplicity of Config Templates.
With the below code running on 113 devices, the config_template method takes 0.28 seconds, while the jinja method takes 0.0032 seconds. The results are repeatable and scale near linearly if I increase the Timer repetitions.
Beta Was this translation helpful? Give feedback.
All reactions