Example #1
0
    def wrap(*args, **kwargs):
        ret = profilr(func)(*args, **kwargs)

        stats = profilr.get_stats()
        with open(uniquify(file + '.txt'), 'w') as s:
            lp.show_text(stats.timings, stats.unit, stream=s)

        return ret
Example #2
0
def process_profiler(prof):
    if Options()['misc'].get('profile', False):
        # unavoidable lazy import -- only if profiler is enabled
        from line_profiler import show_text
        results_file = os.path.join(Options()['exp']['dir'], 'profile_results.lprof')
        Logger()('Saving profiler results to {}...'.format(results_file))
        prof.dump_stats(results_file)
        stats = prof.get_stats()
        textio = io.StringIO()
        show_text(stats.timings, stats.unit, stream=textio)
        lines = textio.getvalue()
        Logger()('Printing profiling results', log_level=Logger.SYSTEM)
        for line in lines.splitlines():
            Logger()(line, log_level=Logger.SYSTEM, print_header=False)
Example #3
0
def show_stats_file(stats_file):
    """Create utils for display stats."""
    if not show_text:
        print("Please use 'pip install line_profiler`, return with nothing do!")
        return

    def load_stats(filename):
        """Create utility function to load a pickled LineStats object from a given filename."""
        with open(filename, 'rb') as stats_handle:
            return pickle.load(stats_handle)

    print(load_stats(stats_file))
    tmp_lp = load_stats(stats_file)
    show_text(tmp_lp.timings, tmp_lp.unit)
    sleep(0.1)
    sys.stdout.flush()
Example #4
0
    def __repr__(self):
        label = function_label(self.function)
        timings = {label: self._line_stats_in_seconds}  # format needed for show_text
        unit = 1.

        class Stream(object):
            def __init__(self):
                self.items = []
            def write(self, s):
                self.items.append(s)
            def get_value(self):
                return ''.join(self.items)

        output = Stream()
        line_profiler.show_text(timings, unit, output)
        s = output.get_value()
        return s
Example #5
0
def save_and_dump_stats(profiler, stats_file="default_stats.pkl"):
    """Create utils for save stats into file."""
    if not profiler:
        print("invalid profiler handler!")
        return

    if os.path.exists(stats_file):
        print("remove {}, and re-write it.".format(stats_file))
        os.remove(stats_file)
    else:
        print("write into file: {}".format(stats_file))
    try:
        # if profiler.print_stats(), will can't be dump.
        stats_info = profiler.get_stats()
        show_text(stats_info.timings, stats_info.unit)
        sys.stdout.flush()
        profiler.dump_stats(stats_file)
    # fixme: too general except
    except BaseException:
        print("profiler end without dump stats!")
Example #6
0
def display_line_profiling(request, filename='/tmp/lpstats', mode='html'):
    """
    This will display saved line-profiler output as a web page (default) or return it as a string (mode='string')
    To display, you will need to have this line in your urls.py:
        url(r'^profile/line/$', 'decorators.views.display_line_profiling'),
    and have the filename to read in the GET paramaters (eg: http://localhost/profile/line/?filename=/tmp/lpstats )

    :param request: request object
    :param filename: line-profiler stats file path, created by dump_stats()
    :param mode: return as a web page ('html') or as a 'string'
    :return:
    """
    if DEBUG == False:
        logger.error('display_line_profiling() disabled in Production mode')
    else:
        filename =  request.GET.get('filename', '/tmp/lpstats')
        logger.error('Getting display_line_profiling(%s) results', filename)

        try:
            lstats = load_stats(filename)

            # Turn on output buffering to capture STDOUT to a string
            old_stdout = sys.stdout  # Need to save it for later
            sys.stdout = StringIO.StringIO()  # Replace stdout with StringIO instance

            # run code that outputs to STDOUT, but capture it with output buffering
            show_text(lstats.timings, lstats.unit)

            data = sys.stdout.getvalue()  # Retrieve the buffered data.
            sys.stdout = old_stdout  # Set stdout to the real one. ( stops buffering )

        except Exception as e:
            logger.error('display_line_profiling(%s) failed: %s', (filename, e))
            data = "Error reading line profiling data"

        if mode == 'html':
            return HttpResponse('<pre>%s</pre>' % data, mimetype='text/html')
        else:
            return data
Example #7
0
 def print_line_profile(self, line_profile_pickled, **kwargs):
     lp_data = self.decode_line_profile(line_profile_pickled)
     line_profiler.show_text(lp_data.timings, lp_data.unit, **kwargs)
Example #8
0
 def print_stats(self, stream=None):
     """ Write out the results of the timing so far. """
     stats = self.get_stats()
     show_text(stats.timings, stats.unit, stream=stream)
Example #9
0
 def print_stats(self, stream=None):
     """ Write out the results of the timing so far. """
     stats = self.get_stats()
     show_text(stats.timings, stats.unit, stream=stream)
Example #10
0
# -- coding = 'utf-8' --
# Author Kylin
# Python Version 3.7.3
# OS macOS

import line_profiler
from line_profiler import LineProfiler
import random


def do_something(numbers):
    s = sum(numbers)
    l = [numbers[i] / 43 for i in range(len(numbers))]


numbers = [random.randint(1, 100) for i in range(1000)]
lp = LineProfiler()  # 实例化对象
lp_wrapper = lp(do_something)  # 封装想要处理的函数
lp_wrapper(numbers)  # 传入参数
lp.print_stats()  # 查看LineProfiler的状态,显示各阶段的运行时间】

# 保存
# profile结果保存到test.lp
lp.dump_stats("test.lp")

# 重新载入
# 载入二进制文件
lstats = line_profiler.load_stats("test.lp")
# 打印到标准输出
line_profiler.show_text(lstats.timings, lstats.unit)