SlideShare une entreprise Scribd logo
1  sur  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/

Contenu connexe

Dernier

%+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
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
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
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
%+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
 
+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
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Dernier (20)

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%+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...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
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...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%+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...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%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
 
+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...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%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
 

En vedette

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)
 

En vedette (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/

Notes de l'éditeur

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