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