1 | import sys
|
---|
2 | import timeit
|
---|
3 | from collections import namedtuple
|
---|
4 |
|
---|
5 | from django.utils.functional import SimpleLazyObject
|
---|
6 |
|
---|
7 | ALREADY_PROFILING = 'cProfile' in sys.modules
|
---|
8 | if not ALREADY_PROFILING:
|
---|
9 | import cProfile
|
---|
10 | import os
|
---|
11 | from django.conf import settings
|
---|
12 | import django
|
---|
13 | from django.template import Template, Context
|
---|
14 |
|
---|
15 | ROOT = os.path.dirname(django.__file__)
|
---|
16 |
|
---|
17 | def urls():
|
---|
18 | from django.urls import path
|
---|
19 | from django.contrib import admin
|
---|
20 | return (
|
---|
21 | path('admin', admin.site.urls),
|
---|
22 | )
|
---|
23 |
|
---|
24 | settings.configure(
|
---|
25 | DEBUG=False,
|
---|
26 | INSTALLED_APPS=('django.contrib.contenttypes', 'django.contrib.auth', 'django.contrib.admin',),
|
---|
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 | )
|
---|
43 | django.setup()
|
---|
44 |
|
---|
45 |
|
---|
46 | changelist_path = os.path.join(ROOT, 'contrib', 'admin', 'templates', 'admin', 'change_list.html')
|
---|
47 | with open(changelist_path, 'r') as f:
|
---|
48 | changelist = f.read()
|
---|
49 |
|
---|
50 | templates = {
|
---|
51 | 'admin_changelist': Template(changelist),
|
---|
52 | 'single_textnode': Template(
|
---|
53 | """
|
---|
54 | <!doctype html>
|
---|
55 | <html class="no-js" lang="">
|
---|
56 |
|
---|
57 | <head>
|
---|
58 | <meta charset="utf-8">
|
---|
59 | <title></title>
|
---|
60 | <meta name="description" content="">
|
---|
61 | <meta name="viewport" content="width=device-width, initial-scale=1">
|
---|
62 |
|
---|
63 | <meta property="og:title" content="">
|
---|
64 | <meta property="og:type" content="">
|
---|
65 | <meta property="og:url" content="">
|
---|
66 | <meta property="og:image" content="">
|
---|
67 |
|
---|
68 | <link rel="manifest" href="site.webmanifest">
|
---|
69 | <link rel="apple-touch-icon" href="icon.png">
|
---|
70 | <!-- Place favicon.ico in the root directory -->
|
---|
71 |
|
---|
72 | <link rel="stylesheet" href="css/normalize.css">
|
---|
73 | <link rel="stylesheet" href="css/style.css">
|
---|
74 |
|
---|
75 | <meta name="theme-color" content="#fafafa">
|
---|
76 | </head>
|
---|
77 | <body>
|
---|
78 |
|
---|
79 | <!-- Add your site or application content here -->
|
---|
80 | <p>Hello world! This is HTML5 Boilerplate.</p>
|
---|
81 | <script src="js/vendor/modernizr-3.11.7.min.js"></script>
|
---|
82 | <script src="js/app.js"></script>
|
---|
83 | <!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
|
---|
84 | <script>
|
---|
85 | window.ga = function () { ga.q.push(arguments) }; ga.q = []; ga.l = +new Date;
|
---|
86 | ga('create', 'UA-XXXXX-Y', 'auto'); ga('set', 'anonymizeIp', true); ga('set', 'transport', 'beacon'); ga('send', 'pageview')
|
---|
87 | </script>
|
---|
88 | <script src="https://www.google-analytics.com/analytics.js" async></script>
|
---|
89 | </body>
|
---|
90 | </html>
|
---|
91 | """
|
---|
92 | ),
|
---|
93 | }
|
---|
94 | fakeopts = namedtuple('Opts', 'app_label object_name')('auth', 'user')
|
---|
95 | class ChangeList:
|
---|
96 | result_count = 0
|
---|
97 | full_result_count = 0
|
---|
98 | opts = fakeopts
|
---|
99 | list_display = ()
|
---|
100 | formset = None
|
---|
101 | result_list = ()
|
---|
102 | show_all = False
|
---|
103 | multi_page = False
|
---|
104 | can_show_all = True
|
---|
105 | def get_ordering_field_columns(self):
|
---|
106 | return []
|
---|
107 | ctx = Context({
|
---|
108 | 'cl': ChangeList(),
|
---|
109 | 'opts': fakeopts
|
---|
110 | })
|
---|
111 |
|
---|
112 | if __name__ == "__main__":
|
---|
113 | args_count = len(sys.argv)
|
---|
114 | runner = 'profile'
|
---|
115 | template = 'single_textnode'
|
---|
116 | if args_count > 1:
|
---|
117 | runner = sys.argv[1]
|
---|
118 | if args_count > 2:
|
---|
119 | template = sys.argv[2]
|
---|
120 |
|
---|
121 | if template not in templates:
|
---|
122 | sys.stderr.write("Expected one of\n{0!s}\nfor template name\n".format(", ".join(templates)))
|
---|
123 | sys.exit(1)
|
---|
124 |
|
---|
125 | if runner == 'profile':
|
---|
126 | sys.stdout.write("Running cProfile for\n{0!s}\n".format(template))
|
---|
127 |
|
---|
128 | if not ALREADY_PROFILING:
|
---|
129 | sys.stdout.write("Starting cProfile".format(template))
|
---|
130 | pr = cProfile.Profile()
|
---|
131 | pr.enable()
|
---|
132 |
|
---|
133 | def profile_many():
|
---|
134 | t = templates[template]
|
---|
135 | for x in range(100_000):
|
---|
136 | t.render(ctx)
|
---|
137 |
|
---|
138 | profile_many()
|
---|
139 |
|
---|
140 | if not ALREADY_PROFILING:
|
---|
141 | pr.disable()
|
---|
142 | sys.stdout.write("Stopping cProfile".format(template))
|
---|
143 | pr.print_stats()
|
---|
144 |
|
---|
145 | elif runner == 'timeit':
|
---|
146 | sys.stdout.write("Running timeit for\n{0!s}\n".format(template))
|
---|
147 |
|
---|
148 | def profile():
|
---|
149 | t = templates[template]
|
---|
150 | t.render(ctx)
|
---|
151 |
|
---|
152 | out = timeit.timeit(profile, number=100_000)
|
---|
153 | sys.stdout.write("100000 loops -> {secs:.{prec}g} secs\n".format(
|
---|
154 | secs=out,
|
---|
155 | prec=4,
|
---|
156 | ))
|
---|
157 |
|
---|
158 | elif runner == 'yappi':
|
---|
159 | import yappi
|
---|
160 | sys.stdout.write("Running yappi for\n{0!s}\n".format(template))
|
---|
161 |
|
---|
162 | yappi.start()
|
---|
163 |
|
---|
164 | def profile_many():
|
---|
165 | t = templates[template]
|
---|
166 | for x in range(100_000):
|
---|
167 | t.render(ctx)
|
---|
168 |
|
---|
169 | profile_many()
|
---|
170 |
|
---|
171 | yappi.stop()
|
---|
172 | yappi.get_func_stats().print_all()
|
---|
173 |
|
---|
174 | else:
|
---|
175 | sys.stderr.write("Expected one of\nprofile, timeit, yappi\nfor execution method\n")
|
---|
176 | sys.exit(1)
|
---|
177 |
|
---|
178 |
|
---|