@amjithr
WRITE YOUR OWN
WRITE YOUR OWN STATISTICAL THREAD PROFILER
STATISTICAL
THREAD PROFILER
PDX PYTHON ■
Tuesday, February 12, 13
Slide 2
Slide 2 text
@amjithr
Hi
Tuesday, February 12, 13
Slide 3
Slide 3 text
@amjithr
♥
Tuesday, February 12, 13
Slide 4
Slide 4 text
@amjithr
Tuesday, February 12, 13
Slide 5
Slide 5 text
@amjithr
PERFORMANCE!
Tuesday, February 12, 13
Slide 6
Slide 6 text
@amjithr
WHAT’S
PROFILING?
Tuesday, February 12, 13
Slide 7
Slide 7 text
@amjithr
WHAT’S
PROFILING?
Measuring Performance
Tuesday, February 12, 13
Slide 8
Slide 8 text
@amjithr
cPROFILE
Tuesday, February 12, 13
Slide 9
Slide 9 text
@amjithr
$ python -m cProfile lcm.py
7780242 function calls in 3.822 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 3.822 3.822 lcm.py:12()
1 2.383 2.383 3.822 3.822 lcm.py:12(lcm)
3890120 0.724 0.000 0.724 0.000 {max}
1 0.000 0.000 0.000 0.000 {method 'disable' of
3890119 0.716 0.000 0.716 0.000 {min}
Tuesday, February 12, 13
Slide 10
Slide 10 text
@amjithr
RunSnakeRun
Tuesday, February 12, 13
Slide 11
Slide 11 text
@amjithr
Tuesday, February 12, 13
Slide 12
Slide 12 text
@amjithr
$ time python fib.py
python fib.py 5.43s user 0.01s system 99% cpu 5.451 total
$ time python -m cProfile fib.py
python -m cProfile fib.py 9.63s user 0.04s system 99% cpu 9.670 total
Tuesday, February 12, 13
Slide 13
Slide 13 text
@amjithr
You can’t run it in production
Tuesday, February 12, 13
Slide 14
Slide 14 text
@amjithr
SLOW
PRODUCTION
NOT REALISTIC
cProfile
Tuesday, February 12, 13
Slide 15
Slide 15 text
@amjithr
Cool Story,
Bro!
let’s talk about statistical profilers
Tuesday, February 12, 13
Slide 16
Slide 16 text
@amjithr
STATISTICAL
THREAD PROFILER
Tuesday, February 12, 13
Slide 17
Slide 17 text
@amjithr
Tuesday, February 12, 13
Slide 18
Slide 18 text
@amjithr
WAT?
Tuesday, February 12, 13
Slide 19
Slide 19 text
@amjithr
OVERLY ATTACHED
Tuesday, February 12, 13
Slide 20
Slide 20 text
@amjithr
OVERLY ATTACHED
Interrupt Inquire Collate
Tuesday, February 12, 13
Slide 21
Slide 21 text
@amjithr
OVERLY ATTACHED
Interrupt Inquire Collate
Tuesday, February 12, 13
@amjithr
SIGNALS
THREADS
INTERRUPT
Tuesday, February 12, 13
Slide 26
Slide 26 text
@amjithr
HTTP://FLIC.KR/P/bE NXH
SIGNALS
Tuesday, February 12, 13
Slide 27
Slide 27 text
@amjithr
import signal
# Use the virtual timer
sig = signal.ITIMER_VIRTUAL
# register a handler
signal.signal(sig, handler)
# 100ms repeating timer
signal.setitimer(sig, 0.1, 0.1)
SIGNAL
Tuesday, February 12, 13
Slide 28
Slide 28 text
@amjithr
PROs CONs
Accurate timer Unix only
- Ignored in mod_wsgi
-
Can’t interrupt
C-extensions
SIGNAL
Tuesday, February 12, 13
Slide 29
Slide 29 text
@amjithr
HTTP://FLIC.KR/P/db ZS
THREADS
Tuesday, February 12, 13
Slide 30
Slide 30 text
@amjithr
import threading
# Create a background thread
t = threading.Thread(target=handler)
t.setDaemon(True)
# Start the thread
t.start()
# Sleep for 100ms in handler
time.sleep(0.1)
THREAD
Tuesday, February 12, 13
Slide 31
Slide 31 text
@amjithr
PROs CONs
Cross Platform
Inaccurate for CPU
tasks
mod_wsgi compatible
Can’t interrupt
C-extensions
THREAD
Tuesday, February 12, 13
Slide 32
Slide 32 text
@amjithr
INQUIRE
Stack frame of every thread
Tuesday, February 12, 13
Slide 33
Slide 33 text
@amjithr
>>> import sys
# Get stack frames of every thread
>>> frames = sys._current_frames()
# Dictionary of { thread_id: frame_obj }
>>> frames
{4420: ,
1407: }
INQUIRE
Tuesday, February 12, 13
Slide 34
Slide 34 text
@amjithr
function3
function2
function1
FRAMES
Tuesday, February 12, 13
Slide 35
Slide 35 text
@amjithr
function3
function2
function1
Fn Name
File Name
Line No
Arguments
Globals
Locals
...
FRAMES
Tuesday, February 12, 13
Slide 36
Slide 36 text
@amjithr
function3
function2
function1
Fn Name
File Name
Line No
Arguments
Globals
Locals
...
FRAMES
Tuesday, February 12, 13
Slide 37
Slide 37 text
@amjithr
>>> import traceback
# Extract useful info from frame
>>> traceback.extract_stack(frame)
[('code.py', 88, 'runsource', 'return'),
('code.py', 110, 'runcode', 'print')]
EXTRACT
Tuesday, February 12, 13
Slide 38
Slide 38 text
@amjithr
COLLATE
Tuesday, February 12, 13
Slide 39
Slide 39 text
@amjithr
Default Dictionary
ATTEMPT
Tuesday, February 12, 13
Slide 40
Slide 40 text
@amjithr
Default Dictionary
Uniquely identify a
function
fdata = (f_name, file, line)
Build a dictionary calls = defaultdict(int)
Increment the value
when found in
frames
calls[fn_a] += 1
ATTEMPT
Tuesday, February 12, 13
Slide 41
Slide 41 text
@amjithr
Default Dictionary
Uniquely identify a
function
fdata = (f_name, file, line)
Build a dictionary calls = defaultdict(int)
Increment the value
when found in
frames
calls[fn_a] += 1
ATTEMPT
Tuesday, February 12, 13
Slide 42
Slide 42 text
@amjithr
Default Dictionary
Uniquely identify a
function
fdata = (f_name, file, line)
Build a dictionary calls = defaultdict(int)
Increment the value
when found in
frames
calls[fn_a] += 1
ATTEMPT
Tuesday, February 12, 13