Skip to content

Instantly share code, notes, and snippets.

@lemon24
Created July 1, 2018 10:18
Show Gist options
  • Save lemon24/9b8a33054900dd5e91e579c0863349db to your computer and use it in GitHub Desktop.
Save lemon24/9b8a33054900dd5e91e579c0863349db to your computer and use it in GitHub Desktop.
https://github.com/lemon24/reader/issues/77 (generate instead of render)
$ python bench.py time
entries runs get_entries /?show=all ratio
32 8 0.01 0.20 32.7
64 8 0.01 0.30 28.4
128 8 0.02 0.51 25.2
256 8 0.04 0.94 23.1
512 8 0.08 1.78 22.0
1024 4 0.09 1.77 20.1
2048 2 0.11 1.85 17.4
4096 1 0.13 1.84 13.8
$ python bench.py profile
1980424 function calls (1938802 primitive calls) in 1.508 seconds
Ordered by: cumulative time
List reduced from 818 to 40 due to restriction <40>
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.508 1.508 test.py:827(get)
1 0.000 0.000 1.508 1.508 testing.py:166(open)
1 0.000 0.000 1.508 1.508 test.py:762(open)
1 0.000 0.000 1.508 1.508 test.py:712(run_wsgi_app)
1 0.000 0.000 1.505 1.505 test.py:896(run_wsgi_app)
1 0.000 0.000 1.505 1.505 app.py:2305(__call__)
1 0.000 0.000 1.505 1.505 app.py:2262(wsgi_app)
1 0.000 0.000 1.505 1.505 app.py:1801(full_dispatch_request)
1 0.001 0.001 1.503 1.503 app.py:1779(dispatch_request)
1 0.000 0.000 1.502 1.502 app.py:42(entries)
1 0.000 0.000 1.435 1.435 templating.py:122(render_template)
20835/219 0.018 0.000 1.367 0.006 {method 'join' of 'str' objects}
1 0.000 0.000 1.367 1.367 templating.py:113(_render)
1 0.000 0.000 1.367 1.367 asyncsupport.py:74(render)
1 0.000 0.000 1.367 1.367 environment.py:993(render)
20515 0.006 0.000 1.361 0.000 entries.html:5(root)
20515 0.006 0.000 1.317 0.000 layout.html:5(root)
20507 0.066 0.000 1.310 0.000 entries.html:49(block_body)
16416/14368 0.048 0.000 1.070 0.000 runtime.py:234(call)
6156 0.042 0.000 0.849 0.000 helpers.py:216(url_for)
6156 0.042 0.000 0.712 0.000 routing.py:1701(build)
12312/6156 0.022 0.000 0.440 0.000 routing.py:1680(_partial_build)
6156 0.048 0.000 0.408 0.000 routing.py:797(build)
20538 0.166 0.000 0.227 0.000 urls.py:452(url_quote)
6156 0.059 0.000 0.203 0.000 urls.py:852(url_join)
4101 0.005 0.000 0.164 0.000 urls.py:796(url_encode)
8204 0.017 0.000 0.148 0.000 urls.py:382(_url_encode_impl)
4097 0.026 0.000 0.119 0.000 runtime.py:501(__call__)
3 0.000 0.000 0.106 0.035 environment.py:809(get_template)
3 0.000 0.000 0.106 0.035 environment.py:794(_load_template)
3 0.000 0.000 0.106 0.035 loaders.py:99(load)
3 0.000 0.000 0.106 0.035 environment.py:553(compile)
8208 0.010 0.000 0.105 0.000 urls.py:481(url_quote_plus)
34844 0.053 0.000 0.100 0.000 {built-in method markupsafe._speedups.escape}
4097 0.003 0.000 0.087 0.000 asyncsupport.py:107(_invoke)
4097 0.007 0.000 0.084 0.000 runtime.py:577(_invoke)
12318 0.046 0.000 0.077 0.000 urls.py:404(url_parse)
1 0.000 0.000 0.068 0.068 environment.py:859(get_or_select_template)
2049 0.001 0.000 0.066 0.000 reader.py:422(get_entries)
2057 0.013 0.000 0.066 0.000 reader.py:332(_get_entries)
diff --git a/reader/app.py b/reader/app.py
index 28a6bee..1750ef4 100644
--- a/reader/app.py
+++ b/reader/app.py
@@ -62,7 +62,12 @@ def entries():
if feed_url:
entries_data = [e.id for e in entries]
- return render_template('entries.html', entries=entries, feed=feed, entries_data=entries_data)
+ from flask import Response, stream_with_context
+ return Response(
+ stream_with_context(
+ current_app.jinja_env.get_template('entries.html')
+ .generate(entries=entries, feed=feed, entries_data=entries_data)
+ ))
@blueprint.route('/feeds')
$ python bench.py time
entries runs get_entries /?show=all ratio
32 8 0.01 0.21 34.5
64 8 0.01 0.32 29.8
128 8 0.02 0.55 26.6
256 8 0.05 1.02 21.0
512 8 0.09 1.96 21.2
1024 4 0.10 1.96 20.4
2048 2 0.11 1.92 18.0
4096 1 0.14 1.91 13.4
$ python bench.py profile
2122020 function calls (2096900 primitive calls) in 1.590 seconds
Ordered by: cumulative time
List reduced from 817 to 40 due to restriction <40>
ncalls tottime percall cumtime percall filename:lineno(function)
20515 0.013 0.000 1.454 0.000 wsgi.py:869(__next__)
20515 0.019 0.000 1.441 0.000 wrappers.py:81(_iter_encoded)
20516 0.008 0.000 1.412 0.000 helpers.py:137(generator)
20515 0.007 0.000 1.404 0.000 environment.py:1029(generate)
20515 0.006 0.000 1.397 0.000 entries.html:5(root)
20515 0.006 0.000 1.353 0.000 layout.html:5(root)
20507 0.066 0.000 1.345 0.000 entries.html:49(block_body)
16416/14368 0.049 0.000 1.084 0.000 runtime.py:234(call)
6156 0.041 0.000 0.859 0.000 helpers.py:216(url_for)
6156 0.041 0.000 0.725 0.000 routing.py:1701(build)
12312/6156 0.022 0.000 0.446 0.000 routing.py:1680(_partial_build)
6156 0.049 0.000 0.415 0.000 routing.py:797(build)
20538 0.168 0.000 0.230 0.000 urls.py:452(url_quote)
6156 0.061 0.000 0.211 0.000 urls.py:852(url_join)
4101 0.005 0.000 0.167 0.000 urls.py:796(url_encode)
20834 0.012 0.000 0.163 0.000 {method 'join' of 'str' objects}
8204 0.018 0.000 0.151 0.000 urls.py:382(_url_encode_impl)
1 0.000 0.000 0.135 0.135 test.py:827(get)
1 0.000 0.000 0.135 0.135 testing.py:166(open)
1 0.000 0.000 0.135 0.135 test.py:762(open)
1 0.000 0.000 0.135 0.135 test.py:712(run_wsgi_app)
1 0.000 0.000 0.132 0.132 test.py:896(run_wsgi_app)
1 0.000 0.000 0.132 0.132 app.py:2305(__call__)
1 0.000 0.000 0.132 0.132 app.py:2262(wsgi_app)
1 0.000 0.000 0.132 0.132 app.py:1801(full_dispatch_request)
1 0.000 0.000 0.131 0.131 app.py:1779(dispatch_request)
1 0.000 0.000 0.131 0.131 app.py:42(entries)
4097 0.026 0.000 0.121 0.000 runtime.py:501(__call__)
3 0.000 0.000 0.107 0.036 environment.py:809(get_template)
3 0.000 0.000 0.107 0.036 environment.py:794(_load_template)
3 0.000 0.000 0.107 0.036 loaders.py:99(load)
8208 0.010 0.000 0.107 0.000 urls.py:481(url_quote_plus)
3 0.000 0.000 0.106 0.035 environment.py:553(compile)
34844 0.054 0.000 0.103 0.000 {built-in method markupsafe._speedups.escape}
4097 0.003 0.000 0.089 0.000 asyncsupport.py:107(_invoke)
4097 0.008 0.000 0.086 0.000 runtime.py:577(_invoke)
12318 0.048 0.000 0.080 0.000 urls.py:404(url_parse)
129619/125505 0.050 0.000 0.080 0.000 {built-in method builtins.getattr}
8237 0.007 0.000 0.067 0.000 local.py:344(__getattr__)
2049 0.001 0.000 0.062 0.000 reader.py:422(get_entries)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment