Ticket #32824: textnode_test2.py

File textnode_test2.py, 2.2 KB (added by Keryn Knight, 3 years ago)

yappi/cprofile/timeit oriented template rendering (1 complex template, fake/minimal context)

Line 
1from collections import namedtuple
2
3from django.utils.functional import SimpleLazyObject
4
5import os
6from django.conf import settings
7import django
8from django.template import Template, Context
9
10ROOT = os.path.dirname(django.__file__)
11
12
13def urls():
14 from django.urls import path
15 from django.contrib import admin
16
17 return (path("admin", admin.site.urls),)
18
19
20settings.configure(
21 DEBUG=False,
22 INSTALLED_APPS=(
23 "django.contrib.contenttypes",
24 "django.contrib.auth",
25 "django.contrib.admin",
26 ),
27 ROOT_URLCONF=SimpleLazyObject(urls),
28 TEMPLATES=[
29 {
30 "BACKEND": "django.template.backends.django.DjangoTemplates",
31 "DIRS": [],
32 "APP_DIRS": True,
33 "OPTIONS": {
34 "context_processors": [
35 "django.template.context_processors.request",
36 "django.contrib.auth.context_processors.auth",
37 "django.contrib.messages.context_processors.messages",
38 ],
39 },
40 },
41 ],
42)
43django.setup()
44
45
46changelist_path = os.path.join(
47 ROOT, "contrib", "admin", "templates", "admin", "change_list.html"
48)
49with open(changelist_path, "r") as f:
50 changelist = f.read()
51
52t = Template(changelist)
53fakeopts = namedtuple("Opts", "app_label object_name")("auth", "user")
54
55
56class ChangeList:
57 result_count = 0
58 full_result_count = 0
59 opts = fakeopts
60 list_display = ()
61 formset = None
62 result_list = ()
63 show_all = False
64 multi_page = False
65 can_show_all = True
66
67 def get_ordering_field_columns(self):
68 return []
69
70
71ctx = Context({"cl": ChangeList(), "opts": fakeopts})
72
73
74def yappi():
75 import yappi as pr
76
77 pr.start()
78
79 for x in range(1_000):
80 t.render(ctx)
81
82 pr.stop()
83 pr.get_func_stats().print_all()
84
85
86def cprofile():
87 import cProfile as profiler
88
89 pr = profiler.Profile()
90 pr.enable()
91
92 for x in range(1_000):
93 t.render(ctx)
94
95 pr.disable()
96 pr.print_stats()
97
98
99def timeonly():
100 for x in range(1_000):
101 t.render(ctx)
102
103
104if __name__ == "__main__":
105 # For checking walltime via CLI `time python ...`
106 for x in range(1_000):
107 t.render(ctx)
Back to Top