From 43b0708769eb61392050045b881f8e6ba39c5b66 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Fri, 26 May 2023 00:40:40 +0200 Subject: Massive update to posts, archetypes Added a archetypes for creating notes and posts so it auto-populates fields. Fixed existing posts so they align with the rule of 80 columns now. --- ...ng-python-web-applications-with-visual-tools.md | 41 ++++++++++++++++------ 1 file changed, 30 insertions(+), 11 deletions(-) (limited to 'content/posts/2017-04-21-profiling-python-web-applications-with-visual-tools.md') 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 @@ --- title: Profiling Python web applications with visual tools url: profiling-python-web-applications-with-visual-tools.html -date: 2017-04-21 +date: 2017-04-21T12:00:00+02:00 draft: false --- -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. +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. Before we begin there are some requirements. We will need to: @@ -25,7 +28,8 @@ We will be dividing this post into two main categories: ## Simple web-service -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. +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. ```bash # let's install virtualenv globally @@ -69,7 +73,8 @@ $ pip install bottle $ deactivate ``` -We are now ready to write simple web service. Let's create file app.py and paste code bellow in this newly created file. +We are now ready to write simple web service. Let's create file app.py and +paste code bellow in this newly created file. ```python # -*- coding: utf-8 -*- @@ -121,7 +126,8 @@ if __name__ == '__main__': # open browser 'http://0.0.0.0:4000' ``` -When browser hits awesome\_random\_number() function profile is created in prof/ subfolder. +When browser hits awesome\_random\_number() function profile is created in +prof/ subfolder. ## Visualize profile @@ -133,17 +139,27 @@ $ pyprof2calltree -i awesome_random_number.prof # this creates 'awesome_random_number.prof.log' file in the same folder ``` -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. +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. ![Profilling Viewer](/assets/python-profiling/profiling-viewer.png) -> 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. +> 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. -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. +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. ## Update 2017-04-22 -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. +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.
Comment from discussion Profiling Python web applications with visual tools.
@@ -160,7 +176,8 @@ $ snakeviz awesome_random_number.prof ![SnakeViz](/assets/python-profiling/snakeviz.png) -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. +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.
Comment from discussion Profiling Python web applications with visual tools.
@@ -182,4 +199,6 @@ $ echo $PATH $ pip install snakeviz --user ``` -Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can use [pipsi](https://github.com/mitsuhiko/pipsi). +Or as suggested by [mvt](https://www.reddit.com/user/mvt) you can +use [pipsi](https://github.com/mitsuhiko/pipsi). + -- cgit v1.2.3