SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Python
performance
profiling
What is CPU profiling and
lessoned learned using it
Part 1: CPU profiling
What is CPU
profiling?
A profile is a set of statistics that
describes how often and for how long
various parts of the program executed.
See output sample.
How do
CPU
profilers
work?
● Most profilers run inside your Python process.
● If you’re inside a Python program you generally have
pretty easy access to its stack.
● There are two types of profilers that differ upon their
triggers:
○ Tracing profilers - triggered on function/line called
○ Sampling profilers - triggered on a time interval
How do
tracing
profilers
work?
● Python let you specify a callback that gets run when
various interpreter events (like “calling a function” or
“executing a line of code”) happen.
● When the callback gets called, it records the stack for
later analysis.
● You can set up that callback with:
○ PyEval_SetProfile - triggered only when a function is called
○ PyEval_SetTrace - triggered when a function is called or a
line of code is executed
● Cprofile uses PyEval_SetProfile
● line_profile uses PyEval_SetTrace
Disadvantag
e of tracing
profilers
● The main disadvantage of tracing profilers implemented
in this way is that they introduce a fixed amount of
latency for every function call / line of code executed.
● See example
● The documentation for cProfile says:
○ “The interpreted nature of Python tends to add so much
overhead to execution, that deterministic profiling tends to
only add small processing overhead in typical applications”
● Makes sense since standard programs does not have
so many function calls.
How do
sampling
profilers
work?
Well – let’s say you want to get a snapshot of a program’s
stack 50 times a second. A way to do that is:
● Ask the Linux kernel to send you a signal every 20
milliseconds (using the setitimer system call)
● Register a signal handler to record the stack every time
you get a signal.
● When you’re done profiling print the output!
Comparison ● A sample profile in 61 LOC
● A demo using it.
● A comparison of sampling vs. tracing
● Real Projects:
○ stacksampler
○ pyflame
○ python-flamegraph
Visualising
profiling output
Flame graphs adds a nice visual touch
to understand the profiler output. See
python-flamegraph and the FlameGraph
tool.
Part 2: lesson learned
A new core
infrastructure
feature
Performance
consideratio
ns in design
● Consider performance at design time, not all
business/API requirement can be answered and there
might need to be some compromises need to be made.
● Do your best to understand the performance impact, but
no more (beware of analysis paralysis). Invest in a
testable, monitored environment instead.
Performance
monitoring in
vitro
After design and implementation we can check performance
using:
● CI and test suite:
○ Expose speed degradation
○ Have the base to run profiling on.
○ Notice! test fakers might have different data then the
actual data in production
● Saging environment:
○ Test scenarios on the same data as in production.
○ There might be some actions that will not know to do and
have significant performance issue in production
Performance
monitoring in
vivo
● Application Performance Monitoring (APM) such as
New Relic or Datadog allows to:
○ Set alerts on certain metrics
○ Analyze real transactions
○ Add custom instrumentation
● Users are the best QA 😱
Actual software is
composed of code
& data
Questions?
Reference ● Juila Evans blog post -
https://jvns.ca/blog/2017/12/17/how-do-ruby---python-
profilers-work-/
● Nylas blog post -
https://www.nylas.com/blog/performance/

Weitere ähnliche Inhalte

Kürzlich hochgeladen

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Kürzlich hochgeladen (20)

%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 

Empfohlen

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Empfohlen (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Python Performance Profiling

  • 1. Python performance profiling What is CPU profiling and lessoned learned using it
  • 2. Part 1: CPU profiling
  • 3. What is CPU profiling? A profile is a set of statistics that describes how often and for how long various parts of the program executed. See output sample.
  • 4. How do CPU profilers work? ● Most profilers run inside your Python process. ● If you’re inside a Python program you generally have pretty easy access to its stack. ● There are two types of profilers that differ upon their triggers: ○ Tracing profilers - triggered on function/line called ○ Sampling profilers - triggered on a time interval
  • 5. How do tracing profilers work? ● Python let you specify a callback that gets run when various interpreter events (like “calling a function” or “executing a line of code”) happen. ● When the callback gets called, it records the stack for later analysis. ● You can set up that callback with: ○ PyEval_SetProfile - triggered only when a function is called ○ PyEval_SetTrace - triggered when a function is called or a line of code is executed ● Cprofile uses PyEval_SetProfile ● line_profile uses PyEval_SetTrace
  • 6. Disadvantag e of tracing profilers ● The main disadvantage of tracing profilers implemented in this way is that they introduce a fixed amount of latency for every function call / line of code executed. ● See example ● The documentation for cProfile says: ○ “The interpreted nature of Python tends to add so much overhead to execution, that deterministic profiling tends to only add small processing overhead in typical applications” ● Makes sense since standard programs does not have so many function calls.
  • 7. How do sampling profilers work? Well – let’s say you want to get a snapshot of a program’s stack 50 times a second. A way to do that is: ● Ask the Linux kernel to send you a signal every 20 milliseconds (using the setitimer system call) ● Register a signal handler to record the stack every time you get a signal. ● When you’re done profiling print the output!
  • 8. Comparison ● A sample profile in 61 LOC ● A demo using it. ● A comparison of sampling vs. tracing ● Real Projects: ○ stacksampler ○ pyflame ○ python-flamegraph
  • 9. Visualising profiling output Flame graphs adds a nice visual touch to understand the profiler output. See python-flamegraph and the FlameGraph tool.
  • 10. Part 2: lesson learned
  • 12. Performance consideratio ns in design ● Consider performance at design time, not all business/API requirement can be answered and there might need to be some compromises need to be made. ● Do your best to understand the performance impact, but no more (beware of analysis paralysis). Invest in a testable, monitored environment instead.
  • 13. Performance monitoring in vitro After design and implementation we can check performance using: ● CI and test suite: ○ Expose speed degradation ○ Have the base to run profiling on. ○ Notice! test fakers might have different data then the actual data in production ● Saging environment: ○ Test scenarios on the same data as in production. ○ There might be some actions that will not know to do and have significant performance issue in production
  • 14. Performance monitoring in vivo ● Application Performance Monitoring (APM) such as New Relic or Datadog allows to: ○ Set alerts on certain metrics ○ Analyze real transactions ○ Add custom instrumentation ● Users are the best QA 😱
  • 15. Actual software is composed of code & data
  • 17. Reference ● Juila Evans blog post - https://jvns.ca/blog/2017/12/17/how-do-ruby---python- profilers-work-/ ● Nylas blog post - https://www.nylas.com/blog/performance/

Hinweis der Redaktion

  1. Show: Ncalls Tottime Cumtime
  2. PyEval_SetTrace is similar to PyEval_SetProfile, except the tracing function does receive line-number events.