Python profiling - Chrom Tracing

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
mariwan
Posts: 469
Joined: Wed Jan 06, 2021 2:00 pm

Python profiling - Chrom Tracing

Post by mariwan »

You might know that I talked about tools for profiling CPP.
I made a python version for the tool I mentioned in another post.
Feel free to use it , make PR, distribute it ..

It is not perfect yet but it works ..
main.py is just an example how you use it.

Code: Select all

https://github.com/MariwanJ/profile-python
Use

Code: Select all

chrome://tracing/
to upload the created json file.

Original library is from and it is for CPP

https://www.youtube.com/watch?v=xlAH4dbMVnU
User avatar
mariwan
Posts: 469
Joined: Wed Jan 06, 2021 2:00 pm

Re: Python profiling - Chrom Tracing

Post by mariwan »

I am not expert in python. Converting the code needed to think about how you make both static method and static variable.
I couldn't make it working.. I tried to make class-variable object and use it .. it didn't work.
Please, if you are expert, help in fixing that issue.
At the moment, You need to create a global variable in the python file/object you need to profile. which is not optimal.
I updated the tool so it count millisecond .. not nano seconds.
nic
Posts: 136
Joined: Thu Apr 18, 2019 1:14 pm
Location: France

Re: Python profiling - Chrom Tracing

Post by nic »

just a naive question: why don't you use existing python profilers? *eg* https://docs.python.org/3/library/profile.html.

Or if you like visuals, you have pyinstrument (https://github.com/joerick/pyinstrument)
User avatar
mariwan
Posts: 469
Joined: Wed Jan 06, 2021 2:00 pm

Re: Python profiling - Chrom Tracing

Post by mariwan »

nic wrote: Thu Nov 25, 2021 1:07 pm just a naive question: why don't you use existing python profilers? *eg* https://docs.python.org/3/library/profile.html.

Or if you like visuals, you have pyinstrument (https://github.com/joerick/pyinstrument)
As I said before, I am not expert. I needed a tool .. I didn't search for python profiling deep. I have a cpp background and knew about that library. And said to me why not make it python :)
Everyday you learn something. Thank you .
But, perhaps it is not the same taste.
And Don't know how it works. I will look at it.
Thanks again.
User avatar
mariwan
Posts: 469
Joined: Wed Jan 06, 2021 2:00 pm

Re: Python profiling - Chrom Tracing

Post by mariwan »

mariwan wrote: Thu Nov 25, 2021 3:09 pm
nic wrote: Thu Nov 25, 2021 1:07 pm just a naive question: why don't you use existing python profilers? *eg* https://docs.python.org/3/library/profile.html.

Or if you like visuals, you have pyinstrument (https://github.com/joerick/pyinstrument)
As I said before, I am not expert. I needed a tool .. I didn't search for python profiling deep. I have a cpp background and knew about that library. And asked mysel why not make it python :)
Everyday you learn something. Thank you .
But, perhaps it is not the same taste.
And Don't know how it works. I will look at it.
Thanks again.
User avatar
mariwan
Posts: 469
Joined: Wed Jan 06, 2021 2:00 pm

Re: Python profiling - Chrom Tracing

Post by mariwan »

mariwan wrote: Thu Nov 25, 2021 3:10 pm
mariwan wrote: Thu Nov 25, 2021 3:09 pm
nic wrote: Thu Nov 25, 2021 1:07 pm just a naive question: why don't you use existing python profilers? *eg* https://docs.python.org/3/library/profile.html.

Or if you like visuals, you have pyinstrument (https://github.com/joerick/pyinstrument)
As I said before, I am not expert. I needed a tool .. I didn't search for python profiling deep. I have a cpp background and knew about that library. And asked myself why not make it python :)
Everyday you learn something. Thank you .
But, perhaps it is not the same taste.
And Don't know how it works. I will look at it.
Thanks again.
User avatar
mariwan
Posts: 469
Joined: Wed Jan 06, 2021 2:00 pm

Re: Python profiling - Chrom Tracing

Post by mariwan »

Ok . please would you explain this for me? I don't understand how it capture the time .
Attachments
prifiling.7z
(28.75 KiB) Downloaded 26 times
nic
Posts: 136
Joined: Thu Apr 18, 2019 1:14 pm
Location: France

Re: Python profiling - Chrom Tracing

Post by nic »

You whole profiled code took about 10sec. to run, mainly spent within recreateObject. This recreateObject spends almost all its 6.8 sec in calling tuple.makeFilledFace() method.
pyinst1.png
pyinst1.png (77.06 KiB) Viewed 1847 times
Consider this code:

Code: Select all

# test.py
from time import sleep

def do_something():
     list(range(1000000))

def wait():
    sleep(0.1)

for i in range(11):
    do_something()
    for i in range(5):
        wait()
running

Code: Select all

pyinstrument --show-all -r text test.py
, you will get this terminal output which exposes sleep() to be the guilty guy.

pyinst2.png
pyinst2.png (16.41 KiB) Viewed 1847 times
I personally prefer built-in `cprofile`, even if its output are less "sexy"
User avatar
mariwan
Posts: 469
Joined: Wed Jan 06, 2021 2:00 pm

Re: Python profiling - Chrom Tracing

Post by mariwan »

Thank you very much.
That is the evidence of my concern.
FreeCAD/OCCT is slow.
There is no way to make it better .. and that is a killer for my project.
Post Reply