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)
def handle(self, *args, **options): if options['profile']: profiler = Profile() profiler.runcall(self._handle, *args, **options) profiler.print_stats() else: self._handle(*args, **options)
def run(self): haichi=[[0 for i in range(8)] for j in range(15)] for i in range(15): haichi[i][0]=20 haichi[i][7]=20 for i in range(1,7): haichi[0][i]=20 fort=Fort() ai=aiclass.AIField(haichi) t1=time.time() pr=Profile() pr.enable() for i in range(1000): ai.haichi[1][1]=i%4 ai.haichi[1][2]=i%3 ai.haichi[1][3]=i%2 ai.haichi[1][4]=i%3 ai.haichi[1][5]=4 ai.haichi[1][6]=1 ai.haichi[2][1]=1 ai.haichi[2][2]=1 ai.haichi[2][3]=3 ai.haichi[2][4]=2 ai.haichi[2][5]=3 fort.call_fortran(ai.haichi) pr.disable() pr.print_stats() t2=time.time() print(t2-t1) print(haichi)
def __call__(self, environ, start_response): response_body = [] def catching_start_response(status, headers, exc_info=None): start_response(status, headers, exc_info) return response_body.append def runapp(): appiter = self._app(environ, catching_start_response) response_body.extend(appiter) if hasattr(appiter, 'close'): appiter.close() p = Profile() p.runcall(runapp) body = ''.join(response_body) stats = Stats(p, stream=self._stream) stats.sort_stats(*self._sort_by) self._stream.write('-' * 80) self._stream.write('\nPATH: %r\n' % environ.get('PATH_INFO')) stats.print_stats(*self._restrictions) self._stream.write('-' * 80 + '\n\n') return [body]
def __init__(self, *args, **kwargs): super(OpticalAgent, self).__init__(*args, **kwargs) self.switches = {} self.switch_feature_reply_msg = {} self.socket_to_phy_conn = {} # STRUCTURES self.switch_to_flow_tables = {} self.switch_to_ports = {} # OSNR monitoring self.paths = {0: []} # Enabled paths self.flow_to_ipv6_flabel = {} # Validation of flows self.flow_to_datapath_match = {} # Per-datapath registers of # match fields for deleiton # purposes. self.ripple_function = self.init_ripple_function() self.osnr_list = [] # for debugging purposes # Converting 0.2dB loss, and multiplying # for 60km. self.abs_fiber_loss = 10**((0.2 * 60) / 10) # constant # Converting 6dB loss self.abs_WSS_loss = 10**(6 / float(10)) # constant self.packet_in_counter = 0 self.packet_in_trigger = 3 self.pr = Profile() self.pr.enable()
def profile(sort): if not sort: yield return sort_columns = ( "calls", "cumtime", "file", "ncalls", "pcalls", "line", "name", "nfl", "stdname", "time", "tottime", ) if sort not in sort_columns: raise RuntimeError( "{} not a valid sort column. Please use one of {}".format( sort, ", ".join(sort_columns))) try: from cProfile import Profile # type: ignore except ImportError: from Profile import Profile # type: ignore prof = Profile() prof.enable() yield prof.create_stats() prof.print_stats(sort=sort)
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")
def enable(self): if not self.started: self.start() elif not self.enabled: self.p = Profile() self.p.enable(subcalls=True) self.enabled = True
def prepare_game(cursor_lib_callback, cmdline_args=None): options = get_commandline_options(cmdline_args) initialize_files_and_folders() # locale.setlocale(locale.LC_ALL, "") init_logger_system() if options.load: game = load_game(options.load, cursor_lib_callback) else: from game import Game game = Game(options.game, cursor_lib_callback) if options.profile: profiler = Profile() profiler.enable() def write_profile(): profiler.disable() profile_util.write_results_log(profiler) atexit.register(write_profile) return game
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
def __run(self, *args, **kwargs): __start = time.time() # notify if we don't process quickly if __start - self.__time_submitted > 0.05: self.log.warning(f'Starting of {self.name} took too long: {__start - self.__time_submitted:.2f}s. ' f'Maybe there are not enough threads?') # start profiler pr = Profile() pr.enable() # Execute the function try: self._func(*args, **kwargs) except Exception as e: self.__format_traceback(e, *args, **kwargs) # disable profiler pr.disable() # log warning if execution takes too long __dur = time.time() - __start if self.__warn_too_long and __dur > 0.8: self.log.warning(f'Execution of {self.name} took too long: {__dur:.2f}s') s = io.StringIO() ps = Stats(pr, stream=s).sort_stats(SortKey.CUMULATIVE) ps.print_stats(0.1) # limit to output to 10% of the lines for line in s.getvalue().splitlines()[4:]: # skip the amount of calls and "Ordered by:" if line: self.log.warning(line)
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)
def wrap(*args, **kw): pr = Profile() pr.enable() result = func(*args, **kw) pr.disable() pr.print_stats() return result
def setUpClass(cls): cls.profiler = Profile() cls.profiler.enable() # Check if the server is running pipe1 = Popen(["ps", "aux"], stdout=PIPE) pipe1.wait() pipe2 = Popen(["grep", "doController.py"], stdin=pipe1.stdout, stdout=PIPE) pipe1.stdout.close() pipe1 = Popen(["grep", "-v", "grep"], stdin=pipe2.stdout, stdout=PIPE) pipe1.wait() pipe2.wait() pipe2.stdout.close() procs = pipe1.communicate()[0] # If the server is running, we keep it running if b"doController.py" in procs: urlopen('http://localhost:8000/restart') cls.is_running = True else: # If the server is not running, start it for the duration of the tests cls.pipe = Popen(['python3', f'{BASE_DIR}/src/controller.py']) i = 0 while not get_connection() and i < 100: i += 1 cls.cli = client.HTTPConnection('localhost:8000', timeout=100)
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
def profile(sort_args=['cumulative'], print_args=[10]): ''' Simple wrapper of cProfile python library to print in stdout useful infos about specific function runtime Suggested usage: Insert the following line before the definitio of the function whose profiling is needed @profile(sort_args=['name'], print_args=[N]) with N = # of tasks which are listed ''' profiler = Profile() def decorator(fn): def inner(*args, **kwargs): result = None try: result = profiler.runcall(fn, *args, **kwargs) finally: stats = pstats.Stats(profiler) stats.strip_dirs().sort_stats(*sort_args).print_stats( *print_args) return result return inner return decorator
def handle(self, *args, **options): if options['profile']: profiler = Profile() profiler.runcall(issue_cert) pstats.Stats(profiler).sort_stats('cumulative').print_stats(25) return issue_cert()
def profile(): prof = Profile() prof.runcall(f10) stat = Stats(prof) stat.strip_dirs() stat.sort_stats('cumulative') stat.print_stats()
def profile(self, request): """Start/stop the python profiler, returns profile results""" profile = self.__dict__.get("_profile") if "start" in request.properties: if not profile: profile = self.__dict__["_profile"] = Profile() profile.enable() self._log(LOG_INFO, "Started python profiler") return (OK, None) if not profile: raise BadRequestStatus("Profiler not started") if "stop" in request.properties: profile.create_stats() self._log(LOG_INFO, "Stopped python profiler") out = StringIO() stats = pstats.Stats(profile, stream=out) try: stop = request.properties["stop"] if stop == "kgrind": # Generate kcachegrind output using pyprof2calltree from pyprof2calltree import convert convert(stats, out) elif stop == "visualize": # Start kcachegrind using pyprof2calltree from pyprof2calltree import visualize visualize(stats) else: stats.print_stats() # Plain python profile stats return (OK, out.getvalue()) finally: out.close() raise BadRequestStatus("Bad profile request %s" % (request))
def setUp(self): dummy_cfg = os.path.join(gettempdir(), 'nousedft.cfg') open(dummy_cfg, "w").write("") self.default_options = [ '--load-plugins=pylint_odoo', '--reports=no', '--msg-template=' '"{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}"', '--output-format=colorized', '--rcfile=%s' % os.devnull, ] path_modules = os.path.join( os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'test_repo') self.paths_modules = [] root, dirs, _ = six.next(os.walk(path_modules)) for path in dirs: self.paths_modules.append(os.path.join(root, path)) self.default_extra_params = [ '--disable=all', '--enable=odoolint,pointless-statement,trailing-newlines', ] self.profile = Profile() self.sys_path_origin = list(sys.path) self.maxDiff = None self.expected_errors = EXPECTED_ERRORS.copy()
def handle(self, *args, **options): """ Handles the command line arguments - initiates profiling if set to true """ if options.get('profile', False): profiler = Profile() profiler.runcall(self._handle, *args, **options) profiler.print_stats() else: self._handle(*args, **options)
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)
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))
def generate_profiler_entry(): def func(): a = 1 + 2 return a prof = Profile() prof.runctx("func()", locals(), globals()) return prof.getstats()
def _profile(self, goal, func): from cProfile import Profile from rpython.tool.lsprofcalltree import KCacheGrind d = {'func': func} prof = Profile() prof.runctx("res = func()", globals(), d) KCacheGrind(prof).output(open(goal + ".out", "w")) return d['res']
def cursor_execute_profiler(): cursor = connection.cursor() profiler = Profile() for _ in range(10): profiler.runctx( "cursor.execute('''SELECT * FROM httc_order WHERE mk_contract_id = 1000 ORDER BY price''')", locals(), globals()) convert(profiler.getstats(), 'cursor_execute_profiler.kgrind')
def test_080_fnovalid(self): fname = self.get_fname_stats('_novalid') Profile().dump_stats(fname) result = get_pstats_print2list(fname, filter_fnames=[], exclude_fnames=None, limit=None) self.assertFalse(result)
def profile(): #cProfile.run('main()','stats') #Tweede test met RGB topo coloring zonder schaduw.py from cProfile import Profile from pyprof2calltree import convert, visualize profiler = Profile() profiler.runctx('main()', locals(), globals()) visualize(profiler.getstats())
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 wrap(*args, **kwargs): prof = Profile() res = prof.runcall(func, *args, **kwargs) stats = Stats(prof) stats.strip_dirs() stats.sort_stats('tottime') stats.print_stats(20) return res