diff --git a/README.md b/README.md deleted file mode 100644 index 84b89a0..0000000 --- a/README.md +++ /dev/null @@ -1,106 +0,0 @@ -[![Build Status](https://travis-ci.com/publons/flame-analyzer.svg?token=WSHb2ssbuqzAyoqCvdCs&branch=master)](https://travis-ci.com/publons/flame-analyzer) - -# Flame analyzer - -This package is an often used part of our debug environment at Publons. -It helps benchmark and explain inefficiencies in pieces of code as well as our -dependencies on different service response times. - - -There are four kinds of Context managers we support with this package - -- FileFlame -- InlineFlame -- DjangoFileFlame -- DjangoInlineFlame - -They all serve the same use case outputting a Flame graph to your -machine for you to dive into and debug your code. `FileFlame`/`DjangoFileFlame` -save the graph to an SVG for you to share, while `InlineFlame`/`DjangoInlineFlame` -will render it in your IPython browser. - - -### Examples - - -Saving a Flame graph to an SVG can be done with the following benchmarking code - -```Python - from flame_analyzer import FileFlame - - - with FileFlame('./file_flame_test.svg'): - # Some expensive piece of code. - [len(u.email) for u in User.objects.all()] -``` - -Or directly to the IPython notebook - -```Python - from flame_analyzer import InlineFlame - - - with InlineFlame(): - # Some expensive piece of code. - [len(u.email) for u in User.objects.all()] -``` - - -You can also optionally configure the width by adding the width kwarg - -```Python - with FileFlame( - './file_flame_test.svg', flame_width=1200, - options={'title': 'This is my test title'} - ): - # some expensive piece of code -``` - -### Extensions - -By default both IPython and Django are optional imports meaning you can install -this library and use it in the terminal to debug your app code without them -installed. Support can be added for other Database frameworks or if your wanting -to hook into the context enter/exit methods by creating your own hooks and adding -to the output flame type your wanting for example: - -```Python - -from flame_analyzer import InlineFlame - -class CustomHook: - """ - Append the time taken to execute to the flame graphs title. - """ - def before(self): - self.called_before = '< Called before code execution >' - - def after(self): - self.called_after = '< Called after code execution >' - - def modify_flame_options(self, flame_options): - title = flame_options.get('title', '') - flame_options['title'] = self.called_before + ' --- ' + self.called_after - return flame_options - - -class CustomInlineFlame(InlineFlame): - hook_classes = (CustomHook,) - - - -with CustomInlineFlame(flame_width=500): - total_email_length = 0 - for u in User.objects.all(): - total_email_length += len(u.email) - print(total_email_length) -``` - -Outputs the IPython viewed Graph: - -![image](https://user-images.githubusercontent.com/6813352/68050764-c1107800-fd4a-11e9-94a2-8ab0bc564617.png) - - -Credits to the following projects: - - https://github.com/brendangregg/FlameGraph - - https://github.com/23andMe/djdt-flamegraph diff --git a/README.rst b/README.rst index 6d1c720..3c32579 100644 --- a/README.rst +++ b/README.rst @@ -2,6 +2,9 @@ Flame analyzer ================= +.. image:: https://travis-ci.com/publons/flame-analyzer.svg?branch=master + :target: https://travis-ci.com/publons/flame-analyzer + This package is an often used part of our debug environment at Publons. It helps benchmark and explain inefficiencies in pieces of code as well as our dependencies on different service response times. @@ -23,7 +26,8 @@ Examples -------- Saving a Flame graph to an SVG can be done with the following benchmarking -code:: + +.. code-block:: python from flame_analyzer import FileFlame @@ -32,7 +36,9 @@ code:: # Some expensive piece of code. [len(u.email) for u in User.objects.all()] -Or directly to the IPython notebook:: +Or directly to the IPython notebook + +.. code-block:: python from flame_analyzer import InlineFlame @@ -41,9 +47,9 @@ Or directly to the IPython notebook:: # Some expensive piece of code. [len(u.email) for u in User.objects.all()] +You can also optionally configure the width by adding the width kwarg - -You can also optionally configure the width by adding the width kwarg:: +.. code-block:: python with FileFlame( './file_flame_test.svg', flame_width=1200, @@ -60,7 +66,9 @@ By default both IPython and Django are optional imports meaning you can install this library and use it in the terminal to debug your app code without them installed. Support can be added for other Database frameworks or if your wanting to hook into the context enter/exit methods by creating your own hooks -and adding to the output flame type your wanting for example::: +and adding to the output flame type your wanting for example + +.. code-block:: python from flame_analyzer import InlineFlame @@ -83,8 +91,6 @@ and adding to the output flame type your wanting for example::: class CustomInlineFlame(InlineFlame): hook_classes = (CustomHook,) - - with CustomInlineFlame(flame_width=500): total_email_length = 0 for u in User.objects.all(): diff --git a/flame_analyzer/hooks.py b/flame_analyzer/hooks.py index f763a91..f5ef61d 100644 --- a/flame_analyzer/hooks.py +++ b/flame_analyzer/hooks.py @@ -1,7 +1,5 @@ from datetime import datetime -from humanize import naturaldelta - """ Hooks allow the flamegraph functionality to be extended in a framework specific way. By default TimeTakenHook is set but depending on your stack you can further @@ -18,11 +16,11 @@ def before(self): self.started = datetime.now() def after(self): - self.time_taken = naturaldelta(datetime.now() - self.started) + self.time_taken = str(datetime.now() - self.started) def modify_flame_options(self, flame_options): title = flame_options.get('title', '') - title += 'Excecuted context in {}. '.format(self.time_taken) + title += 'Executed context in {}. '.format(self.time_taken) flame_options['title'] = title return flame_options diff --git a/setup.py b/setup.py index 64202ad..39fde39 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name='flame-analyzer', - version='0.1.4', + version='0.1.5', packages=find_packages( include=['flame_analyzer'], exclude=('tests') ), @@ -20,9 +20,6 @@ 'A small Django and IPython compatible application for benchmarking ' 'database and IO heavy work.' ), - install_requires=[ - 'humanize', - ], url='https://github.com/publons/flame-analyzer', author='Matthew Betts, Aidan Houlihan', author_email='aidan@publons.com', diff --git a/tests/test_hooks.py b/tests/test_hooks.py index 970cc42..d191de6 100644 --- a/tests/test_hooks.py +++ b/tests/test_hooks.py @@ -16,7 +16,7 @@ def test_time_is_tracked(self): modified_options = self.hook.modify_flame_options({}) self.assertEqual( - 'Excecuted context in a second. ', + 'Executed context in {}. '.format(self.hook.time_taken), modified_options['title'], )