Ejemplo n.º 1
0
        def inner(*args, **kwargs):
            bm_data = dict()
            bm_data.update(self._bm_static)
            bm_data['_func'] = func
            bm_data['_args'] = args
            bm_data['_kwargs'] = kwargs

            if isinstance(self, MBLineProfiler):
                if not line_profiler:
                    raise ImportError('This functionality requires the '
                                      '"line_profiler" package')
                self._line_profiler = line_profiler.LineProfiler(func)

            self.pre_run_triggers(bm_data)

            if isinstance(self, MBLineProfiler):
                res = self._line_profiler.runcall(func, *args, **kwargs)
            else:
                res = func(*args, **kwargs)

            self.post_run_triggers(bm_data)

            if isinstance(self, MBReturnValue):
                bm_data['return_value'] = res

            # Delete any underscore-prefixed keys
            bm_data = {
                k: v
                for k, v in bm_data.items() if not k.startswith('_')
            }

            self.output_result(bm_data)

            return res
Ejemplo n.º 2
0
    def run_command(self):
        settings = get_settings(self.arguments.configuration)
        app = self.make_app(settings)

        if self.arguments.line_profiler:
            if not HAS_LINE_PROFILER:
                sys.stderr.write(
                    'You must first install line_profiler for the --line-profiler option to work.\n'
                    'Use `pip install line_profiler` to install line_profiler.\n'
                )
                return 1
            self.line_profiler = line_profiler.LineProfiler()
            for func in profile.get_profilable_functions():
                if fnmatch(get_dotted_name(func),
                           self.arguments.line_profiler_matcher or '*'):
                    self.line_profiler.add_function(func)
            self.line_profiler.enable_by_count()

        run_func = self.__run
        if self.arguments.monitor:
            if not HAS_AIOMONITOR:
                sys.stderr.write(
                    'You must install aiomonitor for the '
                    '--monitor option to work.\n'
                    'Use `pip install aiomonitor` to install aiomonitor.\n')
                return 1
            run_func = self.__run_with_monitor

        if self.arguments.profile:
            self.profiler = cProfile.Profile()
            self.profiler.runcall(run_func, app, settings)
        else:
            run_func(app, settings)
Ejemplo n.º 3
0
 def _run(self, cmd):
     """Run the given command under the currently configured profiler."""
     if self.jobinfo.profile_mode == 'mem':
         profiler = _MemoryProfile()
         self._run_profiler(cmd, profiler, 'profile_mem_txt')
     elif self.jobinfo.profile_mode == 'line':
         profiler = None
         try:
             profiler = line_profiler.LineProfiler()
         except NameError:
             martian.throw(
                 'Line-level profiling was requested, but line_profiler was not found.')
         for func in self.funcs:
             profiler.add_function(func)
         self._run_profiler(cmd, profiler, 'profile_line_bin')
         iostr = StringIO.StringIO()
         profiler.print_stats(stream=iostr)
         self.metadata.write_raw('profile_line_txt', iostr.getvalue())
     elif self.jobinfo.profile_mode == 'cpu':
         profiler = cProfile.Profile()
         self._run_profiler(cmd, profiler, 'profile_cpu_bin')
         iostr = StringIO.StringIO()
         stats = pstats.Stats(
             profiler, stream=iostr).sort_stats('cumulative')
         stats.print_stats()
         self.metadata.write_raw('profile_cpu_txt', iostr.getvalue())
     else:
         if self.jobinfo.profile_mode and self.jobinfo.profile_mode != 'disable':
             # Give the profiler a little bit of time to attach.
             time.sleep(0.5)
         cmd()
 def wrapped(*args, **kwargs):
     pr = line_profiler.LineProfiler(func)
     pr.enable()
     result = func(*args, **kwargs)
     pr.disable()
     pr.print_stats()
     return result
Ejemplo n.º 5
0
def main_prof():
    import line_profiler
    prof = line_profiler.LineProfiler(main, Dem3D.create,
                                      Dem3D.make_terrain_grid, Dem3D.fill_base,
                                      Dem3D.to_stl, Dem3D.simplify)
    prof.runcall(main)
    prof.print_stats()
Ejemplo n.º 6
0
def dynamic_profile(func):
    import line_profiler
    profile = line_profiler.LineProfiler()
    new_func = profile(func)
    new_func.profile_info = KernprofParser(profile)
    new_func.print_report = new_func.profile_info.print_report
    return new_func
Ejemplo n.º 7
0
    def process_request(self):
        self.profiler = line_profiler.LineProfiler()

        for f in functions_to_profile:
            self.profiler.add_function(f)

        self.stats = None
Ejemplo n.º 8
0
 def _test_line_profiler(self):
     import line_profiler
     profiler = line_profiler.LineProfiler()
     import pyexpect
     profiler.add_module(pyexpect)
     profiler.run("from pyexpect import expect; expect('foo') == 'foo'")
     profiler.print_stats(stripzeros=True)
     self.fail()
Ejemplo n.º 9
0
 def handler(*args, **kwargs):
     import line_profiler
     profiler = line_profiler.LineProfiler()
     profiler.enable_by_count()
     profiler.add_function(func)
     result = func(*args, **kwargs)
     profiler.print_stats()
     return result
Ejemplo n.º 10
0
 def profiled_func(*args, **kwargs):
     profiler = line_profiler.LineProfiler()
     try:
         profiler.add_function(func_or_list)
         profiler.enable_by_count()
         return func_or_list(*args, **kwargs)
     finally:
         profiler.print_stats()
Ejemplo n.º 11
0
    def __init__(self, original_path, renditions_paths, metrics_list, duration,
                 do_profiling):
        # ************************************************************************
        # Initialize global variables
        # ************************************************************************

        self.original_path = original_path  # Stores system path to original asset
        self.original = cv2.VideoCapture(
            self.original_path
        )  # Initializes original asset to OpenCV VideoCapture class
        self.fps = int(self.original.get(
            cv2.CAP_PROP_FPS))  # Frames Per Second of the original asset
        self.asset_length = int(self.original.get(
            cv2.CAP_PROP_FRAME_COUNT))  # Counts number of frames of the asset
        self.duration = duration  # Establishes how many seconds of the original asset are used
        self.skip_frames = 1  # Defines whether to use all frames or leap frog skip_frames frames
        self.hash_size = 16  # Size of the hash for frame hash analysis in video_metrics
        self.renditions = {
        }  # Dictionary containing dict of renditions ('frame_list',
        #                                           'dimensions',
        #                                           'ID')
        self.metrics = {}  # Dictionary containing dict of metrics
        self.metrics_list = metrics_list  # List of metrics to be extracted from the asset and its renditions
        self.renditions_paths = renditions_paths  # List of paths to renditions

        # Retrieve original rendition dimensions
        self.height = self.original.get(
            cv2.CAP_PROP_FRAME_HEIGHT
        )  # Obtains vertical dimension of the frames of the original
        self.width = self.original.get(
            cv2.CAP_PROP_FRAME_WIDTH
        )  # Obtains horizontal dimension of the frames of the original
        self.dimensions = '{}:{}'.format(int(self.width), int(
            self.height))  # Collects both dimensional values in a string
        if do_profiling:
            import line_profiler
            self.cpu_profiler = line_profiler.LineProfiler()
        else:
            self.cpu_profiler = None
        self.do_profiling = do_profiling
        self.video_metrics = video_metrics(
            self.metrics_list, self.skip_frames, self.hash_size,
            int(self.dimensions[self.dimensions.find(':') + 1:]),
            self.cpu_profiler,
            self.do_profiling)  # Instance of the video_metrics class

        # Convert OpenCV video captures of original to list
        # of numpy arrays for better performance of numerical computations
        self.original = self.capture_to_array(self.original)
        # Compute its features
        self.metrics[self.original_path] = self.compute(
            self.original, self.original_path, self.dimensions)
        # Store the value in the renditions dictionary
        self.renditions['original'] = {
            'frame_list': self.original,
            'dimensions': self.dimensions,
            'ID': self.original_path.split('/')[-2]
        }
Ejemplo n.º 12
0
def profile(loops):
    profile = line_profiler.LineProfiler(nilakantha_infinite_series)
    profile.runcall(nilakantha_infinite_series, loops)
    now = datetime.now()
    file = open(
        "./profile/pi_python_profile_" + now.strftime("%Y-%m-%d_%H-%M-%S") +
        ".txt", 'w+')
    profile.print_stats(file)
    file.close()
Ejemplo n.º 13
0
 def wrapped(*args, **kwargs):
     prof = line_profiler.LineProfiler()
     prof.add_function(view)
     if extra_view:
         [prof.add_function(v) for v in extra_view]
     with prof:
         resp = view(*args, **kwargs)
     prof.print_stats()
     return resp
Ejemplo n.º 14
0
def line_profiling(*functions):
    import line_profiler
    profiler = line_profiler.LineProfiler(*functions)

    try:
        with profiler:
            yield
    finally:
        profiler.print_stats()
Ejemplo n.º 15
0
def start_line_profiler():
    """Start the line profiler"""
    global _active_line_profiler
    _active_line_profiler = line_profiler.LineProfiler()

    xlcAlert(
        "Line Profiler Active\n"
        "Run the function you are interested in and then stop the profiler.\n"
        "Ensure you have decoratored the function with @enable_line_profiler.")
Ejemplo n.º 16
0
def lets_profile():
    import random
    import make_set_cython
    import line_profiler
    A = bytes([random.randrange(256) for i in range(3 * 10000)])
    B = bytes([random.randrange(256) for i in range(3 * 10000)])
    profile = line_profiler.LineProfiler(
        make_set_cython.detect_users_densehash)
    profile.runcall(make_set_cython.detect_users_densehash, A, B)
    profile.print_stats()
    def __init__(self):
        self.num_functions_marked = len(_functions_to_profile)

        if line_profiler is None:
            self.line_prof = None
        else:
            self.line_prof = line_profiler.LineProfiler()

            for f in _functions_to_profile:
                self.line_prof.add_function(f)
Ejemplo n.º 18
0
def line_profile():
    sampledata = flattened.data[:, :, 0, 0]

    import line_profiler
    profile = line_profiler.LineProfiler(nangeomedian_axis_one)
    # profile.add_function(nangeomedian_axis_one)
    # profile.runcall(nangeomedian_axis_one, sampledata)
    profile.run('simple_reduce()')

    profile.print_stats()
Ejemplo n.º 19
0
        def wrapper(*args, **kwargs):
            profiler = line_profiler.LineProfiler()
            profiler.add_function(func)
            profiler.enable()

            result = func(*args, **kwargs)

            profiler.print_stats()

            return result
Ejemplo n.º 20
0
def _enable_profiler(line_by_line=False):  # pragma: no cover
    if 'profile' in builtins.__dict__:
        return builtins.__dict__['profile']
    if line_by_line:
        import line_profiler
        prof = line_profiler.LineProfiler()
    else:
        prof = ContextualProfile()
    builtins.__dict__['profile'] = prof
    return prof
Ejemplo n.º 21
0
    def handle(self):
        if conf.MEASUREMENT_MODE_PROCESSOR:

            utilities.clean_folder(conf.PROF_FOLDER)

            if conf.PROFILER == 'cProfiler':
                import cProfile
                import pstats
                pr = cProfile.Profile()
                pr.enable()
                self.handle_clean()
                pr.disable()

                #pr.dump_stats(conf.PROF_FILE_PROCESSOR + "pickle") #pickled

                #readable
                sortby = 'cumulative'
                ps = pstats.Stats(pr,
                                  stream=open(
                                      conf.PROF_FILE_PROCESSOR + "prof",
                                      'w')).sort_stats(sortby)
                ps.print_stats()

            elif conf.PROFILER == 'LineProfiler':
                import line_profiler
                import pstats
                import io
                pr = line_profiler.LineProfiler(
                    self.handle_clean, get_sketches_from_relays_non_blocking,
                    Classes.CountSketchCt.aggregate, op.median_operation,
                    op.mean_operation, op.variance_operation)
                pr.enable()
                self.handle_clean()
                pr.disable()

                pr.print_stats(open(conf.PROF_FILE_PROCESSOR + "prof",
                                    'w'))  #readable
                #pr.dump_stats(conf.PROF_FILE_PROCESSOR + "pickle") #pickled

            elif conf.PROFILER == "viz":
                from pycallgraph import PyCallGraph
                from pycallgraph.output import GraphvizOutput
                from pycallgraph import Config

                config = Config(max_depth=conf.DEPTH)
                graphviz = GraphvizOutput()
                graphviz.output_file = conf.PROF_FILE_PROCESSOR + 'png'
                with PyCallGraph(output=graphviz, config=config):
                    self.handle_clean()

            else:
                self.handle_clean()

        else:
            self.handle_clean()
Ejemplo n.º 22
0
Archivo: bridge.py Proyecto: hx2A/py5
    def __init__(self, sketch):
        self._sketch = sketch
        self._functions = dict()
        self._function_param_counts = dict()
        self._pre_hooks = defaultdict(dict)
        self._post_hooks = defaultdict(dict)
        self._profiler = line_profiler.LineProfiler()
        self._is_terminated = False

        from .java_conversion import convert_to_python_types
        self._convert_to_python_types = convert_to_python_types
Ejemplo n.º 23
0
    def run_command(self, settings=None, loop=None):
        if loop is not None:
            self.loop = loop
        if settings is None:
            settings = get_settings(self.arguments.configuration,
                                    self.arguments.override)
        if settings.get("loop_policy"):
            loop_policy = resolve_dotted_name(settings["loop_policy"])
            asyncio.set_event_loop_policy(loop_policy())

        app = self.make_app(settings)

        if self.arguments.line_profiler:
            if not HAS_LINE_PROFILER:
                sys.stderr.write(
                    "You must first install line_profiler for the --line-profiler option to work.\n"
                    "Use `pip install line_profiler` to install line_profiler.\n"
                )
                return 1
            self.line_profiler = line_profiler.LineProfiler()
            for func in profile.get_profilable_functions():
                if fnmatch(get_dotted_name(func),
                           self.arguments.line_profiler_matcher or "*"):
                    self.line_profiler.add_function(func)
            self.line_profiler.enable_by_count()

        run_func = self.__run
        if self.arguments.monitor:
            if not HAS_AIOMONITOR:
                sys.stderr.write(
                    "You must install aiomonitor for the "
                    "--monitor option to work.\n"
                    "Use `pip install aiomonitor` to install aiomonitor.\n")
                return 1
            run_func = self.__run_with_monitor

        if self.arguments.profile:
            self.profiler = cProfile.Profile()
            self.profiler.runcall(run_func, app, settings)
            if self.arguments.profile_output:
                self.profiler.dump_stats(self.arguments.profile_output)
            else:
                # dump to screen
                self.profiler.print_stats(-1)
        else:
            run_func(app, settings)

        if self.line_profiler is not None:
            self.line_profiler.disable_by_count()
            if self.arguments.line_profiler_output:
                self.line_profiler.dump_stats(
                    self.arguments.line_profiler_output)
            else:
                self.line_profiler.print_stats()
Ejemplo n.º 24
0
    def decorated_func(*args, **kwargs):

        import line_profiler
        profile = line_profiler.LineProfiler()

        f = profile(func)
        return_value = f(*args, **kwargs)

        profile.print_stats()

        return return_value
Ejemplo n.º 25
0
def profile_analyse_image():
    """
    Profile the analysis function
    """

    path = "../data/Real_Application/"

    model = load_model(path="./state_dicts/custom_cnn_e4_0.pt", tracing=True)
    lp = line_profiler.LineProfiler()
    lp_wrapper = lp(analyse_image)
    lp_wrapper(path, model)
    lp.print_stats()
Ejemplo n.º 26
0
def profile_function_np(number_of_points):
    points = np.zeros((number_of_points, 2), dtype=np.float64)
    for i in range(number_of_points):
        points[i] = np.random.uniform(-10, 10), np.random.uniform(-10, 10)

    profile = line_profiler.LineProfiler(convex_hull_np_array)
    profile.runcall(convex_hull_np_array, points)
    now = datetime.now()
    file = open(
        "../cython_profiler_np_" + now.strftime("%Y-%m-%d_%H-%M-%S") + ".txt",
        'w+')
    profile.print_stats(file)
    file.close()
Ejemplo n.º 27
0
def run_worker(infile, master, work_id=None):
    worker = Worker(infile,
                    master,
                    work_id=work_id,
                    process_line=proc_dur_count)
    prof = line_profiler.LineProfiler(
        worker.run, worker.upload_data, worker.find_skyline_diff,
        worker.get_master_updates, worker.expire_points, worker.update_skyline,
        worker.sky.update_sky_for_point, worker.sky.check_dominated)
    prof.enable()
    worker.run()
    prof.disable()
    prof.dump_stats("worker.prof")
Ejemplo n.º 28
0
def line_profile_func(func, args):
    '''
    Print time to run each line.
    *** There are issues after reloading code.

    Import function (with preceding module name, without trailing parentheses),
    its arguments (all within ()).
    '''
    import line_profiler
    reload(line_profiler)
    ppp = line_profiler.LineProfiler(func)
    ppp.runcall(func, *args)
    ppp.print_stats()
Ejemplo n.º 29
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        if getattr(settings, 'PAUSE_PROFILING', False):
            return None

        codes = inspect.getmodule(view_func).__dict__.values()
        request.profiler = line_profiler.LineProfiler()
        for code in codes:
            try:
                request.profiler.add_module(inspect.getmodule(code))
                request.mem_profiler.add_function(code)
            except AttributeError:
                continue
        request.profiler.enable_by_count()
Ejemplo n.º 30
0
def profile_onthefly(func):
    import line_profiler
    profile = line_profiler.LineProfiler()
    new_func = profile(func)
    new_func.profile_info = KernprofParser(profile)
    new_func.print_report = new_func.profile_info.print_report

    def wraper(*args, **kwargs):
        retval = new_func(*args, **kwargs)
        new_func.print_report()
        return retval

    return wraper