Fall in love with the python series-python performance (3): KCacheGrind performance analysis visualization

KCacheGrind is a performance visualization tool, you need to read a fixed format file to get the visualization graphics, not unique to python

Previously we used cProfile for performance analysis, and then we used cProfile to analyze and save it as a file. The file cannot be directly
read by KCacheGrind . You need to use the tool pyprof2calltree to convert. The resulting file is available for KCacheGrind. Can get visual results.
Therefore, two tools need to be prepared next:
(1) KCacheGrind

This can be downloaded directly: https://sourceforge.net/projects/qcachegrindwin/

Because there are many versions of KCacheGrind, in order to facilitate the viewing of the results (because there is no desktop on my linux), I use the win version. After downloading, it can be used after decompression, without installation.

(2)pyprof2calltree

Installation is simple

root@root:/opt/pypy3.7-v7.3.2-linux64# pip3 install pyprof2calltree
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pyprof2calltree
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ca/2a/e9a76261183b4b5e059a6625d7aae0bcb0a77622bc767d4497148ce2e218/pyprof2calltree-1.4.5.tar.gz
Building wheels for collected packages: pyprof2calltree
  Running setup.py bdist_wheel for pyprof2calltree ... done
  Stored in directory: /root/.cache/pip/wheels/1f/d5/22/e302f7ad85b7bef4f591f7e9b75c681ceea602400bacd9fcdc
Successfully built pyprof2calltree
Installing collected packages: pyprof2calltree
Successfully installed pyprof2calltree-1.4.5

Then proceed to the entire process:

(1) Obtain the .prof file

import cProfile
import pstats
import sys

def run(): 
    a = [1]*100 
    b = [x**3 for x in a ] 
    c = [x for x in b] 
    d = c*2 

profiler = cProfile.Profile()
profiler.enable()##start                     
run()
profiler.create_stats()
stats = pstats.Stats(profiler)
stats.strip_dirs().sort_stats('cumulative').dump_stats('run.prof')#保存为run.prof

(2) Convert to a file readable by KCacheGrind

After the above operation is successful, a file named run.prof is obtained

Next need to be converted:

pyprof2calltree -i run.prof

Parameter explanation:

root@root:/opt/pypy3.7-v7.3.2-linux64# pyprof2calltree -h
usage: pyprof2calltree [-h] [-o output_file_path] [-i input_file_path] [-k]
                       [-r ...]

optional arguments:
  -h, --help            show this help message and exit
  -o output_file_path, --outfile output_file_path
                        Save calltree stats to <outfile>
  -i input_file_path, --infile input_file_path
                        Read Python stats from <infile>
  -k, --kcachegrind     Run the kcachegrind tool on the converted data
  -r ..., --run-script ...
                        Name of the Python script to run to collect profiling
                        data

The above command does not use -o, that is, if no name is specified, a run.prof.log file will be generated

This can be specified using:

-o 名称

And if KCacheGrind is installed on this machine, you can use the -k parameter, which will automatically open KCacheGrind

But I copied the run.prof.log file to win, and then read it:

Finally, attach the KCacheGrind instruction book: https://docs.kde.org/trunk5/en/kdesdk/kcachegrind/kcachegrind.pdf

Guess you like

Origin blog.csdn.net/zhou_438/article/details/109185386