django-cotton/dev/example_project/render_load_test.py
2024-06-08 14:33:11 +01:00

71 lines
2.4 KiB
Python

from django.conf import settings
import time
from django.template.loader import render_to_string
import django
# Configure Django settings
settings.configure(
INSTALLED_APPS=[
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django_cotton",
],
TEMPLATES=[
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": ["example_project/templates"],
"APP_DIRS": False,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
"loaders": [
"django_cotton.cotton_loader.Loader",
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
"builtins": [
"django.templatetags.static",
"django_cotton.templatetags.cotton",
],
},
},
],
)
django.setup()
def benchmark_template_rendering(template_name, iterations=1000):
start_time = time.time()
for _ in range(iterations):
render_to_string(template_name)
end_time = time.time()
return end_time - start_time, render_to_string(template_name)
# Benchmarking each template
time_native_extends, output_native_extends = benchmark_template_rendering(
"cotton/benchmarks/native_extends.html"
)
# time_native_include, output_native_include = benchmark_template_rendering('cotton/benchmarks/native_include.html')
time_compiled_cotton, output_compiled_cotton = benchmark_template_rendering(
"cotton/benchmarks/cotton_compiled.html"
)
time_cotton, output_cotton = benchmark_template_rendering(
"cotton/benchmarks/cotton.cotton.html"
)
# Output results
print(f"Native Django Template using extend: {time_native_extends} seconds")
# print(f"Native Django Template using include: {time_native_include} seconds")
print(f"Compiled Cotton Template: {time_compiled_cotton} seconds")
print(f"Cotton Template: {time_cotton} seconds")