Skip to content

trevor-vincent/simple-python-flamegraph-profiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Python Sampling Profiler with FlameGraph Output

This repository contains a simple sampling-based profiler for Python programs. It periodically collects stack traces to analyze the performance of the code. The output can be directly converted to a FlameGraph using Brendan Greggs Flamegraph perl script. Don't be fooled by the simplicity, it is still very accurate and useful.

Installation

  1. Nothing to install. Python 3 is required.

Usage

Example

from profiler import Sampler

def some_function():
    b = 0    
    for i in range(100000000):
        b += 1

def another_function():
    c = 0
    for i in range(100000000):
        c += 1

def main():
    # Initialize the sampler
    sampler = Sampler()

    # Start the sampler
    sampler.start()

    # Code to profile
    some_function()
    another_function()

    # Output the collected statistics to a file
    sampler.output_stats("example.stacks")
    
    # Stop the sampler
    sampler.stop()

if __name__ == "__main__":
    main()

Output

The profiler will generate a .stacks file containing the profiling results that is directly readable by Brendan Gregg's Flamegraph program.

Converting the stacks file to a FlameGraph

To convert the profiling output to a FlameGraph, follow these steps:

  1. Ensure you have FlameGraph tools installed. If not, clone the repository:

    git clone https://github.com/brendangregg/FlameGraph.git
  2. Use the flamegraph.pl script to generate the FlameGraph from the .stacks file:

    ./FlameGraph/flamegraph.pl example.stacks > example.svg
  3. Open the generated example.svg file in a web browser to visualize the FlameGraph.

License

This project is licensed under the MIT License.

About

Simple sampling python profiler with Flamegraph output

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages