mirror of
https://github.com/django-components/django-components.git
synced 2025-07-16 04:54:59 +00:00
Performance (+50%): Compile ComponentNode at creation, not render (#22)
Co-authored-by: rbeard0330 <@dul2k3BKW6m>
This commit is contained in:
parent
d61c0fa469
commit
87f9994c81
4 changed files with 66 additions and 17 deletions
42
benchmarks/component_rendering.py
Normal file
42
benchmarks/component_rendering.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
from time import perf_counter
|
||||
|
||||
from django.template import Context, Template
|
||||
|
||||
from django_components import component
|
||||
|
||||
from tests.django_test_setup import * # NOQA
|
||||
from tests.testutils import Django111CompatibleSimpleTestCase as SimpleTestCase
|
||||
|
||||
|
||||
class SlottedComponent(component.Component):
|
||||
def template(self, context):
|
||||
return "slotted_template.html"
|
||||
|
||||
|
||||
class SimpleComponent(component.Component):
|
||||
def context(self, variable, variable2="default"):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
}
|
||||
|
||||
def template(self, context):
|
||||
return "simple_template.html"
|
||||
|
||||
|
||||
class RenderBenchmarks(SimpleTestCase):
|
||||
def setUp(self):
|
||||
component.registry.clear()
|
||||
component.registry.register('test_component', SlottedComponent)
|
||||
component.registry.register('inner_component', SimpleComponent)
|
||||
|
||||
def test_render_time(self):
|
||||
template = Template("{% load component_tags %}{% component_block 'test_component' %}"
|
||||
"{% slot \"header\" %}{% component 'inner_component' variable='foo' %}{% endslot %}"
|
||||
"{% endcomponent_block %}", name='root')
|
||||
start_time = perf_counter()
|
||||
for _ in range(1000):
|
||||
template.render(Context({}))
|
||||
end_time = perf_counter()
|
||||
total_elapsed = end_time - start_time # NOQA
|
||||
print(f'{total_elapsed } ms per template')
|
Loading…
Add table
Add a link
Reference in a new issue