Last active
October 5, 2016 17:08
-
-
Save AlwxSin/0b3ef04e63ee518a01705412fac16f8b to your computer and use it in GitHub Desktop.
Python profiling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Collect profiling statistic into graphite""" | |
import socket | |
import time | |
CARBON_SERVER = '127.0.0.1' | |
CARBON_PORT = 2003 | |
class Stats(object): | |
"""Context manager for send stats to graphite""" | |
def __init__(self, name): | |
self.name = name | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, *args): | |
duration = (time.time() - self.start) * 1000 # msec | |
message = '%s %d %d\n' % (self.name, duration, time.time()) | |
sock = socket.socket() | |
sock.connect((CARBON_SERVER, CARBON_PORT)) | |
sock.sendall(message) | |
sock.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Collect profiling statistic into graphite""" | |
import socket | |
import time | |
import cProfile | |
CARBON_SERVER = '127.0.0.1' | |
CARBON_PORT = 2003 | |
def stats(name): | |
"""Decorator for send stats to graphite""" | |
def _timing(func): | |
def _wrapper(*args, **kwargs): | |
start = time.time() | |
result = func(*args, **kwargs) | |
duration = (time.time() - start) * 1000 # msec | |
message = '%s %d %d\n' % (name, duration, time.time()) | |
sock = socket.socket() | |
sock.connect((CARBON_SERVER, CARBON_PORT)) | |
sock.sendall(message) | |
sock.close() | |
return result | |
return _wrapper | |
return _timing | |
def profile(func): | |
"""Decorator for run function profile""" | |
def wrapper(*args, **kwargs): | |
profile_filename = func.__name__ + '.prof' | |
profiler = cProfile.Profile() | |
result = profiler.runcall(func, *args, **kwargs) | |
profiler.dump_stats(profile_filename) | |
return result | |
return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from context_managers import Stats | |
from decorators import stats, profile | |
with Stats('project.application.some_action'): | |
do_some_action() | |
@stats('project.application.some_action') | |
def do_some_action(): | |
"""Doing some useful action""" | |
@profile | |
def do_another_action(): | |
"""Doing another useful action""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Graphite to log time data | |
http://graphiteapp.org/ | |
StatsD for aggregation | |
https://github.com/etsy/statsd/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment