implemented pipeline in cotton loader for processors

This commit is contained in:
Will Abbott 2024-09-15 09:38:20 +01:00
parent 32dab2e1ae
commit a6a68831f4
4 changed files with 93 additions and 121 deletions

View file

@ -1,5 +1,6 @@
import time
from django.template.loader import render_to_string
from statistics import mean
def configure_django():
@ -19,7 +20,6 @@ def configure_django():
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": ["example_project/templates"],
# "APP_DIRS": True,
"OPTIONS": {
"loaders": [
(
@ -53,62 +53,83 @@ def configure_django():
print(f"DEBUG: {settings.DEBUG}")
def template_bench(template_name, iterations=5000):
start_time = time.time()
for _ in range(iterations):
render_to_string(template_name)
end_time = time.time()
duration = round((end_time - start_time) * 1000, 2)
return duration
def template_bench_data_loop(template_name, iterations=5000):
data = list(range(1, iterations))
start_time = time.time()
render_to_string(template_name, context={"data": data})
end_time = time.time()
duration = round((end_time - start_time) * 1000, 2)
return duration
def run_benchmark(bench_func, template_name, iterations=5000, runs=5):
# Warm up
bench_func(template_name, iterations=1)
results = []
for _ in range(runs):
result = bench_func(template_name, iterations)
results.append(result)
return mean(results)
def main():
configure_django()
def template_bench(template_name, iterations=5000):
start_time = time.time()
for _ in range(iterations):
render_to_string(template_name)
end_time = time.time()
duration = round((end_time - start_time) * 1000, 2)
runs = 5
iterations = 5000
return duration, render_to_string(template_name)
print(f"Running benchmarks with {runs} runs, {iterations} iterations each")
def template_bench_data_loop(template_name, iterations=5000):
data = list(range(1, iterations))
start_time = time.time()
render_to_string(template_name, context={"data": data})
end_time = time.time()
duration = round((end_time - start_time) * 1000, 2)
return duration, render_to_string(template_name)
# warm caches
template_bench_data_loop("simple_native.html", iterations=1)
template_bench_data_loop("simple_cotton.html", iterations=1)
simple_native, _ = template_bench_data_loop("simple_native.html")
simple_cotton, _ = template_bench_data_loop("simple_cotton.html")
simple_native = run_benchmark(
template_bench_data_loop, "simple_native.html", iterations, runs
)
simple_cotton = run_benchmark(
template_bench_data_loop, "simple_cotton.html", iterations, runs
)
print("---")
print(f"Native Django {{% for %}} loop: {simple_native} ms")
print(f"Cotton {{% for %}} loop: {simple_cotton} ms")
print(f"Native Django {{% for %}} loop: {simple_native:.2f} ms")
print(f"Cotton {{% for %}} loop: {simple_cotton:.2f} ms")
# warm caches
template_bench("benchmarks/native_include.html", iterations=1)
template_bench("cotton/benchmarks/cotton_include.html", iterations=1)
time_native_include, _ = template_bench("benchmarks/native_include.html")
time_cotton_include, _ = template_bench("cotton/benchmarks/cotton_include.html")
time_native_include = run_benchmark(
template_bench, "benchmarks/native_include.html", iterations, runs
)
time_cotton_include = run_benchmark(
template_bench, "cotton/benchmarks/cotton_include.html", iterations, runs
)
print("---")
print(f"Native {{% include %}}: {time_native_include} ms")
print(f"Cotton for include: {time_cotton_include} ms")
print(f"Native {{% include %}}: {time_native_include:.2f} ms")
print(f"Cotton for include: {time_cotton_include:.2f} ms")
# warm caches
template_bench("benchmarks/native_extends.html", iterations=1)
template_bench("cotton/benchmarks/cotton_compiled.html", iterations=1)
template_bench("cotton/benchmarks/cotton_extends_equivalent.html", iterations=1)
time_native_extends, _ = template_bench("benchmarks/native_extends.html")
time_compiled_cotton, _ = template_bench("cotton/benchmarks/cotton_compiled.html")
time_cotton, _ = template_bench("cotton/benchmarks/cotton_extends_equivalent.html")
time_native_extends = run_benchmark(
template_bench, "benchmarks/native_extends.html", iterations, runs
)
time_compiled_cotton = run_benchmark(
template_bench, "cotton/benchmarks/cotton_compiled.html", iterations, runs
)
time_cotton = run_benchmark(
template_bench,
"cotton/benchmarks/cotton_extends_equivalent.html",
iterations,
runs,
)
print("---")
print(f"Native {{% block %}} and {{% extends %}}: {time_native_extends} ms")
print(f"Compiled Cotton Template: {time_compiled_cotton} ms")
print(f"Uncompiled Cotton Template: {time_cotton} ms")
print(f"Native {{% block %}} and {{% extends %}}: {time_native_extends:.2f} ms")
print(f"Compiled Cotton Template: {time_compiled_cotton:.2f} ms")
print(f"Uncompiled Cotton Template: {time_cotton:.2f} ms")
if __name__ == "__main__":