diff options
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.md | 41 |
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 | --- |
| 2 | title: Profiling Python web applications with visual tools | 2 | title: Profiling Python web applications with visual tools |
| 3 | url: profiling-python-web-applications-with-visual-tools.html | 3 | url: profiling-python-web-applications-with-visual-tools.html |
| 4 | date: 2017-04-21 | 4 | date: 2017-04-21T12:00:00+02:00 |
| 5 | draft: false | 5 | draft: false |
| 6 | --- | 6 | --- |
| 7 | 7 | ||
| 8 | I 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. | 8 | I have been profiling my software with KCachegrind for a long time now and I |
| 9 | was missing this option when I am developing API's or other web services. I | ||
| 10 | always knew that this is possible but never really took the time and dive | ||
| 11 | into it. | ||
| 9 | 12 | ||
| 10 | Before we begin there are some requirements. We will need to: | 13 | Before 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 | ||
| 28 | Let'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. | 31 | Let's use virtualenv so we won't pollute our base system. If you don't have |
| 32 | virtualenv 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 | ||
| 72 | We are now ready to write simple web service. Let's create file app.py and paste code bellow in this newly created file. | 76 | We are now ready to write simple web service. Let's create file app.py and |
| 77 | paste 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 | ||
| 124 | When browser hits awesome\_random\_number() function profile is created in prof/ subfolder. | 129 | When browser hits awesome\_random\_number() function profile is created in |
| 130 | prof/ 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 | ||
| 136 | This 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. | 142 | This file can be opened with visualizing tools listed above. In this case we |
| 143 | will be using Profilling Viewer under MacOS. You can open image in new tab. | ||
| 144 | As you can see from this example there is hierarchy of execution order of | ||
| 145 | your code. | ||
| 137 | 146 | ||
| 138 |  | 147 |  |
| 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 |
| 150 | refresh and take a look at your possible optimizations because cProfile | ||
| 151 | updates .prof file every time browser hits the function. | ||
| 141 | 152 | ||
| 142 | This 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. | 153 | This is just a simple example but when you are developing real-life applications |
| 154 | this can be very illuminating, especially to see which parts of your code are | ||
| 155 | bottlenecks and need to be optimized. | ||
| 143 | 156 | ||
| 144 | ## Update 2017-04-22 | 157 | ## Update 2017-04-22 |
| 145 | 158 | ||
| 146 | Reddit 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. | 159 | Reddit user [mvt](https://www.reddit.com/user/mvt) also recommended this |
| 160 | awesome web based profile visualizer [SnakeViz](https://jiffyclub.github.io/snakeviz/) | ||
| 161 | that directly takes output from [cProfile](https://docs.python.org/2/library/profile.html#module-cProfile) | ||
| 162 | module. | ||
| 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 |  | 177 |  |
| 162 | 178 | ||
| 163 | Reddit user [ccharles](https://www.reddit.com/user/ccharles) suggested a better way for installing pip software by targeting user level instead of using sudo. | 179 | Reddit user [ccharles](https://www.reddit.com/user/ccharles) suggested a better |
| 180 | way 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 | ||
| 185 | Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can use [pipsi](https://github.com/mitsuhiko/pipsi). | 202 | Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can |
| 203 | use [pipsi](https://github.com/mitsuhiko/pipsi). | ||
| 204 | |||
