Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Thread Profiling in Python

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Thread Profiling in Python

How to create your own statistical thread profiler in Python language.

Amjith

May 15, 2013

Transcript

93 slides
  1. @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(<module>) 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} Wednesday, May 15, 13
  2. @amjithr $ python -m cProfile shorten.py 88466 function calls (86374

    primitive calls) in 0.220 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 <string>:1(<lambda>) 1 0.000 0.000 0.000 0.000 <string>:1(<module>) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedCTE) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedCase) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedCast) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedClauseElement) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedClauseList) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedColumn) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedColumnClause) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedColumnElement) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedCompoundSelect) 1 0.000 0.000 0.000 0.000 <string>:1(AnnotatedDelete) .... .... ..... ..... .... ...... .... .... ..... ..... .... ...... Wednesday, May 15, 13
  3. @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 Wednesday, May 15, 13
  4. @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 ~ % OVERHEAD Wednesday, May 15, 13
  5. @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 Wednesday, May 15, 13
  6. @amjithr PROs CONs Accurate timer Unix only - Ignored in

    mod_wsgi - Can’t interrupt C-extensions SIGNAL Wednesday, May 15, 13
  7. @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 Wednesday, May 15, 13
  8. @amjithr PROs CONs Cross Platform Inaccurate for CPU tasks mod_wsgi

    compatible Can’t interrupt C-extensions THREAD Wednesday, May 15, 13
  9. @amjithr >>> import sys # Get stack frames of every

    thread >>> frames = sys._current_frames() # Dictionary of { thread_id: frame_obj } >>> frames {4420: <frame object at 0x7fd96c04a650>, 1407: <frame object at 0x7fd96c301730>} INQUIRE Wednesday, May 15, 13
  10. @amjithr function3 function2 function1 Fn Name File Name Line No

    Arguments Globals Locals ... FRAMES Wednesday, May 15, 13
  11. @amjithr function3 function2 function1 Fn Name File Name Line No

    Arguments Globals Locals ... FRAMES Wednesday, May 15, 13
  12. @amjithr >>> import traceback # Extract useful info from frame

    >>> traceback.extract_stack(frame) [('code.py', 88, 'runsource', 'return'), ('code.py', 110, 'runcode', 'print')] EXTRACT Wednesday, May 15, 13
  13. @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 Wednesday, May 15, 13
  14. @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 Wednesday, May 15, 13
  15. @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 Wednesday, May 15, 13
  16. @amjithr WRONG USE OF MEME. WHO LET THIS GUY ON

    THE STAGE? PROFILER in PRODUCTION Wednesday, May 15, 13
  17. Questions? • Promo: newrelic.com/djangocon • 30 days of Pro. •

    Free T-shirt. • In-depth data. Wednesday, May 15, 13

More Decks by Amjith

Other Decks in Programming