def profile_list_deserialization(serializer, child_serializer, data_list):
    if os.environ.get('CI', None) != 'true' or not LineProfiler:
        return
    profile = LineProfiler(serializer.to_internal_value, child_serializer.to_internal_value)
    profile.enable()
    serializer.to_internal_value(data_list)
    profile.disable()
    profile.print_stats()
def profile_list_serialization(serializer, child_serializer, instances_list):
    if os.environ.get('CI', None) != 'true' or not LineProfiler:
        return
    profile = LineProfiler(serializer.instances_list, child_serializer.instances_list)
    profile.enable()
    serializer.to_representation(instances_list)
    profile.disable()
    profile.print_stats()
def profile_list_deserialization(serializer, child_serializer, data_list):
    if os.environ.get('CI', None) != 'true' or not LineProfiler:
        return
    profile = LineProfiler(serializer.to_internal_value,
                           child_serializer.to_internal_value)
    profile.enable()
    serializer.to_internal_value(data_list)
    profile.disable()
    profile.print_stats()
Esempio n. 4
0
def main():
    profiler = cProfile.Profile()

    profiler.enable()
    function_runner('original_method')
    function_runner('step_one')
    function_runner('step_two')
    function_runner('step_three')
    function_runner('step_four')
    function_runner('step_five')
    function_runner('step_six')
    function_runner('step_seven')
    function_runner('step_eight')
    function_runner('step_nine')
    function_runner('current')
    profiler.disable()

    profiler.dump_stats('function_event.stats')
    line_profiler = LineProfiler(CurrentFunctionContainer().current)
    line_profiler.enable()
    function_runner('current')
    line_profiler.disable()
    line_profiler.dump_stats('function_event.line_stats')
    line_profiler.print_stats()

    print 'Original', timeit.timeit(
        lambda: function_runner('original_method'), number=7)
    print 'One', timeit.timeit(
        lambda: function_runner('step_one'), number=7)
    print 'Two', timeit.timeit(
        lambda: function_runner('step_two'), number=7)
    print 'Three', timeit.timeit(
        lambda: function_runner('step_three'), number=7)
    print 'Four', timeit.timeit(
        lambda: function_runner('step_four'), number=7)
    print 'Five', timeit.timeit(
        lambda: function_runner('step_five'), number=7)
    print 'Six', timeit.timeit(
        lambda: function_runner('step_six'), number=7)
    print 'Seven', timeit.timeit(
        lambda: function_runner('step_seven'), number=7)
    print 'Eight', timeit.timeit(
        lambda: function_runner('step_eight'), number=7)
    print 'Nine', timeit.timeit(
        lambda: function_runner('step_nine'), number=7)
    print 'Current', timeit.timeit(
        lambda: function_runner('current'), number=7)
Esempio n. 5
0
def run_profiling(args):
    lprofiler = LineProfiler()

    monitor_fuctions = [
        api.problem.submit_key, api.problem.get_unlocked_pids,
        api.problem.get_solved_pids, api.problem.get_all_problems,
        api.problem.get_solved_problems, api.stats.get_score,
        api.cache.memoize, api.autogen.grade_problem_instance,
        api.autogen.get_problem_instance, api.autogen.get_number_of_instances
    ]

    for func in monitor_fuctions:
        lprofiler.add_function(func)

    lprofiler.enable()

    if args.stack:
        profiler = Profiler(use_signal=False)
        profiler.start()

    for func, a, kw in operations:
        func(*a, **kw)

    if args.stack:
        profiler.stop()

    lprofiler.disable()

    if args.print:
        print(profiler.output_text(unicode=True, color=True))
        lprofiler.print_stats()

    output = open(args.output, "w")

    if args.stack:
        output.write(profiler.output_text(unicode=True))

        if args.output_html is not None:
            output_html = open(args.output_html, "w")
            output_html.write(profiler.output_html())
            output_html.close()
            print("Wrote test info to " + args.output_html)

    lprofiler.print_stats(output)
    output.close()
    print("Wrote test info to " + args.output)
Esempio n. 6
0
def run_profiling(args):
    lprofiler = LineProfiler() 

    monitor_fuctions = [api.problem.submit_key, api.problem.get_unlocked_pids, api.problem.get_solved_pids,
                        api.problem.get_all_problems, api.problem.get_solved_problems, api.stats.get_score,
                        api.cache.memoize, api.autogen.grade_problem_instance, api.autogen.get_problem_instance,
                        api.autogen.get_number_of_instances]

    for func in monitor_fuctions:
        lprofiler.add_function(func)

    lprofiler.enable()

    if args.stack:
        profiler = Profiler(use_signal=False)
        profiler.start()

    for func, a, kw in operations:
        func(*a, **kw)

    if args.stack:
        profiler.stop()

    lprofiler.disable()

    if args.print:
        print(profiler.output_text(unicode=True, color=True))
        lprofiler.print_stats()

    output = open(args.output, "w")

    if args.stack:
        output.write(profiler.output_text(unicode=True))

        if args.output_html is not None:
            output_html = open(args.output_html, "w")
            output_html.write(profiler.output_html())
            output_html.close()
            print("Wrote test info to " + args.output_html)

    lprofiler.print_stats(output)
    output.close()
    print("Wrote test info to " + args.output)
Esempio n. 7
0
class SpecialTestRunner(SpecialTest):
    """
    Test runner, calls the specified test under specified profiler
    Mode = None - no profiler, "c" - cProfile, "l" - LineProfiler, "h" - hotshot
    """
    def __init__(self, test, mode=None):
        super(SpecialTestRunner, self).__init__()

        self.mode = mode
        self.test = test
        self.profiler = None

    def setup(self):
        if self.mode == 'c':
            import cProfile
            self.profiler = cProfile.Profile()

        elif self.mode == 'l':
            from line_profiler import LineProfiler
            self.profiler = LineProfiler()

        elif self.mode == 'h':
            import hotshot
            self.info['name'] = 'special.prof'
            self.profiler = hotshot.Profile(self.info['name'])

        self.test.setup()

    def run(self):
        if self.mode == 'c':
            self.profiler.enable()
        elif self.mode == 'l':
            self.profiler.enable_by_count()

            self.profiler.add_function(Handler.handle)
            self.profiler.add_function(Condition.check_string_match)
            self.profiler.add_function(Condition.check_function)
            self.profiler.add_function(Condition.check_list)

        t = Timer()

        # Run itself
        if self.mode == 'h':
            self.profiler.runcall(self.test.run)
        else:
            self.test.run()

        print('Test time: %s' % t.delta())

        if self.mode == 'c':
            import pstats
            import StringIO

            self.profiler.disable()
            sio = StringIO.StringIO()

            ps = pstats.Stats(self.profiler, stream=sio).sort_stats('time')
            ps.print_stats()

            print(sio.getvalue())

        elif self.mode == 'h':
            import hotshot.stats

            print('Processing results...')

            self.profiler.close()
            name = self.info['name']
            stats = hotshot.stats.load(name)
            stats.strip_dirs()
            stats.sort_stats('time', 'calls')
            stats.print_stats(50)

            print(
                'Run "hotshot2calltree -o %s.out %s" to generate the cachegrind file'
                % (name, name))

        elif self.mode == 'l':
            self.profiler.disable()
            self.profiler.print_stats()
Esempio n. 8
0
class SpecialTestRunner(SpecialTest):
    """
    Test runner, calls the specified test under specified profiler
    Mode = None - no profiler, "c" - cProfile, "l" - LineProfiler, "h" - hotshot
    """
    def __init__(self, test, mode=None):
        super(SpecialTestRunner, self).__init__()

        self.mode = mode
        self.test = test
        self.profiler = None

    def setup(self):
        if self.mode == 'c':
            import cProfile
            self.profiler = cProfile.Profile()

        elif self.mode == 'l':
            from line_profiler import LineProfiler
            self.profiler = LineProfiler()

        elif self.mode == 'h':
            import hotshot
            self.info['name'] = 'special.prof'
            self.profiler = hotshot.Profile(self.info['name'])

        self.test.setup()

    def run(self):
        if self.mode == 'c':
            self.profiler.enable()
        elif self.mode == 'l':
            self.profiler.enable_by_count()

            self.profiler.add_function(Handler.handle)
            self.profiler.add_function(Condition.check_string_match)
            self.profiler.add_function(Condition.check_function)
            self.profiler.add_function(Condition.check_list)

        t = Timer()

        # Run itself
        if self.mode == 'h':
            self.profiler.runcall(self.test.run)
        else:
            self.test.run()

        print('Test time: %s' % t.delta())

        if self.mode == 'c':
            import pstats
            import StringIO

            self.profiler.disable()
            sio = StringIO.StringIO()

            ps = pstats.Stats(self.profiler, stream=sio).sort_stats('time')
            ps.print_stats()

            print(sio.getvalue())

        elif self.mode == 'h':
            import hotshot.stats

            print('Processing results...')

            self.profiler.close()
            name = self.info['name']
            stats = hotshot.stats.load(name)
            stats.strip_dirs()
            stats.sort_stats('time', 'calls')
            stats.print_stats(50)

            print('Run "hotshot2calltree -o %s.out %s" to generate the cachegrind file' % (name, name))

        elif self.mode == 'l':
            self.profiler.disable()
            self.profiler.print_stats()
        count *= i + 1
    return count


if __name__ == '__main__':
    num = 10000
    lp = LineProfiler()
    lp.add_function(test)  # 添加被测试函数
    lp_wrapper = lp(myFunc)
    lp_wrapper(num)
    lp.print_stats()

    lp2 = LineProfiler(test)
    lp2.enable()  # 开始性能分析
    test()
    lp2.disable()  # 停止性能分析
    lp2.print_stats()

# ### line_profiler
# PyPI: https://pypi.org/project/line_profiler/
# 可以统计每行代码的执行次数和执行时间等,时间单位为微秒;
#
# ### 结果说明
# - Total Time: 测试代码的总运行时间
# - File: 代码文件的地址
# - Function: 函数的行号
# - Line: 每行代码的行号
# - Hits: 每行代码的运行次数
# - Time: 每行代码的运行时间
# - Per Hit: 每行代码运行一次的时间
# - % Time: 每行代码运行时间与测试代码的总运行时间的百分比