« Back to Index

[Python Flame Graph with pyflame]

View original Gist on GitHub

Tags: #pyflame #flameĀ #graph #python

1. Python Flame Graph with pyflame-Dockerfile

FROM python:3.6.3

WORKDIR /pyflame

RUN apt-get update -y
RUN apt-get install -y git autoconf automake autotools-dev g++ pkg-config python-dev python3-dev libtool make

RUN git clone https://github.com/uber/pyflame.git && \
    cd pyflame && ./autogen.sh && ./configure && make

WORKDIR /flamegraph

RUN git clone https://github.com/brendangregg/FlameGraph

COPY app.py /app/app.py

WORKDIR /app

CMD /pyflame/pyflame/src/pyflame -o prof.txt -t python app.py &&\
    /flamegraph/FlameGraph/flamegraph.pl ./prof.txt

# docker build -t pyflame .
# docker run --privileged pyflame > output.svg && tail -n+2 output.svg > output_stripped.svg

2. app.py

def foo():
    return 1 + 1


def get_number():
    foo()
    for i in range(10000000):
        yield i


def expensive_function():
    for n in get_number():
        r = n ^ n ^ n
    return f"some result! {r}"


result = expensive_function()
print(result)