aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2023-06-27 14:50:20 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2023-06-27 14:50:20 +0200
commit8697555125c57ae64a0c9b78514b4aac4fd523de (patch)
treea699df53a7c35a4425f30bca86982c4341f6de40 /content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md
parent33b2615a5038bc85036081e8b5e0da8584d88097 (diff)
downloadmitjafelicijan.com-8697555125c57ae64a0c9b78514b4aac4fd523de.tar.gz
Massive formatting and added figcaption
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.md47
1 files changed, 24 insertions, 23 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 911e6e0..2e36eaf 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
@@ -5,10 +5,9 @@ date: 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 8I have been profiling my software with KCachegrind for a long time now and I was
9was missing this option when I am developing API's or other web services. I 9missing this option when I am developing API's or other web services. I always
10always knew that this is possible but never really took the time and dive 10knew that this is possible but never really took the time and dive into it.
11into it.
12 11
13Before we begin there are some requirements. We will need to: 12Before we begin there are some requirements. We will need to:
14 13
@@ -17,7 +16,9 @@ Before we begin there are some requirements. We will need to:
17- visualize data with [KCachegrind](http://kcachegrind.sourceforge.net/html/Home.html) or [Profiling Viewer](http://www.profilingviewer.com/). 16- visualize data with [KCachegrind](http://kcachegrind.sourceforge.net/html/Home.html) or [Profiling Viewer](http://www.profilingviewer.com/).
18 17
19 18
20If you are using MacOS you should check out [Profiling Viewer](http://www.profilingviewer.com/) or [MacCallGrind](http://www.maccallgrind.com/). 19If you are using MacOS you should check out [Profiling
20Viewer](http://www.profilingviewer.com/) or
21[MacCallGrind](http://www.maccallgrind.com/).
21 22
22![KCachegrind](/assets/python-profiling/kcachegrind.png) 23![KCachegrind](/assets/python-profiling/kcachegrind.png)
23 24
@@ -28,7 +29,7 @@ We will be dividing this post into two main categories:
28 29
29## Simple web-service 30## Simple web-service
30 31
31Let's use virtualenv so we won't pollute our base system. If you don't have 32Let'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. 33virtualenv installed on your system you can install it with pip command.
33 34
34```bash 35```bash
@@ -73,8 +74,8 @@ $ pip install bottle
73$ deactivate 74$ deactivate
74``` 75```
75 76
76We are now ready to write simple web service. Let's create file app.py and 77We are now ready to write simple web service. Let's create file app.py and paste
77paste code bellow in this newly created file. 78code bellow in this newly created file.
78 79
79```python 80```python
80# -*- coding: utf-8 -*- 81# -*- coding: utf-8 -*-
@@ -126,8 +127,8 @@ if __name__ == '__main__':
126# open browser 'http://0.0.0.0:4000' 127# open browser 'http://0.0.0.0:4000'
127``` 128```
128 129
129When browser hits awesome\_random\_number() function profile is created in 130When browser hits awesome\_random\_number() function profile is created in prof/
130prof/ subfolder. 131subfolder.
131 132
132## Visualize profile 133## Visualize profile
133 134
@@ -139,26 +140,27 @@ $ pyprof2calltree -i awesome_random_number.prof
139# this creates 'awesome_random_number.prof.log' file in the same folder 140# this creates 'awesome_random_number.prof.log' file in the same folder
140``` 141```
141 142
142This file can be opened with visualizing tools listed above. In this case we 143This 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. 144will be using Profilling Viewer under MacOS. You can open image in new tab. As
144As you can see from this example there is hierarchy of execution order of 145you can see from this example there is hierarchy of execution order of your
145your code. 146code.
146 147
147![Profilling Viewer](/assets/python-profiling/profiling-viewer.png) 148![Profilling Viewer](/assets/python-profiling/profiling-viewer.png)
148 149
149> Make sure you convert output of the cProfile output every time you want to 150> 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 151refresh and take a look at your possible optimizations because cProfile updates
151updates .prof file every time browser hits the function. 152.prof file every time browser hits the function.
152 153
153This is just a simple example but when you are developing real-life applications 154This 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 155this can be very illuminating, especially to see which parts of your code are
155bottlenecks and need to be optimized. 156bottlenecks and need to be optimized.
156 157
157## Update 2017-04-22 158## Update 2017-04-22
158 159
159Reddit user [mvt](https://www.reddit.com/user/mvt) also recommended this 160Reddit user [mvt](https://www.reddit.com/user/mvt) also recommended this awesome
160awesome web based profile visualizer [SnakeViz](https://jiffyclub.github.io/snakeviz/) 161web 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) 162that directly takes output from
163[cProfile](https://docs.python.org/2/library/profile.html#module-cProfile)
162module. 164module.
163 165
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> 166<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>
@@ -201,4 +203,3 @@ $ pip install snakeviz --user
201 203
202Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can 204Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can
203use [pipsi](https://github.com/mitsuhiko/pipsi). 205use [pipsi](https://github.com/mitsuhiko/pipsi).
204