コード例 #1
0
 def decorator(*args, **kwargs):
     if should_profile and not should_profile():
         return func(*args, **kwargs)
     result = None
     if cumulative:
         global profiler
     else:
         profiler = Profile()
     try:
         result = profiler.runcall(func, *args, **kwargs)
     finally:
         if lock.acquire(False):
             if dump_stats:
                 profiler.dump_stats(profile_filename)
             if callgrind_filename:
                 stats = pstats.Stats(profiler)
                 conv = pyprof2calltree.CalltreeConverter(stats)
                 with open(callgrind_filename, 'w') as fd:
                     conv.output(fd)
                 if print_stats:
                     stats.strip_dirs().sort_stats(
                         sort_stats).print_stats(print_stats)
             lock.release()
             return result
     return result
コード例 #2
0
class Monitor:

    def __init__(self, monitoring_dir: str,
                 with_runtime: bool = False,
                 with_profiler: bool = False):
        self._monitoring_dir = monitoring_dir
        self._duration_sampler = _new_duration_sampler(monitoring_dir)
        self._runtime_bucket = None
        self._profiler = None
        self._lock = Lock()

        if with_runtime:
            self._runtime_bucket = _start_runtime_sampler(monitoring_dir)

        if with_profiler:
            self._profiler = Profile()
            self._profiler.enable()

    def start_duration_sample(self) -> str:
        return self._duration_sampler.sample()

    def stop_duration_sample(self, label: str, sample_id: str):
        self._duration_sampler.collect(label, sample_id)

    def stop(self):
        if self._profiler:
            with self._lock:
                self._profiler.disable()
                outfile = _profiler_file_pathname(self._monitoring_dir)
                self._profiler.dump_stats(outfile)

        self._duration_sampler.bucket().empty()
        if self._runtime_bucket:
            self._runtime_bucket.empty()
コード例 #3
0
def make_melody():
    valence = request.args.get("valence", default=1, type=float)
    arousal = request.args.get("arousal", default=1, type=float)

    if environ.get("PROFILE", False):
        print("~~ profiling ~~")
        pr = Profile()
        pr.enable()

    mg = MelodyGenerator(valence, arousal)
    m = mg.gen_melody()

    if environ.get("PROFILE", False):
        pr.disable()
        print("~~ end profile ~~")
        pr.dump_stats("profile.perf")

    # TODO: move this where appropriate maybe
    meta = m21.metadata.Metadata()
    meta.title = "Mood of the day"  # TODO: maybe we can generate random titles
    meta.composer = "Meldy"
    meta.date = date.today().strftime("%Y/%m/%d")
    m.metadata = meta

    with NamedTemporaryFile() as t:
        m.write("musicxml", fp=t.name)
        t.seek(0)
        return Response(t.read(),
                        mimetype="application/vnd.recordare.musicxml")
コード例 #4
0
def _exec_main(parser, values):
    sconsflags = os.environ.get('SCONSFLAGS', '')
    all_args = string.split(sconsflags) + sys.argv[1:]

    options, args = parser.parse_args(all_args, values)

    if type(options.debug) == type([]) and "pdb" in options.debug:
        import pdb
        pdb.Pdb().runcall(_main, parser)
    elif options.profile_file:
        try:
            from cProfile import Profile
        except ImportError, e:
            from profile import Profile

        # Some versions of Python 2.4 shipped a profiler that had the
        # wrong 'c_exception' entry in its dispatch table.  Make sure
        # we have the right one.  (This may put an unnecessary entry
        # in the table in earlier versions of Python, but its presence
        # shouldn't hurt anything).
        try:
            dispatch = Profile.dispatch
        except AttributeError:
            pass
        else:
            dispatch['c_exception'] = Profile.trace_dispatch_return

        prof = Profile()
        try:
            prof.runcall(_main, parser)
        except SConsPrintHelpException, e:
            prof.dump_stats(options.profile_file)
            raise e
コード例 #5
0
ファイル: util.py プロジェクト: lsst/pipe_supertask
def profile(filename, log=None):
    """!Context manager for profiling with cProfile

    @param filename     filename to which to write profile (profiling disabled if None or empty)
    @param log          log object for logging the profile operations

    If profiling is enabled, the context manager returns the cProfile.Profile object (otherwise
    it returns None), which allows additional control over profiling.  You can obtain this using
    the "as" clause, e.g.:

        with profile(filename) as prof:
            runYourCodeHere()

    The output cumulative profile can be printed with a command-line like:

        python -c 'import pstats; pstats.Stats("<filename>").sort_stats("cumtime").print_stats(30)'
    """
    if not filename:
        # Nothing to do
        yield
        return
    from cProfile import Profile

    prof = Profile()
    if log is not None:
        log.info("Enabling cProfile profiling")
    prof.enable()
    yield prof
    prof.disable()
    prof.dump_stats(filename)
    if log is not None:
        log.info("cProfile stats written to %s" % filename)
コード例 #6
0
ファイル: workers.py プロジェクト: diffeo/coordinate
 def _run_unit(self, unit, set_title=False):
     try:
         if set_title:
             setproctitle('coordinate worker {0!r} {1!r}'
                          .format(unit.work_spec_name, unit.key))
         profiler = None
         if ((self.profile_destination and
              unit.work_spec_name in self.profile_work_specs)):
             now = datetime.now()
             unit_info = {
                 'work_spec_name': unit.work_spec_name,
                 'work_unit_key': unit.key,
                 'ymd': now.strftime('%Y%m%d'),
                 'hms': now.strftime('%H%M%S'),
             }
             destination = self.profile_destination % unit_info
             profiler = Profile()
             profiler.enable()
         unit.run()
         if profiler:
             profiler.disable()
             profiler.dump_stats(destination)
         unit.finish()
     except LostLease:
         # We don't own the unit any more so don't try to report on it
         logger.warn('Lost Lease on %r %r', unit.work_spec_name, unit.key)
     except Exception, exc:  # pylint: disable=broad-except
         unit.fail(exc)
コード例 #7
0
    def profiled_fn(*args, **kwargs):
        filename = fn.__name__ + '.profile'

        prof = Profiler()
        ret = prof.runcall(fn, *args, **kwargs)
        prof.dump_stats(filename)
        return ret
コード例 #8
0
ファイル: Profiling.py プロジェクト: YongYang86/proteus
    def profile_function(self, func, args, kwargs, profile_name):
        """
        Profile a Proteus function using the call: func(*args, **kwargs)

        Returns the output of the function call.
        """

        comm = self.comm
        if type(comm) is None:
            raise ValueError(
                "The Dispatcher does not have a valid Comm object")

        prof = Profile()
        func_return = prof.runcall(func, *args, **kwargs)
        profile_rank_name = profile_name + str(comm.rank())
        stripped_profile_name = profile_name + '_c' + str(comm.rank())

        prof.dump_stats(profile_rank_name)
        comm.barrier()  #ensure files are ready for master
        if comm.isMaster():
            import copy
            import StringIO
            profilingLog = StringIO.StringIO()
            stats = pstats.Stats(profile_rank_name, stream=profilingLog)
            stats.__dict__['files'] = [
                'Maximum times across MPI tasks for',
                stats.__dict__['files'][0]
            ]
            statsm = stats.stats
            for i in range(1, comm.size()):
                pstatsi = pstats.Stats(profile_name + str(i))
                statsi = pstatsi.stats
                stats.__dict__['files'].append(pstatsi.__dict__['files'][0])
                for f, c in statsi.iteritems():
                    if f in statsm:
                        if c[2] > statsm[f][2]:
                            statsm[f] = c
                    else:
                        statsm[f] = c
            stats.sort_stats('cumulative')
            stats.print_stats(30)
            stats.sort_stats('time')
            stats.print_stats(30)
            logEvent(profilingLog.getvalue())
            msg = r"""
Wall clock percentage of top 20 calls
-------------------------------------
"""
            total = 0.0
            for f in stats.__dict__['fcn_list'][0:20]:
                if f[0] == '~':
                    fname = f[-1].strip("<").strip(">")
                else:
                    fname = "function '{2:s}' at {0:s}:{1:d}".format(*f)
                msg += ("{0:11.1%} {1:s}\n".format(
                    statsm[f][2] / stats.__dict__['total_tt'], str(fname)))
                total += statsm[f][2] / stats.__dict__['total_tt']
            logEvent(msg)
            logEvent("Representing " + ` total * 100. ` + "%")
        return func_return
コード例 #9
0
    def handle(self, *args, **options):
        """
        Main function, called by django.
        """

        if options['upis']:
            options['upis'] = options['upis'].split(',')
        elif options['upi_file']:
            with open(options['upi_file'], 'rb') as raw:
                options['upis'] = [line.strip() for line in raw]
        else:
            if not options['min'] and options['min'] != 0:
                raise CommandError('Please specify --min')

            if not options['max']:
                raise CommandError('Please specify --max')

            options['min'] = int(options['min'])
            options['max'] = int(options['max'])

        if not options['date']:
            options['date'] = date.today()
        else:
            options['date'] = dt.strptime(options['date'], '%Y-%m-%d')

        if options['profile']:
            profiler = Profile()
            profiler.runcall(self.run, options)
            profiler.print_stats()
            profiler.dump_stats('profile.txt')
        else:
            self.run(options)
コード例 #10
0
    def profile_function(self, func, args, kwargs, profile_name):
        """
        Profile a Proteus function using the call: func(*args, **kwargs)

        Returns the output of the function call.
        """

        comm = self.comm
        if type(comm) is None:
            raise ValueError("The Dispatcher does not have a valid Comm object")

        prof = Profile()
        func_return = prof.runcall(func, *args, **kwargs)
        profile_rank_name = profile_name + str(comm.rank())
        stripped_profile_name = profile_name + '_c' + str(comm.rank())

        prof.dump_stats(profile_rank_name)
        comm.beginSequential()
        stats = pstats.Stats(profile_rank_name)
        stats.strip_dirs()
        stats.dump_stats(stripped_profile_name)
        stats.sort_stats('cumulative')
        if verbose and comm.isMaster():
            stats.print_stats(30)
            stats.sort_stats('time')
            if verbose and comm.isMaster():
                stats.print_stats(30)
        comm.endSequential()

        return func_return
コード例 #11
0
    def handle(self, **options):
        """Django entry point."""
        def validate_options():
            """Validate command line options."""
            if not options['destination']:
                raise CommandError('Please specify the --destination option')

            if not options['format']:
                raise CommandError('Please specify the --format option')

            if options['format'] in ['bed', 'all'] and not options['bedToBigBed']:
                raise CommandError('Please specify the --bedToBigBed option')

            if options['format'] not in self.formats:
                raise CommandError('Please choose a valid output format (%s)' % '|'.join(self.formats))

        validate_options()

        if options['profile']:
            profiler = Profile()
            profiler.runcall(self.export, **options)
            profiler.print_stats()
            profiler.dump_stats('profile.txt')
        elif options['format'] == 'all':
            self.export_all(**options)
        else:
            self.export(**options)
コード例 #12
0
ファイル: Main.py プロジェクト: cournape/numscons
def _exec_main(parser, values):
    sconsflags = os.environ.get('SCONSFLAGS', '')
    all_args = string.split(sconsflags) + sys.argv[1:]

    options, args = parser.parse_args(all_args, values)

    if type(options.debug) == type([]) and "pdb" in options.debug:
        import pdb
        pdb.Pdb().runcall(_main, parser)
    elif options.profile_file:
        try:
            from cProfile import Profile
        except ImportError, e:
            from profile import Profile

        # Some versions of Python 2.4 shipped a profiler that had the
        # wrong 'c_exception' entry in its dispatch table.  Make sure
        # we have the right one.  (This may put an unnecessary entry
        # in the table in earlier versions of Python, but its presence
        # shouldn't hurt anything).
        try:
            dispatch = Profile.dispatch
        except AttributeError:
            pass
        else:
            dispatch['c_exception'] = Profile.trace_dispatch_return

        prof = Profile()
        try:
            prof.runcall(_main, parser)
        except SConsPrintHelpException, e:
            prof.dump_stats(options.profile_file)
            raise e
コード例 #13
0
ファイル: parse_gtfs.py プロジェクト: hasadna/OpenTrain2
 def handle(self, *args, **options):
     if options['profile']:
         profiler = Profile()
         profiler.runcall(self._handle, *args, **options)
         profiler.dump_stats('parse_gtfs.prof')
     else:
         self._handle(*args, **options)
コード例 #14
0
def main(args):
    SEED = 12345

    torch.manual_seed(SEED)
    torch.cuda.manual_seed(SEED)
    np.random.seed(SEED)
    
    model = models.vgg16(pretrained=True)
    model.classifier[0] = nn.Linear(8*8*512, 4096)
    model.classifier[6] = nn.Linear(4096, 27)
    model.load_state_dict(torch.load("model.pth"))
    print("total params:{}".format(count_params(model)))    

    dataset_test = MyDataset(args.pickle_path, args.test_list_path, args.test_dir)
    test_loader = DataLoader(dataset_test, batch_size=args.batch_size, shuffle=False)
    
    device = torch.device("cpu" if args.no_cuda else "cuda:0")
    model = model.to(device)
    
    model.eval()

    if not args.no_prof:
        pr = Profile()
        pr.runcall(bench, model, test_loader, device)
        pr.dump_stats("profile.txt")

        ps = pstats.Stats("profile.txt")
        ps.sort_stats('time').print_stats(15)
コード例 #15
0
def stats_for_fib(type_, fib):
    p = Profile()
    p.runcall(fib, 30)
    p.dump_stats(type_.lower().replace(' ', '_') + '.stats')
    s = Stats(p)
    s.strip_dirs().sort_stats('time', 'cumulative')
    print_stats(type_, s)
コード例 #16
0
ファイル: import_games.py プロジェクト: haweave/bbsystems
 def handle(self, *args, **options):
     if options["profile"]:
         profiler = Profile()
         profiler.runcall(self._handle, *args, **options)
         profiler.dump_stats("import_games.profile")
     else:
         self._handle(*args, **options)
コード例 #17
0
    def profile_function(self, func, args, kwargs, profile_name):
        """
        Profile a Proteus function using the call: func(*args, **kwargs)

        Returns the output of the function call.
        """

        comm = self.comm
        if type(comm) is None:
            raise ValueError(
                "The Dispatcher does not have a valid Comm object")

        prof = Profile()
        func_return = prof.runcall(func, *args, **kwargs)
        profile_rank_name = profile_name + str(comm.rank())
        stripped_profile_name = profile_name + '_c' + str(comm.rank())

        prof.dump_stats(profile_rank_name)
        comm.beginSequential()
        stats = pstats.Stats(profile_rank_name)
        stats.strip_dirs()
        stats.dump_stats(stripped_profile_name)
        stats.sort_stats('cumulative')
        if verbose and comm.isMaster():
            stats.print_stats(30)
            stats.sort_stats('time')
            if verbose and comm.isMaster():
                stats.print_stats(30)
        comm.endSequential()

        return func_return
コード例 #18
0
def profile(filename, log=None):
    """!Context manager for profiling with cProfile

    @param filename     filename to which to write profile (profiling disabled if None or empty)
    @param log          log object for logging the profile operations

    If profiling is enabled, the context manager returns the cProfile.Profile object (otherwise
    it returns None), which allows additional control over profiling.  You can obtain this using
    the "as" clause, e.g.:

        with profile(filename) as prof:
            runYourCodeHere()

    The output cumulative profile can be printed with a command-line like:

        python -c 'import pstats; pstats.Stats("<filename>").sort_stats("cumtime").print_stats(30)'
    """
    if not filename:
        # Nothing to do
        yield
        return
    from cProfile import Profile
    profile = Profile()
    if log is not None:
        log.info("Enabling cProfile profiling")
    profile.enable()
    yield profile
    profile.disable()
    profile.dump_stats(filename)
    if log is not None:
        log.info("cProfile stats written to %s" % filename)
コード例 #19
0
 def intprofiled(*n, **kw):
     prof = Profile(timer=timer)
     a = prof.runcall(eff, *n, **kw)
     TRACE('Dumping profile stats to: %r' % (outputfile,))
     prof.dump_stats(outputfile)
     TRACE('Profile run: %d' % timer())
     return a
コード例 #20
0
    def handle(self, **options):
        """
        Django entry point.
        """
        def validate_options():
            """
            Validate command line options.
            """
            if not options['destination']:
                raise CommandError('Please specify the --destination option')

            if not options['format']:
                raise CommandError('Please specify the --format option')

            if options['format'] in ['bed', 'all'] and not options['bedToBigBed']:
                raise CommandError('Please specify the --bedToBigBed option')

            if options['format'] not in self.formats:
                raise CommandError('Please choose a valid output format (%s)' % '|'.join(self.formats))

        validate_options()

        if options['profile']:
            profiler = Profile()
            profiler.runcall(self.export, **options)
            profiler.print_stats()
            profiler.dump_stats('profile.txt')            
        elif options['format'] == 'all':
            self.export_all(**options)
        else:
            self.export(**options)
コード例 #21
0
ファイル: homeserver.py プロジェクト: yvwvnacb/synapse
 def profiled(*args, **kargs):
     profile = Profile()
     profile.enable()
     func(*args, **kargs)
     profile.disable()
     ident = current_thread().ident
     profile.dump_stats("/tmp/%s.%s.%i.pstat" %
                        (hs.hostname, func.__name__, ident))
コード例 #22
0
 def intprofiled(*n, **kw):
     import arch
     prof = Profile(timer=arch.startup.get_rss)
     a = prof.runcall(eff, *n, **kw)
     TRACE('Dumping stats to: %r' % (outputfile,))
     prof.dump_stats(outputfile)
     TRACE('Final RSS = %d' % arch.startup.get_rss())
     return a
コード例 #23
0
 def thenewfunc(*n, **kw):
     if not hasattr(sys, 'getmallocbytecount'):
         raise RuntimeError('Not a memory debug version of Python!')
     prof = Profile(timer=sys.getmallocbytecount)
     a = prof.runcall(eff, *n, **kw)
     prof.dump_stats(outputfile)
     TRACE('Allocated %d bytes' % sys.getmallocbytecount())
     return a
コード例 #24
0
    def handle(self, *args, **options):

        if options['profile_file']:
            profiler = Profile()
            profiler.runcall(self._handle, *args, **options)
            profiler.dump_stats(options['profile_file'])
        else:
            self._handle(*args, **options)
コード例 #25
0
ファイル: ingest_push.py プロジェクト: EricRahm/treeherder
    def handle(self, *args, **options):

        if options['profile_file']:
            profiler = Profile()
            profiler.runcall(self._handle, *args, **options)
            profiler.dump_stats(options['profile_file'])
        else:
            self._handle(*args, **options)
コード例 #26
0
ファイル: profiling.py プロジェクト: dniku/mu-rainbow
def profile(func, stats_path):
    profiler = Profile()
    try:
        profiler.runcall(func)
    except KeyboardInterrupt:
        profiler.dump_stats(stats_path)
        s = pstats.Stats(stats_path)
        s.strip_dirs().sort_stats("time").print_stats(30)
        raise
コード例 #27
0
 def intprofiled(*n, **kw):
     import arch
     timer = arch.util.get_cpu_timer()
     prof = Profile(timer=timer)
     a = prof.runcall(eff, *n, **kw)
     TRACE('Dumping stats to: %r' % (outputfile,))
     prof.dump_stats(outputfile)
     TRACE('Total CPU time (maybe in jiffies) %.4f' % timer(close=True))
     return a
コード例 #28
0
def stats_for_main():
    p = Profile()
    p.runcall(main)
    p.dump_stats('main.stats')
    s = Stats(p)
    s.strip_dirs().sort_stats('time', 'cumulative')
    print_stats('MAIN - ALL STATS', s)
    print_stats('MAIN - CALLERS', s, 'sleep')
    print_stats('MAIN - CALLEES', s, 'heavy')
コード例 #29
0
ファイル: profiling.py プロジェクト: pombredanne/mu-rainbow
def profile(func, stats_path):
    profiler = Profile()
    try:
        profiler.runcall(func)
    except KeyboardInterrupt:
        profiler.dump_stats(stats_path)
        s = pstats.Stats(stats_path)
        s.strip_dirs().sort_stats("time").print_stats(30)
        raise
コード例 #30
0
ファイル: test_export.py プロジェクト: vituocgia/izi
    def test_xid_perfs(self):
        for i in range(10000):
            self.make(i)
        self.model.invalidate_cache()
        records = self.model.search([])

        p = Profile()
        p.runcall(records._export_rows, [['id'], ['value']])
        p.dump_stats('xid_perfs.pstats')
コード例 #31
0
ファイル: test_export.py プロジェクト: zhiqinghuang/odoo
    def test_xid_perfs(self):
        for i in range(10000):
            self.make(i)
        self.model.invalidate_cache()
        records = self.model.search([])

        p = Profile()
        p.runcall(records._export_rows, [['id'], ['value']])
        p.dump_stats('xid_perfs.pstats')
コード例 #32
0
ファイル: homeserver.py プロジェクト: JigmeDatse/synapse
 def profiled(*args, **kargs):
     profile = Profile()
     profile.enable()
     func(*args, **kargs)
     profile.disable()
     ident = current_thread().ident
     profile.dump_stats("/tmp/%s.%s.%i.pstat" % (
         hs.hostname, func.__name__, ident
     ))
コード例 #33
0
    def profile_function(self, func, args, kwargs, profile_name):
        """
        Profile a Proteus function using the call: func(*args, **kwargs)

        Returns the output of the function call.
        """

        comm = self.comm
        if type(comm) is None:
            raise ValueError("The Dispatcher does not have a valid Comm object")

        prof = Profile()
        func_return = prof.runcall(func, *args, **kwargs)
        profile_rank_name = profile_name + str(comm.rank())
        stripped_profile_name = profile_name + '_c' + str(comm.rank())

        prof.dump_stats(profile_rank_name)
        comm.barrier()#ensure files are ready for master
        if comm.isMaster():
            import copy
            import StringIO
            profilingLog = StringIO.StringIO()
            stats = pstats.Stats(profile_rank_name, stream=profilingLog)
            stats.__dict__['files']=['Maximum times across MPI tasks for',
                                     stats.__dict__['files'][0]]
            statsm = stats.stats
            for i in range(1,comm.size()):
                pstatsi = pstats.Stats(profile_name+str(i))
                statsi = pstatsi.stats
                stats.__dict__['files'].append(pstatsi.__dict__['files'][0])
                for f,c in statsi.iteritems():
                    if f in statsm:
                        if c[2] > statsm[f][2]:
                            statsm[f] = c
                    else:
                        statsm[f] = c
            stats.sort_stats('cumulative')
            stats.print_stats(30)
            stats.sort_stats('time')
            stats.print_stats(30)
            logEvent(profilingLog.getvalue())
            msg = r"""
Wall clock percentage of top 20 calls
-------------------------------------
"""
            total=0.0
            for f in stats.__dict__['fcn_list'][0:20]:
                if f[0] == '~':
                    fname=f[-1].strip("<").strip(">")
                else:
                    fname="function '{2:s}' at {0:s}:{1:d}".format(*f)
                msg+=("{0:11.1%} {1:s}\n".format(statsm[f][2]/stats.__dict__['total_tt'],str(fname)))
                total += statsm[f][2]/stats.__dict__['total_tt']
            logEvent(msg)
            logEvent("Representing "+`total*100.`+"%")
        return func_return
コード例 #34
0
ファイル: testPerformance.py プロジェクト: poses/erp5
 def profile(self, func, suffix=''):
     from cProfile import Profile
     prof_file = '%s%s' % (func.__name__, suffix)
     try:
         os.unlink(prof_file)
     except OSError:
         pass
     prof = Profile()
     prof.runcall(func)
     prof.dump_stats(prof_file)
コード例 #35
0
def run_with_profile():
    profile = Profile()
    profile.enable()
    yield profile
    profile.disable()
    with NamedTemporaryFile() as statsfile:
        profile.dump_stats(statsfile.name)
        statsfile.flush()
        statsfile.seek(0)
        profile.stats = base64.b64encode(statsfile.read()).decode()
コード例 #36
0
def profile_call(_func, *args, **kwargs):
    p = Profile()
    rv = []
    p.runcall(lambda: rv.append(_func(*args, **kwargs)))
    p.dump_stats(f"/tmp/sentry-{time.time()}-{_func.__name__}.prof")

    stats = Stats(p, stream=sys.stderr)
    stats.sort_stats("time", "calls")
    stats.print_stats()
    return rv[0]
コード例 #37
0
def profile_call(_func, *args, **kwargs):
    p = Profile()
    rv = []
    p.runcall(lambda: rv.append(_func(*args, **kwargs)))
    p.dump_stats('/tmp/sentry-%s-%s.prof' % (time.time(), _func.__name__))

    stats = Stats(p, stream=sys.stderr)
    stats.sort_stats('time', 'calls')
    stats.print_stats()
    return rv[0]
コード例 #38
0
 def profile(self, func, suffix=''):
     from cProfile import Profile
     prof_file = '%s%s' % (func.__name__, suffix)
     try:
         os.unlink(prof_file)
     except OSError:
         pass
     prof = Profile()
     prof.runcall(func)
     prof.dump_stats(prof_file)
コード例 #39
0
 def wrapper(*args, **kwargs):
     profiler = Profile()
     result = profiler.runcall(func, *args, **kwargs)
     if print_data:
         profiler.print_stats()
     filename = func.__name__
     profiler.dump_stats(filename)
     if visualized:
         visualize(filename, host, port)
     return result
コード例 #40
0
 def wrapped(*args, **kwargs):
     if param:
         pr = Profile()
         pr.enable()
         result = func(*args, **kwargs)
         pr.disable()
         pr.dump_stats(func.__name__ + ".cprofile")
     else:
         result = func(*args, **kwargs)
     return result
コード例 #41
0
ファイル: add_profiler.py プロジェクト: mojaves/ovirt-tools
 def profiled_execution(*args, **kwargs):
     logging.info('profiling method %s' % func.__name__)
     profiler = Profile()
     ret = profiler.runcall(func, *args, **kwargs)
     prof_file = NamedTemporaryFile(mode='w',
                                    prefix=func.__name__,
                                    delete=False)
     profiler.dump_stats(prof_file.name)
     logging.info('profiled method %s and dumped results to %s' % (
                  func.__name__, prof_file.name))
     return ret
コード例 #42
0
def main():
    from ..logging.base import setup_context as setup_logging_context
    from ..logging.base import teardown as teardown_logging

    opts = None
    pr = None

    debug = False
    profile = False

    try:
        setup_logging_context()

        from ..utils.pickle import patch_nipype_unpickler

        patch_nipype_unpickler()

        from .parser import parse_args

        opts, should_run = parse_args()

        debug = opts.debug
        profile = opts.profile

        if profile is True:
            from cProfile import Profile

            pr = Profile()
            pr.enable()

        run(opts, should_run)
    except Exception as e:
        logger.exception("Exception: %s", e, exc_info=True)

        if debug:
            import pdb

            pdb.post_mortem()
    finally:
        if profile and pr is not None:
            pr.disable()
            if opts is not None:
                pr.dump_stats(
                    Path(opts.workdir) /
                    f"profile.{format_current_time():s}.prof")

        teardown_logging()

        # clean up orphan processes

        from ..utils.multiprocessing import terminate

        terminate()
コード例 #43
0
ファイル: Profiler.py プロジェクト: Iotic-Labs/py-IoticAgent
 def wrapper(*args, **kwargs):
     profile = Profile()
     profile.enable()
     try:
         func(*args, **kwargs)
     finally:
         profile.disable()
     try:
         thread = current_thread()
         profile.dump_stats('profile_%s.%s.%s.log' % (getpid(), thread.name, thread.ident))
     except:
         logger.exception('Failed to dump stats')
コード例 #44
0
ファイル: main.py プロジェクト: Yobmod/dmlmung
def prof_to_stats(profile: cProfile.Profile) -> None:
    """."""
    import pstats
    import datetime

    now = datetime.datetime.now()
    stats = pstats.Stats(profile)
    stats.strip_dirs()
    stats.sort_stats('cumulative')
    stats.print_stats(15)
    filename = f'./logs/profile_{now.day}-{now.month}-{now.year}.prof'
    profile.dump_stats(filename)
コード例 #45
0
 def _save_profile(self, profile: cProfile.Profile, sync: str) -> None:
     """
     Dump the profiler data to file in the current working directory.
     """
     if profile:
         profile_name = "{}/cProfile-{}-{}-{}.pstats".format(
             os.getcwd(), self.__class__.__name__, self._parallel_index,
             sync)
         print("Dumping profile data to {}".format(profile_name))
         profile.dump_stats(profile_name)
     else:
         print("No profile generated.")
コード例 #46
0
ファイル: Profiler.py プロジェクト: aniknarayan/ioticiser_new
 def wrapper(*args, **kwargs):
     profile = Profile()
     profile.enable()
     try:
         func(*args, **kwargs)
     finally:
         profile.disable()
     try:
         thread = current_thread()
         profile.dump_stats('profile_%s.%s.%s.log' %
                            (getpid(), thread.name, thread.ident))
     except:
         logger.exception('Failed to dump stats')
コード例 #47
0
ファイル: decorator.py プロジェクト: GlobalSquare/dispersy
    def helper(*args, **kargs):
        filename = "profile-%s-%d.out" % (current_thread().name, get_ident())
        if filename in profiled_threads:
            raise RuntimeError("Can not attach profiler on the same thread twice")

        dprint("running with profiler [", filename, "]")
        profiled_threads.add(filename)
        profiler = Profile()

        try:
            return profiler.runcall(func, *args, **kargs)
        finally:
            dprint("profiler results [", filename, "]")
            profiler.dump_stats(filename)
コード例 #48
0
def runAndTimeSRM(srm, srm_type, make_plot=False):
    print("Testing {} SRM model!".format(srm_type))

    prof = Profile()

    prof.enable()
    P, T, A, x = burnSRM(srm, srm_type, make_plot)
    prof.disable()

    t = delta_t * np.arange(P.size)

    prof.dump_stats('{}_srm.stats'.format(srm_type))

    return P, T, A, x, t
コード例 #49
0
ファイル: utils.py プロジェクト: rmoorman/lektor
def profile_func(func):
    from cProfile import Profile
    from pstats import Stats

    p = Profile()
    rv = []
    p.runcall(lambda: rv.append(func()))
    p.dump_stats('/tmp/lektor-%s.prof' % func.__name__)

    stats = Stats(p, stream=sys.stderr)
    stats.sort_stats('time', 'calls')
    stats.print_stats()

    return rv[0]
コード例 #50
0
ファイル: utils.py プロジェクト: Meresnes/meresnes.github.io
def profile_func(func):
    from cProfile import Profile
    from pstats import Stats

    p = Profile()
    rv = []
    p.runcall(lambda: rv.append(func()))
    p.dump_stats('/tmp/lektor-%s.prof' % func.__name__)

    stats = Stats(p, stream=sys.stderr)
    stats.sort_stats('time', 'calls')
    stats.print_stats()

    return rv[0]
コード例 #51
0
ファイル: main.py プロジェクト: SapphireDensetsu/enough
def profile_call(func, *args, **kw):
    from cProfile import Profile
    p = Profile() #clock)
    print "Profiling to", profile_filename
    p.runcall(func, *args, **kw)

    from pytools import lsprofcalltree
    k = lsprofcalltree.KCacheGrind(p)
    pdump_filename = profile_filename+'.pstats'
    p.dump_stats(pdump_filename)
    print "Profile dumped to", pdump_filename
    kcache_grind_filename = profile_filename+'.kcachegrind'
    with open(kcache_grind_filename, 'w+b') as f:
        k.output(f)
    print "Profile results written to", kcache_grind_filename
コード例 #52
0
def pytest_runtest_call(item):
    if SHOULD_PROFILE:
        p = Profile()
        p.enable()
        yield
        p.disable()
        stats = Stats(p)
        if SHOULD_PRINT:
            stats.sort_stats('cumulative').print_stats(50)
        if SHOULD_STORE:
             if not os.path.exists(BASEDIR):
                os.mkdir(BASEDIR)
             p.dump_stats(os.path.join(BASEDIR, '%s.pkl' % item.name))
    else:
        yield
コード例 #53
0
    def helper(*args, **kargs):
        filename = "profile-%s-%d.out" % (current_thread().name, get_ident())
        if filename in profiled_threads:
            raise RuntimeError(
                "Can not attach profiler on the same thread twice")

        dprint("running with profiler [", filename, "]")
        profiled_threads.add(filename)
        profiler = Profile()

        try:
            return profiler.runcall(func, *args, **kargs)
        finally:
            dprint("profiler results [", filename, "]")
            profiler.dump_stats(filename)
コード例 #54
0
    def output_timings(self, profile: Profile) -> None:
        """
        Output the timings measured by the profiler.
        :param profile: The profile.
        """
        directory = self.experiment_results_directory()
        stats_file_path = path.join(directory, 'timings.pstats')

        # Write raw stats
        profile.dump_stats(stats_file_path)

        # Process raw stats
        step_timings = pstats_to_step_timings(stats_file_path)

        self.output_case_results('timings', step_timings)
コード例 #55
0
    def __call__(self, environ: "WSGIEnvironment",
                 start_response: "StartResponse") -> t.Iterable[bytes]:
        response_body: t.List[bytes] = []

        def catching_start_response(status,
                                    headers,
                                    exc_info=None):  # type: ignore
            start_response(status, headers, exc_info)
            return response_body.append

        def runapp() -> None:
            app_iter = self._app(
                environ, t.cast("StartResponse", catching_start_response))
            response_body.extend(app_iter)

            if hasattr(app_iter, "close"):
                app_iter.close()  # type: ignore

        profile = Profile()
        start = time.time()
        profile.runcall(runapp)
        body = b"".join(response_body)
        elapsed = time.time() - start

        if self._profile_dir is not None:
            if callable(self._filename_format):
                filename = self._filename_format(environ)
            else:
                filename = self._filename_format.format(
                    method=environ["REQUEST_METHOD"],
                    path=environ["PATH_INFO"].strip("/").replace("/", ".")
                    or "root",
                    elapsed=elapsed * 1000.0,
                    time=time.time(),
                )
            filename = os.path.join(self._profile_dir, filename)
            profile.dump_stats(filename)

        if self._stream is not None:
            stats = Stats(profile, stream=self._stream)
            stats.sort_stats(*self._sort_by)
            print("-" * 80, file=self._stream)
            path_info = environ.get("PATH_INFO", "")
            print(f"PATH: {path_info!r}", file=self._stream)
            stats.print_stats(*self._restrictions)
            print(f"{'-' * 80}\n", file=self._stream)

        return [body]
コード例 #56
0
ファイル: utils.py プロジェクト: uniteddiversity/lektor
def profile_func(func):
    # pylint: disable=import-outside-toplevel

    from cProfile import Profile
    from pstats import Stats

    p = Profile()
    rv = []
    p.runcall(lambda: rv.append(func()))
    p.dump_stats("/tmp/lektor-%s.prof" % func.__name__)

    stats = Stats(p, stream=sys.stderr)
    stats.sort_stats("time", "calls")
    stats.print_stats()

    return rv[0]
コード例 #57
0
ファイル: BenchCSVParser.py プロジェクト: Cito/w4py-olde-docs
 def main(self):
     if len(sys.argv) > 1 and sys.argv[1].lower().startswith('prof'):
         self._shouldProfile = True
     if self._shouldRunTestSuite:
         from TestCSVParser import main
         main()
     start = time.time()
     if self._shouldProfile:
         prof = Profile()
         prof.runcall(self._main)
         filename = '%s.pstats' % self.__class__.__name__
         prof.dump_stats(filename)
         print 'Wrote', filename
     else:
         self._main()
     duration = time.time() - start
     print '%.1f secs' % duration
コード例 #58
0
ファイル: profiling.py プロジェクト: algard/testoob
class ProfileHelper(ProfilingHelper):
    def run(self):
        def run_callable():
            "A local function we can refer to in a string with profile.run"
            self.result = self.callable(*self.args, **self.kwargs)

        try:
            from cProfile import Profile
        except ImportError:
            from profile import Profile

        self.p = Profile().runctx("run_callable()", globals(), locals())
        self.p.dump_stats(self.filename)

    def stats(self):
        import pstats
        return pstats.Stats(self.p)
コード例 #59
0
ファイル: base.py プロジェクト: kenatbasis/python-driver
class BenchmarkThread(Thread):
    def __init__(self, thread_num, session, query, values, num_queries, profile):
        Thread.__init__(self)
        self.thread_num = thread_num
        self.session = session
        self.query = query
        self.values = values
        self.num_queries = num_queries
        self.profiler = Profile() if profile else None

    def start_profile(self):
        if self.profiler:
            self.profiler.enable()

    def finish_profile(self):
        if self.profiler:
            self.profiler.disable()
            self.profiler.dump_stats("profile-%d" % self.thread_num)