aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md')
-rw-r--r--content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md41
1 files changed, 30 insertions, 11 deletions
diff --git a/content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md b/content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
index 466d838..911e6e0 100644
--- a/content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
+++ b/content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
@@ -1,11 +1,14 @@
1--- 1---
2title: Profiling Python web applications with visual tools 2title: Profiling Python web applications with visual tools
3url: profiling-python-web-applications-with-visual-tools.html 3url: profiling-python-web-applications-with-visual-tools.html
4date: 2017-04-21 4date: 2017-04-21T12:00:00+02:00
5draft: false 5draft: false
6--- 6---
7 7
8I have been profiling my software with KCachegrind for a long time now and I was missing this option when I am developing API's or other web services. I always knew that this is possible but never really took the time and dive into it. 8I have been profiling my software with KCachegrind for a long time now and I
9was missing this option when I am developing API's or other web services. I
10always knew that this is possible but never really took the time and dive
11into it.
9 12
10Before we begin there are some requirements. We will need to: 13Before we begin there are some requirements. We will need to:
11 14
@@ -25,7 +28,8 @@ We will be dividing this post into two main categories:
25 28
26## Simple web-service 29## Simple web-service
27 30
28Let's use virtualenv so we won't pollute our base system. If you don't have virtualenv installed on your system you can install it with pip command. 31Let's use virtualenv so we won't pollute our base system. If you don't have
32virtualenv installed on your system you can install it with pip command.
29 33
30```bash 34```bash
31# let's install virtualenv globally 35# let's install virtualenv globally
@@ -69,7 +73,8 @@ $ pip install bottle
69$ deactivate 73$ deactivate
70``` 74```
71 75
72We are now ready to write simple web service. Let's create file app.py and paste code bellow in this newly created file. 76We are now ready to write simple web service. Let's create file app.py and
77paste code bellow in this newly created file.
73 78
74```python 79```python
75# -*- coding: utf-8 -*- 80# -*- coding: utf-8 -*-
@@ -121,7 +126,8 @@ if __name__ == '__main__':
121# open browser 'http://0.0.0.0:4000' 126# open browser 'http://0.0.0.0:4000'
122``` 127```
123 128
124When browser hits awesome\_random\_number() function profile is created in prof/ subfolder. 129When browser hits awesome\_random\_number() function profile is created in
130prof/ subfolder.
125 131
126## Visualize profile 132## Visualize profile
127 133
@@ -133,17 +139,27 @@ $ pyprof2calltree -i awesome_random_number.prof
133# this creates 'awesome_random_number.prof.log' file in the same folder 139# this creates 'awesome_random_number.prof.log' file in the same folder
134``` 140```
135 141
136This file can be opened with visualizing tools listed above. In this case we will be using Profilling Viewer under MacOS. You can open image in new tab. As you can see from this example there is hierarchy of execution order of your code. 142This file can be opened with visualizing tools listed above. In this case we
143will be using Profilling Viewer under MacOS. You can open image in new tab.
144As you can see from this example there is hierarchy of execution order of
145your code.
137 146
138![Profilling Viewer](/assets/python-profiling/profiling-viewer.png) 147![Profilling Viewer](/assets/python-profiling/profiling-viewer.png)
139 148
140> Make sure you convert output of the cProfile output every time you want to refresh and take a look at your possible optimizations because cProfile updates .prof file every time browser hits the function. 149> Make sure you convert output of the cProfile output every time you want to
150refresh and take a look at your possible optimizations because cProfile
151updates .prof file every time browser hits the function.
141 152
142This is just a simple example but when you are developing real-life applications this can be very illuminating, especially to see which parts of your code are bottlenecks and need to be optimized. 153This is just a simple example but when you are developing real-life applications
154this can be very illuminating, especially to see which parts of your code are
155bottlenecks and need to be optimized.
143 156
144## Update 2017-04-22 157## Update 2017-04-22
145 158
146Reddit user [mvt](https://www.reddit.com/user/mvt) also recommended this awesome web based profile visualizer [SnakeViz](https://jiffyclub.github.io/snakeviz/) that directly takes output from [cProfile](https://docs.python.org/2/library/profile.html#module-cProfile) module. 159Reddit user [mvt](https://www.reddit.com/user/mvt) also recommended this
160awesome web based profile visualizer [SnakeViz](https://jiffyclub.github.io/snakeviz/)
161that directly takes output from [cProfile](https://docs.python.org/2/library/profile.html#module-cProfile)
162module.
147 163
148<div class="reddit-embed" data-embed-media="www.redditmedia.com" data-embed-parent="false" data-embed-live="false" data-embed-uuid="583880c1-002e-41ed-a373-020a0ef2cff9" data-embed-created="2017-04-22T19:46:54.810Z"><a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/dgljhsb/">Comment</a> from discussion <a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/">Profiling Python web applications with visual tools</a>.</div><script async src="https://www.redditstatic.com/comment-embed.js"></script> 164<div class="reddit-embed" data-embed-media="www.redditmedia.com" data-embed-parent="false" data-embed-live="false" data-embed-uuid="583880c1-002e-41ed-a373-020a0ef2cff9" data-embed-created="2017-04-22T19:46:54.810Z"><a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/dgljhsb/">Comment</a> from discussion <a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/">Profiling Python web applications with visual tools</a>.</div><script async src="https://www.redditstatic.com/comment-embed.js"></script>
149 165
@@ -160,7 +176,8 @@ $ snakeviz awesome_random_number.prof
160 176
161![SnakeViz](/assets/python-profiling/snakeviz.png) 177![SnakeViz](/assets/python-profiling/snakeviz.png)
162 178
163Reddit user [ccharles](https://www.reddit.com/user/ccharles) suggested a better way for installing pip software by targeting user level instead of using sudo. 179Reddit user [ccharles](https://www.reddit.com/user/ccharles) suggested a better
180way for installing pip software by targeting user level instead of using sudo.
164 181
165<div class="reddit-embed" data-embed-media="www.redditmedia.com" data-embed-parent="false" data-embed-live="false" data-embed-uuid="f4f0459e-684d-441e-bebe-eb49b2f0a31d" data-embed-created="2017-04-22T19:46:10.874Z"><a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/dglpzkx/">Comment</a> from discussion <a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/">Profiling Python web applications with visual tools</a>.</div><script async src="https://www.redditstatic.com/comment-embed.js"></script> 182<div class="reddit-embed" data-embed-media="www.redditmedia.com" data-embed-parent="false" data-embed-live="false" data-embed-uuid="f4f0459e-684d-441e-bebe-eb49b2f0a31d" data-embed-created="2017-04-22T19:46:10.874Z"><a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/dglpzkx/">Comment</a> from discussion <a href="https://www.reddit.com/r/Python/comments/66v373/profiling_python_web_applications_with_visual/">Profiling Python web applications with visual tools</a>.</div><script async src="https://www.redditstatic.com/comment-embed.js"></script>
166 183
@@ -182,4 +199,6 @@ $ echo $PATH
182$ pip install snakeviz --user 199$ pip install snakeviz --user
183``` 200```
184 201
185Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can use [pipsi](https://github.com/mitsuhiko/pipsi). 202Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can
203use [pipsi](https://github.com/mitsuhiko/pipsi).
204