Giter Club home page Giter Club logo

python-flamegraph's Introduction

A simple statistical profiler which outputs in format suitable for FlameGraph.

INSTALL:

Simply run:

pip install git+https://github.com/evanhempel/python-flamegraph.git

USAGE:

Run your script under the profiler:

python -m flamegraph -o perf.log myscript.py --your-script args here

Or, run the profiler from your script:

flamegraph.start_profile_thread(fd=open("./perf.log", "w"))

Run Brendan Gregg's FlameGraph tool against the output:

flamegraph.pl --title "MyScript CPU" perf.log > perf.svg

Enjoy the output:

Attic create flame graph

Filtering

Sometimes you may want to exclude a method (for example in a server the method that waits for a new request) or you may want to profile only a subset of your code (a particular method and its children which are performance critical).

Filtering can be done by passing a python regular expression to the -f or --filter command line option which will restrict output to only those lines which match. Filtering is done against the entire line so you can filter by function name, thread name, both, or even by more complex filters such as function ABC calls DEF (ABC.*DEF).

Alternatively since the output is stackframes each on a line by itself, this can simply be done with a simple grep filter.:

Exclude:

  grep -v waiting_method perf.log > removed_waiting.log

Include:

  grep function_name perf.log > filtered.log

Then run the flamegraph.pl script against the filtered file.

yCanta webapp full profile of PDF export

Full profile output of yCanta webapp PDF export. Most time is spent in wait state and graph is not very helpful.

yCanta webapp filtered for PDF export format function.

Filtered profile output of yCanta webapp PDF export. Filtering was on the pdf format function so time spent in wait state has been excluded and the graph is now helpful.

python-flamegraph's People

Contributors

evanhempel avatar koreno avatar mlsteele avatar noamelf avatar xoltar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-flamegraph's Issues

jinja2.exceptions.TemplateNotFound: index.html

I have a simple python web program which uses jinja2 and Quart, when I run python server_async.py, everything is fine. But when I run python -m flamegraph -o perf.log server_async.py, it reports errors:

...
File "server_async.py", line 254, in index
template = await render_template('index.html')
...
jinja2.exceptions.TemplateNotFound: index.html

I guess this is because flamegraph does not work with Quart and jinja2, right?

Upload to PyPI

Would you mind terribly uploading python-flamegraph to PyPI? It's a simple process:

(People will tell you to run twine upload instead, because distutils do not verify SSL certificates, but I think that means you need to register the project manually via some web form, instead of having setup.py register take care of everything.)

/usr/bin/python: No module named flamegraph

i cannot start it because No module named flamegraph.

but i have installed: pip install git+https://github.com/evanhempel/python-flamegraph.git

'''pip install git+https://github.com/evanhempel/python-flamegraph.git
Collecting git+https://github.com/evanhempel/python-flamegraph.git
Cloning https://github.com/evanhempel/python-flamegraph.git to /tmp/pip-req-build-0o57y8hm
Running command git clone -q https://github.com/evanhempel/python-flamegraph.git /tmp/pip-req-build-0o57y8hm
/home/yiwei/.local/lib/python3.8/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: 0.23ubuntu1 is an invalid version and will not be supported in a future release
warnings.warn(
/home/yiwei/.local/lib/python3.8/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: 1.13.1-unknown is an invalid version and will not be supported in a future release
warnings.warn(
/home/yiwei/.local/lib/python3.8/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: 0.1.36ubuntu1 is an invalid version and will not be supported in a future release
warnings.warn(
Building wheels for collected packages: flamegraph
Building wheel for flamegraph (setup.py) ... done
Created wheel for flamegraph: filename=flamegraph-0.1-py3-none-any.whl size=5793 sha256=29005f3f6de44d87677934b3929f9ff5f527d75c4e229a204d17d24b7bde124e
Stored in directory: /tmp/pip-ephem-wheel-cache-x0ptsvmg/wheels/ef/34/64/fb50a5c67924972e8361d0641f1f1b56fa9f1ff8daad2550bb
Successfully built flamegraph
Installing collected packages: flamegraph
Successfully installed flamegraph-0.1
'''

what can i do here?

Create PyPi package

python-flamegraph should be available on PyPi, pip install python-flamegraph works.

jinja2.exceptions.TemplateNotFound: index.html

I have a simple python web program which uses jinja2 and Quart, when I run python server_async.py, everything is fine. But when I run python -m flamegraph -o perf.log server_async.py, it reports errors:

...
File "server_async.py", line 254, in index
template = await render_template('index.html')
...
jinja2.exceptions.TemplateNotFound: index.html

I guess this is because flamegraph does not work with Quart and jinja2, right?

Python 3.8 support

I'm getting an error running this. Seems to work fine on Python 3.7, but on 3.8, I get the following.

Traceback (most recent call last):
  File "/Users/user/.pyenv/versions/3.8.2/lib/python3.8/runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/user/.pyenv/versions/3.8.2/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/user/.pyenv/versions/my-venv-3.8.2/lib/python3.8/site-packages/flamegraph/__main__.py", line 4, in <module>
    flamegraph.main()
  File "/Users/user/.pyenv/versions/my-venv-3.8.2/lib/python3.8/site-packages/flamegraph/flamegraph.py", line 146, in main
    start_time = time.clock()
AttributeError: module 'time' has no attribute 'clock'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/Users/user/.pyenv/versions/my-venv-3.8.2/lib/python3.8/site-packages/flamegraph/flamegraph.py", line 99, in stop
    self.join()
  File "/Users/user/.pyenv/versions/3.8.2/lib/python3.8/threading.py", line 1006, in join
    raise RuntimeError("cannot join thread before it is started")
RuntimeError: cannot join thread before it is started

At least for time.clock(), it seems like there are alternatives in 3.8.

Support for profiling imported c modules

Now only the functions in the script that is profiled are added to the log. It would be really great to also know what takes time in functions from other modules imported by the profiled script.

profile Python program run as a module

Hi there!

Thanks for sharing this, I'm really looking forward to putting your tool to good work :-)

I got here looking for a way to profile pip install because it's much slower on some machines than others. I was hoping to profile it using flamegraph pip install ..., but I take it that flamegraph expects its first argument to be a file.

The coverage tool supports this (ref for inspiration). Would you be willing to accept a contribution that adds something similar?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.