Skip to content

bootinge/profiling

 
 

Repository files navigation

Profiling

The profiling package is an interactive Python profiler. It is inspired from Unity 3D profiler. This package provides these features:

  • Profiling statistics keep the frame stack.
  • An interactive TUI profiling statistics viewer.
  • Utilities for remote profiling.
  • Thread or greenlet aware CPU timer.
  • Supports Python 2.7, 3.2, 3.3 and 3.4.

[![Build Status] (https://travis-ci.org/what-studio/profiling.svg?branch=master)] (https://travis-ci.org/what-studio/profiling) [![Coverage Status] (https://coveralls.io/repos/what-studio/profiling/badge.svg?branch=master)] (https://coveralls.io/r/what-studio/profiling)

Installation

This project is still under development, so you should install it via GitHub instead of PyPI:

pip install git+https://github.com/what-studio/profiling.git

Profiling

To profile a single program, simply run profile command:

$ python -m profiling profile your-program.py

Then an interactive viewer will be executed:

If your program uses greenlets, choose greenlet timer:

$ python -m profiling profile --timer=greenlet your-program.py

With --dump option, it saves the profiling result to a file. You can browse the saved result by using the view command:

$ python -m profiling profile --dump=your-program.prf your-program.py
$ python -m profiling view your-program.prf

If your script reads sys.argv, append your arguments after --. It isolates your arguments from the profile command:

$ python -m profiling profile your-program.py -- --your-flag --your-param=42 -hjkl

Live-profiling

If your program has a long life time like a web server, profiling result at the end of program doesn't help you. You will need a continuous profiler. It works via live-profile command:

$ python -m profiling live-profile webserver.py

See a demo:

asciicast

There's a live-profiling server also. The server doesn't profile the program at ordinary times. But when a client connects to the server, it runs profiler and reports to the all connected clients. Start a server with remote-profile command:

$ python -m profiling remote-profile webserver.py --bind 127.0.0.1:8912

Then run a client with view command:

$ python -m profiling view 127.0.0.1:8912

Timeit then Profiling

Do you use timeit to check the performance of your code?

$ python -m timeit -s 'from trueskill import *' 'rate_1vs1(Rating(), Rating())'
1000 loops, best of 3: 722 usec per loop

If you want to profile the checked code, just add profiling before timeit:

$ python -m profiling timeit -s 'from trueskill import *' 'rate_1vs1(Rating(), Rating())'
            ^^^^^^^^^

Profiling from Code

You can also profile your program by profiling.Profiler directly:

from profiling import Profiler
from profiling.viewer import StatisticsViewer

# profile your program.
profiler = Profiler()
profiler.start()
...  # run your program.
profiler.stop()

# view statistics.
viewer = StatisticsViewer()
viewer.set_stats(profiler.stats)
loop = viewer.loop()
loop.run()

Viewer key commands

  • q - Quit.
  • space - Pause/Resume.
  • and - Navigate frames.
  • - Expand the frame.
  • - Fold the frame.
  • > - Go to the hotspot.
  • esc - Defocus.
  • [ and ] - Change sorting column.

Licensing

This project is licensed under the BSD 3-Clause license.

About

An interactive Python profiler.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 98.5%
  • C 1.5%