SlideShare a Scribd company logo
1 of 21
Download to read offline
LET’S BUILD A PYTHON
PROFILER IN 25 LOC!
by Noam Elfanbaum
Navigate : Space / Arrow Keys | - Menu | - Fullscreen | - Overview | - Blackout | - Speaker | - HelpM F O B S ?

1 / 21
ABOUT ME
Data Engineering lead at Bluevine
Help organise &
Find me online at &
PywebIL Pycon Israel
@noamelf noamelf.com
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

2 / 21
WHAT IS A PROGRAM PROFILE?
A pro le is a set of statistics that
describes how often and for how long
various parts of the program executed.
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

3 / 21
PYTHON’S (STDLIB) PROFILERS
Python has 2 builtin pro lers in stdlib:
pro le - an early pure Python implementation
cPro le - a C extended pro ler for better
performance
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

4 / 21
Pro ling demo
# profiling_demo.py
def super_power(x):
return x ** x ** x
def count_digits(num):
return len(str(num))
result = super_power(7)
digit_count = count_digits(result)
print(f'Num of digits: {digit_count}')
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

5 / 21
Optimized demo
# optimized_demo.py
import math
def super_power(x):
return x ** x ** x
def count_digits(num):
return int(math.log10(num)) + 1
result = super_power(7)
digit_count = count_digits(result)
print(digit_count)
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

6 / 21
IT'S MAGIC! RIGHT?
When inside a Python program you have pretty easy
access to its stack.
Most pro lers run as part of your Python process.
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

7 / 21
Accessing the process call stack
# stack_access.py
import sys
import traceback
def show_stack():
for _, call_stack in sys._current_frames().items():
for frame in traceback.extract_stack(call_stack):
print(f'{frame.filename}:{frame.lineno}:'
f'{frame.name} - "{frame.line}"')
def bar():
show_stack()
bar()
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

8 / 21
TYPES OF TRIGGERS -> TYPE OF PROFILES
There are two types of pro lers that differ upon their
triggers:
Deterministic pro lers - triggered on function/line
called (like pro le/cPro le)
Statistical pro lers - triggered on a time interval
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

9 / 21
HOW DO DETERMINISTIC PROFILERS
WORK?
Python let you specify a callback that gets run when
interpreter events happen:
sys.setprofile - triggered when a function or
a line of code is called
sys.settrace - triggered only when a function
is called
When the callback gets called, it records the stack
for later analysis.
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

10 / 21
Using setpro le
# setprofile.py
import sys
import traceback
def profiler(call_stack, event, arg):
line = traceback.extract_stack(call_stack)[0]
print(f'event: {event} | arg: {arg} | line: {line.line}')
sys.setprofile(profiler)
print('Hello world!')
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

11 / 21
HOW DO STATISTICAL PROFILERS WORK?
Statistical pro lers sample the program on a given
interval.
One way to implement the sampling is to ask the OS
kernel to interrupt the program on a given interval.
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

12 / 21
Using OS signals to trigger sampling
# statistical_sampling.py
import atexit, signal, traceback
def print_handler(signum, call_stack):
line = traceback.extract_stack(call_stack)[0]
print(f'line: {line.line} (signum={signum})')
def start(handler, interval=1):
signal.signal(signal.SIGPROF, handler)
signal.setitimer(signal.ITIMER_PROF, interval, interval)
atexit.register(lambda: signal.setitimer(
signal.ITIMER_PROF, 0))
start(print_handler)
result = 7 ** 7 ** 7
print(len(str(result)))
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

13 / 21
WHEN TO USE WHICH PROFILER?
Statistical pro lers:
Low, controllable and predicted overhead is
possible by optimizing the sampling interval
Less accurate result since it, by design, misses
function/line calls.
More suitable for continuous, low impact
production monitoring.
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

14 / 21
Deterministic pro lers:
Introduces a xed amount of latency for every
function call / line of code executed.
Collects the exact program execution stack
More suitable for interactive/local debugging.
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

15 / 21
NOW, LET’S BUILD A
(NAIVE) STATISTICAL
PROFILER IN 25 LOC!
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

16 / 21
Statistical Flame graph Pro ler
import atexit
import collections
import signal
import traceback
stats = collections.defaultdict(int)
def _sample(_, call_stack):
stack = traceback.extract_stack(call_stack)
formatted_stack = ';'.join(line.line for line in stack)
stats[formatted_stack] += 1
def start(interval=0.005):
signal signal(signal SIGPROF sample)
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

17 / 21
Simple usage of sfPro ler
import sfProfiler
def calc(x):
return x ** x
def main():
calc(100_000)
calc(200_000)
sfProfiler.start()
main()
print(sfProfiler.format_stats())
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

18 / 21
A more complex example
import sys
import tempfile
import urllib.request
import sfProfiler
def save_pep(pep):
url = f'https://www.python.org/dev/peps/pep-{pep:04}'
with urllib.request.urlopen(url) as request:
html = request.read()
with tempfile.TemporaryFile('wb') as f:
f.write(html)
def main():
for pep in range(4):
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

19 / 21
REFERENCE
Brendan Gregg's tool
Juila Evans about Ruby and Python pro lers
Nylas performance where they explain how
they built a homemade performance monitoring
service (recommended!).
Python's
This talk can be found on
Flame Graph
post
post
pro lers docs
Github
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

20 / 21
Thanks!
[ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]

21 / 21

More Related Content

What's hot

What's hot (20)

Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
 
MPHS RC Design Flow
MPHS RC Design FlowMPHS RC Design Flow
MPHS RC Design Flow
 
XREST Protocol
XREST ProtocolXREST Protocol
XREST Protocol
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)
 
Lesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th stepLesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th step
 
C++ Events
C++ EventsC++ Events
C++ Events
 
Lesson 3.1 variables and constant
Lesson 3.1 variables and constantLesson 3.1 variables and constant
Lesson 3.1 variables and constant
 
3DD 1e Laura
3DD 1e Laura3DD 1e Laura
3DD 1e Laura
 
4Developers 2015: Clean JavaScript code - only dream or reality - Sebastian Ł...
4Developers 2015: Clean JavaScript code - only dream or reality - Sebastian Ł...4Developers 2015: Clean JavaScript code - only dream or reality - Sebastian Ł...
4Developers 2015: Clean JavaScript code - only dream or reality - Sebastian Ł...
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Lesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving processLesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving process
 
GCC compiler
GCC compilerGCC compiler
GCC compiler
 
Al2ed chapter17
Al2ed chapter17Al2ed chapter17
Al2ed chapter17
 
Gccgdb
GccgdbGccgdb
Gccgdb
 
Lesson 5 .1 selection structure
Lesson 5 .1 selection structureLesson 5 .1 selection structure
Lesson 5 .1 selection structure
 
Code optimisation presnted
Code optimisation presntedCode optimisation presnted
Code optimisation presnted
 
Optimization
OptimizationOptimization
Optimization
 
LTO plugin
LTO pluginLTO plugin
LTO plugin
 
From System Modeling to Automated System Testing
From System Modeling to Automated System TestingFrom System Modeling to Automated System Testing
From System Modeling to Automated System Testing
 

Similar to Let’s Build a Python Profiler in 25 LOC

1 Vampir Overview
1 Vampir Overview1 Vampir Overview
1 Vampir Overview
PTIHPA
 
Preparing to program Aurora at Exascale - Early experiences and future direct...
Preparing to program Aurora at Exascale - Early experiences and future direct...Preparing to program Aurora at Exascale - Early experiences and future direct...
Preparing to program Aurora at Exascale - Early experiences and future direct...
inside-BigData.com
 
Track A-Compilation guiding and adjusting - IBM
Track A-Compilation guiding and adjusting - IBMTrack A-Compilation guiding and adjusting - IBM
Track A-Compilation guiding and adjusting - IBM
chiportal
 
Cray XT Porting, Scaling, and Optimization Best Practices
Cray XT Porting, Scaling, and Optimization Best PracticesCray XT Porting, Scaling, and Optimization Best Practices
Cray XT Porting, Scaling, and Optimization Best Practices
Jeff Larkin
 
May2010 hex-core-opt
May2010 hex-core-optMay2010 hex-core-opt
May2010 hex-core-opt
Jeff Larkin
 

Similar to Let’s Build a Python Profiler in 25 LOC (20)

1 Vampir Overview
1 Vampir Overview1 Vampir Overview
1 Vampir Overview
 
eBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging InfrastructureeBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging Infrastructure
 
Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance Tooling
 
Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023
 
RHEL8-BETA-RHUG.pdf
RHEL8-BETA-RHUG.pdfRHEL8-BETA-RHUG.pdf
RHEL8-BETA-RHUG.pdf
 
Preparing to program Aurora at Exascale - Early experiences and future direct...
Preparing to program Aurora at Exascale - Early experiences and future direct...Preparing to program Aurora at Exascale - Early experiences and future direct...
Preparing to program Aurora at Exascale - Early experiences and future direct...
 
Squeezing Blood From a Stone V1.2
Squeezing Blood From a Stone V1.2Squeezing Blood From a Stone V1.2
Squeezing Blood From a Stone V1.2
 
Track A-Compilation guiding and adjusting - IBM
Track A-Compilation guiding and adjusting - IBMTrack A-Compilation guiding and adjusting - IBM
Track A-Compilation guiding and adjusting - IBM
 
Functioning incessantly of Data Science Platform with Kubeflow - Albert Lewan...
Functioning incessantly of Data Science Platform with Kubeflow - Albert Lewan...Functioning incessantly of Data Science Platform with Kubeflow - Albert Lewan...
Functioning incessantly of Data Science Platform with Kubeflow - Albert Lewan...
 
Spryker meetup-distribute-your-spryker-deployment-with-docker-and-kubernetes
Spryker meetup-distribute-your-spryker-deployment-with-docker-and-kubernetesSpryker meetup-distribute-your-spryker-deployment-with-docker-and-kubernetes
Spryker meetup-distribute-your-spryker-deployment-with-docker-and-kubernetes
 
LIFT: A Legacy InFormation retrieval Tool
LIFT: A Legacy InFormation retrieval ToolLIFT: A Legacy InFormation retrieval Tool
LIFT: A Legacy InFormation retrieval Tool
 
Cray XT Porting, Scaling, and Optimization Best Practices
Cray XT Porting, Scaling, and Optimization Best PracticesCray XT Porting, Scaling, and Optimization Best Practices
Cray XT Porting, Scaling, and Optimization Best Practices
 
使用 Prometheus 監控 Kubernetes Cluster
使用 Prometheus 監控 Kubernetes Cluster 使用 Prometheus 監控 Kubernetes Cluster
使用 Prometheus 監控 Kubernetes Cluster
 
Felix Müller: Live Coding: Zeebe - Camunda Days Oslo, CPH, Amsterdam
Felix Müller: Live Coding: Zeebe - Camunda Days Oslo, CPH, AmsterdamFelix Müller: Live Coding: Zeebe - Camunda Days Oslo, CPH, Amsterdam
Felix Müller: Live Coding: Zeebe - Camunda Days Oslo, CPH, Amsterdam
 
Probe Debugging
Probe DebuggingProbe Debugging
Probe Debugging
 
Conf42 Python_ ML Enhanced Event Streaming Apps with Python Microservices
Conf42 Python_ ML Enhanced Event Streaming Apps with Python MicroservicesConf42 Python_ ML Enhanced Event Streaming Apps with Python Microservices
Conf42 Python_ ML Enhanced Event Streaming Apps with Python Microservices
 
Digital design with Systemc
Digital design with SystemcDigital design with Systemc
Digital design with Systemc
 
Optimizing Your CI Pipelines
Optimizing Your CI PipelinesOptimizing Your CI Pipelines
Optimizing Your CI Pipelines
 
May2010 hex-core-opt
May2010 hex-core-optMay2010 hex-core-opt
May2010 hex-core-opt
 
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
 

Recently uploaded

Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 

Recently uploaded (20)

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 

Let’s Build a Python Profiler in 25 LOC

  • 1. LET’S BUILD A PYTHON PROFILER IN 25 LOC! by Noam Elfanbaum Navigate : Space / Arrow Keys | - Menu | - Fullscreen | - Overview | - Blackout | - Speaker | - HelpM F O B S ?  1 / 21
  • 2. ABOUT ME Data Engineering lead at Bluevine Help organise & Find me online at & PywebIL Pycon Israel @noamelf noamelf.com [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  2 / 21
  • 3. WHAT IS A PROGRAM PROFILE? A pro le is a set of statistics that describes how often and for how long various parts of the program executed. [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  3 / 21
  • 4. PYTHON’S (STDLIB) PROFILERS Python has 2 builtin pro lers in stdlib: pro le - an early pure Python implementation cPro le - a C extended pro ler for better performance [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  4 / 21
  • 5. Pro ling demo # profiling_demo.py def super_power(x): return x ** x ** x def count_digits(num): return len(str(num)) result = super_power(7) digit_count = count_digits(result) print(f'Num of digits: {digit_count}') [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  5 / 21
  • 6. Optimized demo # optimized_demo.py import math def super_power(x): return x ** x ** x def count_digits(num): return int(math.log10(num)) + 1 result = super_power(7) digit_count = count_digits(result) print(digit_count) [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  6 / 21
  • 7. IT'S MAGIC! RIGHT? When inside a Python program you have pretty easy access to its stack. Most pro lers run as part of your Python process. [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  7 / 21
  • 8. Accessing the process call stack # stack_access.py import sys import traceback def show_stack(): for _, call_stack in sys._current_frames().items(): for frame in traceback.extract_stack(call_stack): print(f'{frame.filename}:{frame.lineno}:' f'{frame.name} - "{frame.line}"') def bar(): show_stack() bar() [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  8 / 21
  • 9. TYPES OF TRIGGERS -> TYPE OF PROFILES There are two types of pro lers that differ upon their triggers: Deterministic pro lers - triggered on function/line called (like pro le/cPro le) Statistical pro lers - triggered on a time interval [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  9 / 21
  • 10. HOW DO DETERMINISTIC PROFILERS WORK? Python let you specify a callback that gets run when interpreter events happen: sys.setprofile - triggered when a function or a line of code is called sys.settrace - triggered only when a function is called When the callback gets called, it records the stack for later analysis. [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  10 / 21
  • 11. Using setpro le # setprofile.py import sys import traceback def profiler(call_stack, event, arg): line = traceback.extract_stack(call_stack)[0] print(f'event: {event} | arg: {arg} | line: {line.line}') sys.setprofile(profiler) print('Hello world!') [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  11 / 21
  • 12. HOW DO STATISTICAL PROFILERS WORK? Statistical pro lers sample the program on a given interval. One way to implement the sampling is to ask the OS kernel to interrupt the program on a given interval. [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  12 / 21
  • 13. Using OS signals to trigger sampling # statistical_sampling.py import atexit, signal, traceback def print_handler(signum, call_stack): line = traceback.extract_stack(call_stack)[0] print(f'line: {line.line} (signum={signum})') def start(handler, interval=1): signal.signal(signal.SIGPROF, handler) signal.setitimer(signal.ITIMER_PROF, interval, interval) atexit.register(lambda: signal.setitimer( signal.ITIMER_PROF, 0)) start(print_handler) result = 7 ** 7 ** 7 print(len(str(result))) [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  13 / 21
  • 14. WHEN TO USE WHICH PROFILER? Statistical pro lers: Low, controllable and predicted overhead is possible by optimizing the sampling interval Less accurate result since it, by design, misses function/line calls. More suitable for continuous, low impact production monitoring. [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  14 / 21
  • 15. Deterministic pro lers: Introduces a xed amount of latency for every function call / line of code executed. Collects the exact program execution stack More suitable for interactive/local debugging. [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  15 / 21
  • 16. NOW, LET’S BUILD A (NAIVE) STATISTICAL PROFILER IN 25 LOC! [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  16 / 21
  • 17. Statistical Flame graph Pro ler import atexit import collections import signal import traceback stats = collections.defaultdict(int) def _sample(_, call_stack): stack = traceback.extract_stack(call_stack) formatted_stack = ';'.join(line.line for line in stack) stats[formatted_stack] += 1 def start(interval=0.005): signal signal(signal SIGPROF sample) [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  17 / 21
  • 18. Simple usage of sfPro ler import sfProfiler def calc(x): return x ** x def main(): calc(100_000) calc(200_000) sfProfiler.start() main() print(sfProfiler.format_stats()) [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  18 / 21
  • 19. A more complex example import sys import tempfile import urllib.request import sfProfiler def save_pep(pep): url = f'https://www.python.org/dev/peps/pep-{pep:04}' with urllib.request.urlopen(url) as request: html = request.read() with tempfile.TemporaryFile('wb') as f: f.write(html) def main(): for pep in range(4): [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  19 / 21
  • 20. REFERENCE Brendan Gregg's tool Juila Evans about Ruby and Python pro lers Nylas performance where they explain how they built a homemade performance monitoring service (recommended!). Python's This talk can be found on Flame Graph post post pro lers docs Github [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  20 / 21
  • 21. Thanks! [ GitPitch @ github/noamelf/Lets-build-a-Python-profiler-in-25-LOC ]  21 / 21