Adding upstream version 0.45+dfsg.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
b4efa209be
commit
eb42e29864
35 changed files with 4489 additions and 0 deletions
4
benchmark/Card.jinja
Normal file
4
benchmark/Card.jinja
Normal file
|
@ -0,0 +1,4 @@
|
|||
<section class="card">
|
||||
{{ content }}
|
||||
<CloseBtn disabled />
|
||||
</section>
|
2
benchmark/CloseBtn.jinja
Normal file
2
benchmark/CloseBtn.jinja
Normal file
|
@ -0,0 +1,2 @@
|
|||
{#def disabled=False -#}
|
||||
<button type="button"{{ " disabled" if disabled else "" }}>×</button>
|
2
benchmark/Greeting.jinja
Normal file
2
benchmark/Greeting.jinja
Normal file
|
@ -0,0 +1,2 @@
|
|||
{#def message #}
|
||||
<div class="greeting [&_a]:flex">{{ message }}</div>
|
13
benchmark/Layout.jinja
Normal file
13
benchmark/Layout.jinja
Normal file
|
@ -0,0 +1,13 @@
|
|||
{#def title #}
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport" />
|
||||
<title>{{ title }}</title>
|
||||
</head>
|
||||
<body>
|
||||
{{ content }}
|
||||
</body>
|
||||
</html>
|
8
benchmark/Real.jinja
Normal file
8
benchmark/Real.jinja
Normal file
|
@ -0,0 +1,8 @@
|
|||
{#def message #}
|
||||
|
||||
<Layout title="Hello">
|
||||
<Card>
|
||||
<Greeting message={{ message }} />
|
||||
<button type="button">Close</button>
|
||||
</Card>
|
||||
</Layout>
|
16
benchmark/Simple.jinja
Normal file
16
benchmark/Simple.jinja
Normal file
|
@ -0,0 +1,16 @@
|
|||
{#def message #}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport" />
|
||||
<title>{{ message }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<section class="card">
|
||||
<div class="greeting [&_a]:flex">{{ message }}</div>
|
||||
<button type="button">Close</button>
|
||||
<button type="button" disabled>×</button>
|
||||
</section>
|
||||
</body>
|
||||
</html>
|
93
benchmark/benchmark.py
Normal file
93
benchmark/benchmark.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
import timeit
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from jinjax import Catalog
|
||||
|
||||
|
||||
here = Path(__file__).parent
|
||||
number = 10_000
|
||||
|
||||
catalog = Catalog()
|
||||
catalog.add_folder(here)
|
||||
|
||||
env = Environment(loader=FileSystemLoader(here))
|
||||
|
||||
templates = Jinja2Templates(directory=here)
|
||||
|
||||
|
||||
def render_jinjax_simple():
|
||||
"""simple case"""
|
||||
catalog.render("Simple", message="Hey there")
|
||||
|
||||
|
||||
def render_jinjax_real():
|
||||
"""realistic case"""
|
||||
catalog.render("Real", message="Hey there")
|
||||
|
||||
|
||||
def render_jinja():
|
||||
env.get_template("hello.html").render(message="Hey there")
|
||||
|
||||
|
||||
def render_fastapi():
|
||||
templates.TemplateResponse("hello.html", {"request": None, "message": "Hey there"})
|
||||
|
||||
|
||||
def benchmark_no_cache(func):
|
||||
print(f"NO CACHE: {number:_} renders of {func.__doc__}...\n")
|
||||
catalog.use_cache = False
|
||||
benchmark(func)
|
||||
|
||||
|
||||
def benchmark_auto_reload(func):
|
||||
print(f"CACHE, AUTO-RELOAD: {number:_} renders of {func.__doc__}...\n")
|
||||
catalog.use_cache = True
|
||||
catalog.auto_reload = True
|
||||
benchmark(func)
|
||||
|
||||
|
||||
def benchmark_no_auto_reload(func):
|
||||
print(f"CACHE, NO AUTO-RELOAD: {number:_} renders of {func.__doc__}...\n")
|
||||
catalog.use_cache = True
|
||||
catalog.auto_reload = False
|
||||
benchmark(func)
|
||||
|
||||
|
||||
def benchmark(func):
|
||||
time_jinjax = timeit.timeit(func, number=number)
|
||||
print_line("JinjaX", time_jinjax)
|
||||
print(f"{time_jinjax / time_jinja:.1f} times Jinja")
|
||||
print(f"{time_jinjax / time_fastapi:.1f} times FastApi")
|
||||
|
||||
|
||||
def print_line(name, time):
|
||||
print(f"{name}: {(time / number):.12f}s per render ({(1_000_000 * time / number):.0f}µs), {time:.1f}s total")
|
||||
|
||||
|
||||
def print_separator():
|
||||
print()
|
||||
print("-" * 60)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"Benchmarking...\n")
|
||||
time_jinja = timeit.timeit(render_jinja, number=number)
|
||||
time_fastapi = timeit.timeit(render_fastapi, number=number)
|
||||
|
||||
print_line("Jinja", time_jinja)
|
||||
print_line("FastApi", time_fastapi)
|
||||
print_separator()
|
||||
benchmark_no_cache(render_jinjax_simple)
|
||||
print_separator()
|
||||
benchmark_auto_reload(render_jinjax_simple)
|
||||
print_separator()
|
||||
benchmark_no_auto_reload(render_jinjax_simple)
|
||||
print_separator()
|
||||
benchmark_no_cache(render_jinjax_real)
|
||||
print_separator()
|
||||
benchmark_auto_reload(render_jinjax_real)
|
||||
print_separator()
|
||||
benchmark_no_auto_reload(render_jinjax_real)
|
||||
print()
|
15
benchmark/hello.html
Normal file
15
benchmark/hello.html
Normal file
|
@ -0,0 +1,15 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport" />
|
||||
<title>{{ message }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<section class="card">
|
||||
<div class="greeting [&_a]:flex">{{ message }}</div>
|
||||
<button type="button">Close</button>
|
||||
<button type="button" disabled>×</button>
|
||||
</section>
|
||||
</body>
|
||||
</html>
|
29
benchmark/profile.py
Normal file
29
benchmark/profile.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
from pathlib import Path
|
||||
|
||||
from jinjax import Catalog, Component
|
||||
from line_profiler import LineProfiler
|
||||
|
||||
|
||||
HERE = Path(__file__).parent
|
||||
|
||||
catalog = Catalog()
|
||||
catalog.add_folder(HERE)
|
||||
|
||||
profile = LineProfiler(
|
||||
Catalog.irender,
|
||||
Catalog._get_from_file,
|
||||
Component.__init__,
|
||||
Component.from_cache,
|
||||
Component.filter_args,
|
||||
Component.render,
|
||||
)
|
||||
|
||||
def render_jinjax():
|
||||
for _ in range(1000):
|
||||
catalog.render("Hello", message="Hey there")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Profiling...")
|
||||
profile.runcall(render_jinjax)
|
||||
profile.print_stats()
|
Loading…
Add table
Add a link
Reference in a new issue