jiffyclub / snakeviz

An in-browser Python profile viewer
https://jiffyclub.github.io/snakeviz/
Other
2.36k stars 139 forks source link

Error "not a valid profile" on python 3.8.9 #181

Closed npit closed 2 years ago

npit commented 2 years ago

Reproduction steps and details below.

Python code:

from cProfile import Profile
from contextlib import contextmanager
from pstats import Stats

@contextmanager
def profiling_wrapper():
    prof = Profile()
    prof.enable()
    yield
    # write profiling results
    prof.disable()
    with open("profiling_info.txt", 'w') as fp:
        stats = Stats(prof, stream=fp)
        stats.strip_dirs().sort_stats('time').print_stats()

with profiling_wrapper():
    print("Yo!")
    import time
    time.sleep(2)

Bash script:

venv/bin/python --version
venv/bin/python -m snakeviz -v

venv/bin/python testprof.py
venv/bin/python -m snakeviz profiling_info.txt

Generated Profile:

         6 function calls in 2.002 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    2.002    2.002    2.002    2.002 {built-in method time.sleep}
        1    0.000    0.000    0.000    0.000 contextlib.py:117(__exit__)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.print}
        1    0.000    0.000    0.000    0.000 testprof.py:5(profiling_wrapper)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.next}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Script output Output:

Python 3.8.9
snakeviz 2.1.1
Yo!
snakeviz: error: The file /home/nik/mnt2/sog/profiling_info.txt is not a valid profile. Generate profiles using: 

        python -m cProfile -o my_program.prof my_program.py

Note that snakeviz must be run under the same version of Python as was used to create the profile.

usage: snakeviz [-h] [-v] [-H ADDR] [-p PORT] [-b BROWSER_PATH] [-s] filename

Start SnakeViz to view a Python profile.

positional arguments:
  filename              Python profile to view

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -H ADDR, --hostname ADDR
                        hostname to bind to (default: 127.0.0.1)
  -p PORT, --port PORT  port to bind to; if this port is already in use a free port will be selected automatically (default: 8080)
  -b BROWSER_PATH, --browser BROWSER_PATH
                        name of webbrowser to launch as described in the documentation of Python's webbrowser module: https://docs.python.org/3/library/webbrowser.html
  -s, --server          start SnakeViz in server-only mode--no attempt will be made to open a browser
jiffyclub commented 2 years ago

The code you have there is not generating the type of profile file that snakeviz can work with, what you're doing there is creating a text file with some output from the Stats instance, what you need is the binary profile data the Profile class can create. I think Profile.dump_stats will do it but I'm not 100% sure.

BTW, you can natively use a Profile instance as a context manager.