def __analyze2 (): import profile profile.runctx("self.__analyze2()", locals(), globals(), "/tmp/pychessprofile") from pstats import Stats s = Stats("/tmp/pychessprofile") s.sort_stats('cumulative') s.print_stats()
def _removeBookingTest(self): def executeRemoveBookingTest(): times = [] i = 0 while i < bookingsPerTest: confId, bookingId = self._getIdsForRemoveBooking() start = time.time() cstart = time.clock() self._removeBooking(confId, bookingId) cend = time.clock() end = time.time() times.append((end - start, cend - cstart)) self._updateValidIdsAfterRemoveBooking(confId) i = i + 1 self.average, self.deviation, self.caverage, self.cdeviation = processResults(times) if doProfile: profile.runctx( "executeRemoveBookingTest()", {}, {"executeRemoveBookingTest": executeRemoveBookingTest}, "removeBooking.prof", ) else: exec "executeRemoveBookingTest()" in {}, {"executeRemoveBookingTest": executeRemoveBookingTest}
def process(self): """ Processes the request, analyzing the parameters, and feeding them to the _getAnswer() method (implemented by derived classes) """ ContextManager.set('currentRH', self) self._checkParams() self._checkProtection() if self.CHECK_HTML: try: security.Sanitization.sanitizationCheck(self._target, self._params, self._aw, ['requestInfo']) except HtmlForbiddenTag as e: raise HTMLSecurityError('ERR-X0', 'HTML Security problem. {}'.format(e)) if self._doProcess: if Config.getInstance().getProfile(): import profile, pstats, random proffilename = os.path.join(Config.getInstance().getTempDir(), "service%s.prof" % random.random()) result = [None] profile.runctx("result[0] = self._getAnswer()", globals(), locals(), proffilename) answer = result[0] rep = Config.getInstance().getTempDir() stats = pstats.Stats(proffilename) stats.strip_dirs() stats.sort_stats('cumulative', 'time', 'calls') stats.dump_stats(os.path.join(rep, "IndicoServiceRequestProfile.log")) os.remove(proffilename) else: answer = self._getAnswer() self._deleteTempFiles() return answer
def _process_retry_do(self, profile): profile_name, res = '', '' # old code gets parameters from call # new code utilizes of flask.request if len(inspect.getargspec(self._checkParams).args) < 2: self._checkParams() else: self._checkParams(self._reqParams) func = getattr(self, '_checkParams_' + request.method, None) if func: func() self._checkProtection() func = getattr(self, '_checkProtection_' + request.method, None) if func: func() security.Sanitization.sanitizationCheck(self._target, self._reqParams, self._aw, self._doNotSanitizeFields) if self._doProcess: if profile: profile_name = os.path.join(Config.getInstance().getTempDir(), 'stone{}.prof'.format(random.random())) result = [None] profiler.runctx('result[0] = self._process()', globals(), locals(), profile_name) res = result[0] else: res = self._process() return profile_name, res
def handle(*args, **kw): vars = dict() vars["_f"] = func vars["_args"] = args vars["_kw"] = kw pf.runctx("r=_f(*_args, **_kw)", globals(), vars, sort="time") return vars["r"]
def rebuild_osm(filepath,context): from io_osm.osm_types import OSM if debug: debugger.start(log) if debug: debugger.debug("OSM import started: %r..." % filepath) debugger.debug("parsing xml to dom ...") # deactive undo for better performance and less memory usage global_undo = context.user_preferences.edit.use_global_undo context.user_preferences.edit.use_global_undo = False xml = parse(filepath) root = xml.documentElement # quit edit mode if enabled and deselect all objects editMode(context.scene,False) deselectObjects(context.scene) osm = OSM(root) if profiler: import profile import time profile.runctx('osm.generate(True)',{'debug':debug,'debugger':debugger,'log':log},{'osm':osm},'profile_results_'+time.strftime("%y-%m-%d-%H-%M-%S")) else: osm.generate(True) xml.unlink() # reset undo preference context.user_preferences.edit.use_global_undo = global_undo
def _process_retry_do(self, profile): profile_name, res = '', '' # old code gets parameters from call # new code utilizes of flask.request if len(inspect.getargspec(self._checkParams).args) < 2: self._checkParams() else: self._checkParams(self._reqParams) func = getattr(self, '_checkParams_' + request.method, None) if func: func() self._checkProtection() func = getattr(self, '_checkProtection_' + request.method, None) if func: func() security.Sanitization.sanitizationCheck(self._target, self._reqParams, self._aw, self._doNotSanitizeFields) if self._doProcess: if profile: profile_name = os.path.join( Config.getInstance().getTempDir(), 'stone{}.prof'.format(random.random())) result = [None] profiler.runctx('result[0] = self._process()', globals(), locals(), profile_name) res = result[0] else: res = self._process() return profile_name, res
def expose(self, widget, event): context = widget.window.cairo_create() #r = (event.area.x, event.area.y, event.area.width, event.area.height) #context.rectangle(r[0]-.5, r[1]-.5, r[2]+1, r[3]+1) #context.clip() if False: import profile profile.runctx("self.draw(context, event.area)", locals(), globals(), "/tmp/pychessprofile") from pstats import Stats s = Stats("/tmp/pychessprofile") s.sort_stats('cumulative') s.print_stats() else: self.drawcount += 1 start = time() self.animationLock.acquire() self.draw(context, event.area) self.animationLock.release() self.drawtime += time() - start #if self.drawcount % 100 == 0: # print "Average FPS: %0.3f - %d / %d" % \ # (self.drawcount/self.drawtime, self.drawcount, self.drawtime) return False
def main(): random.seed samples = 9 #number of samples increase_by = 100 #100-900 for i in range(1, samples + 1): n = increase_by * i a = [] for i in range(n): a.append(random.randint(-100, 100)) profile.runctx('fun(n,a)', {}, { 'n': n, 'fun': fun, 'a': a }) #creates function profiles, replace fun with function name a.clear() increase_by = 1000 # 1000-9000 for i in range(1, samples + 1): n = increase_by * i a = [] for i in range(n): a.append(random.randint(-100, 100)) profile.runctx('fun(n,a)', {}, { 'n': n, 'fun': fun, 'a': a }) #creates function profiles, replace fun with function name a.clear()
def _removeBookingTest(self): def executeRemoveBookingTest(): times = [] i = 0 while i < bookingsPerTest: confId, bookingId = self._getIdsForRemoveBooking() start = time.time() cstart = time.clock() self._removeBooking(confId, bookingId) cend = time.clock() end = time.time() times.append((end - start, cend - cstart)) self._updateValidIdsAfterRemoveBooking(confId) i = i + 1 self.average, self.deviation, self.caverage, self.cdeviation = processResults( times) if doProfile: profile.runctx( "executeRemoveBookingTest()", {}, {'executeRemoveBookingTest': executeRemoveBookingTest}, 'removeBooking.prof') else: exec "executeRemoveBookingTest()" in {}, { 'executeRemoveBookingTest': executeRemoveBookingTest }
def _removeConfTest(self): def executeRemoveConfTest(): self.removedConfs = set(['']) times = [] i = 0 while i < removeConfsPerTest: confId = self.random.choice(self.confsWithBookings) start = time.time() cstart = time.clock() self._removeConf(confId) cend = time.clock() end = time.time() times.append((end - start, cend - cstart)) i = i + 1 self.validConfIds.remove(confId) self.confsWithBookings.remove(confId) if len(self.confsWithBookings) == 0: self._log("Stopped removeConf test after removing " + str(i) + " conferences") break self.average, self.deviation, self.caverage, self.cdeviation = processResults(times) if doProfile: profile.runctx("executeRemoveConfTest()", {}, {'executeRemoveConfTest' : executeRemoveConfTest}, 'removeConference.prof') else: exec "executeRemoveConfTest()" in {}, {'executeRemoveConfTest' : executeRemoveConfTest}
def __call__(self, environ, start_response): path = environ.get('PATH_INFO', '_') if path in LogMiddleware.PROFILE_SET: vars = dict(f=self.invoke_app, environ=environ, start_response=start_response) profile.runctx("r=f(environ, start_response)", globals(), vars, sort="time") return vars["r"] else: return self.invoke_app(environ, start_response)
def main(): global iprange, ipHasFind print("Process Sum:", ProcessSum) if ProcessSum > 1: q = Queue() q.put(ipHasFind) for i in range(ProcessSum): print("Start Process:", i) p = CheckProcess(q, iprange) p.start() try: with open("ip.txt", "w") as f: sum = 0 while True: ip = ipList.get() s = ip + "|" f.write(s) sum += 1 print("All Sucess Ip:%4d" % sum) except (KeyboardInterrupt, SystemExit) as e: print("main exited") finally: file_name = "find_log" + "0" + ".txt" with open(file_name) as f: s = f.readlines() d = dict([[int(i.split(":")[0]), int(i.split(":")[-1])] for i in list(map(lambda x: x[:-1], s))]) while GoodIpRange.qsize() > 0: nowdict = GoodIpRange.get() d.update(nowdict) with open(file_name, "w") as f: for i in d.items(): print(i[0], ":", i[1]) f.write(str(i[0]) + ":" + str(i[1]) + "\n") else: try: q = Queue() q.put(ipHasFind) loop = asyncio.get_event_loop() ipfactory = get_ip.ipFactory(q, iprange) testip = Test_Ip(loop, ipfactory) loop.create_task(testip.Server()) profile.runctx("loop.run_until_complete(testip.SuccessStop())", globals(), locals()) # loop.run_until_complete(testip.SuccessStop()) except (KeyboardInterrupt, SystemExit) as e: loop.create_task(testip.stop()) loop.run_until_complete(testip.SuccessStop()) finally: loop.close() print("Task exit")
def test_2(): d = globals().copy() def testfunc(): global x x = 1 d['testfunc'] = testfunc profile.runctx("testfunc()", d, d, TESTFN) vereq (x, 1) os.unlink (TESTFN)
def handle(*args, **kw): if xconfig.OPEN_PROFILE: vars = dict() vars["_f"] = func vars["_args"] = args vars["_kw"] = kw pf.runctx("r=_f(*_args, **_kw)", globals(), vars, sort="time") return vars["r"] return func(*args, **kw)
def slotMoveManyProfile(self): """ for profiling, to use, rename slotMoveMany() to slotMoveManyReal() and rename this function to slotMoveMany() """ profile.runctx('self.slotMoveManyReal()' , vars(sys.modules[__name__]) , vars() , 'profile.log')
def _profile(self, command): import profile import pstats import tempfile statsfile = tempfile.mkstemp(".prof")[1] profile.runctx('command.module.main()', globals(), locals(), statsfile) pstats.Stats(statsfile).strip_dirs().sort_stats('cumulative').print_stats() os.remove(statsfile)
def profiling(): import profile repeator = CommandRepeater(configuration_dir_name='servers', name='repeater') # repeator.load() repeator.bootstrap() # repeator.test_connection() profile.runctx('repeator.get_response(*args)', globals(), {'repeator':repeator, 'args':(1, 2, 3) })
def test_2(): d = globals().copy() def testfunc(): global x x = 1 d['testfunc'] = testfunc profile.runctx("testfunc()", d, d, TESTFN) vereq(x, 1) os.unlink(TESTFN)
def main(): """Start the game. """ if config.profile: import datetime, os, profile timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") filename = os.path.join(SCRIPT_DIR, "profile-%s.log" % timestamp) profile.runctx("Controller().run()", globals(), None, filename) else: Controller().run()
def _profile(self, command): import profile import pstats import tempfile statsfile = tempfile.mkstemp(".prof")[1] profile.runctx('command.module.main()', globals(), locals(), statsfile) pstats.Stats(statsfile).strip_dirs().sort_stats( 'cumulative').print_stats() os.remove(statsfile)
def write_out_test(fname, anims, uv_img_as_tex, sep_anim, a_only, copy_tex, t_path, fp_accuracy, tbs, tex_processor, b_layers): import profile import pstats wo = "write_out('%s', %s, %s, %s, %s, %s, '%s', %s, '%s', '%s', %s)" % \ (fname, anims, uv_img_as_tex, sep_anim, a_only, copy_tex, t_path, fp_accuracy, tbs, tex_processor, b_layers) profile.runctx(wo, globals(), {}, 'main_prof') stats = pstats.Stats('main_prof') stats.strip_dirs() stats.sort_stats('time') stats.print_stats(10)
def testknn(testfeatures, testactuallabels, trainfeatures, trainactuallabels, k): errorcount = 0 count = 0 for sample in testfeatures: profile.runctx('print(findKNeighbours(sample, trainfeatures, trainactuallabels, k)); print()',globals(),{'sample': [sample], 'trainfeatures': trainfeatures, 'trainactuallabels': trainactuallabels, 'k':k },) prediction = findKNeighbours([sample], trainfeatures, trainactuallabels, k) if int(prediction) != int(testactuallabels[count]): errorcount += 1 count += 1 prederror = float(errorcount)/len(testfeatures) print "Prediction accuracy for KNN %s" % ((1-prederror)*100)
def main(): myreloader = Reloader() Profile = True if Profile: print 'started profiler' fun = myreloader.main profile.runctx('fun()', locals(), globals(), 'myprof') stats = pstats.Stats('myprof') stats.strip_dirs().sort_stats('cumulative').print_stats() else: myreloader.main()
def profiling(): import profile repeator = CommandRepeater(configuration_dir_name='servers', name='repeater') # repeator.load() repeator.bootstrap() # repeator.test_connection() profile.runctx('repeator.get_response(*args)', globals(), { 'repeator': repeator, 'args': (1, 2, 3) })
def main(): """ Main program cycle """ print console.colored("""\nThis script will migrate your Indico DB to a new version. We recommend that this operation be executed while the web server is down, in order to avoid concurrency problems and DB conflicts.\n\n""", 'yellow') parser = argparse.ArgumentParser(description='Execute migration') parser.add_argument('--dry-run', '-n', dest='dry_run', action='store_true', help='Only show which migration tasks would be executed') parser.add_argument('--with-rb', dest='useRBDB', action='store_true', help='Use the Room Booking DB') parser.add_argument('--run-only', dest='specified', default='', help='Specify which step(s) to run (comma-separated)') parser.add_argument('--run-from', dest='run_from', default='', help='Specify FROM which step to run (inclusive)') parser.add_argument('--prev-version', dest='prevVersion', help='Previous version of Indico (used by DB)', default=__version__) parser.add_argument('--profile', dest='profile', help='Use profiling during the migration', action='store_true') args = parser.parse_args() if args.dry_run or console.yesno("Are you sure you want to execute the migration now?"): try: if args.profile: import profile, random, os proffilename = os.path.join(Config.getInstance().getTempDir(), "migration%s.prof" % str(random.random())) result = None profile.runctx("""result=runMigration(withRBDB=args.useRBDB, prevVersion=parse_version(args.prevVersion), specified=filter(lambda x: x, map(lambda x: x.strip(), args.specified.split(','))), run_from=args.run_from, dry_run=args.dry_run)""", globals(), locals(), proffilename) return result else: return runMigration(withRBDB=args.useRBDB, prevVersion=parse_version(args.prevVersion), specified=filter(lambda x: x, map(lambda x: x.strip(), args.specified.split(','))), run_from=args.run_from, dry_run=args.dry_run) except ControlledExit: return 1 except (Exception, SystemExit, KeyboardInterrupt): print console.colored("\nMigration failed! DB may be in " " an inconsistent state:", 'red', attrs=['bold']) print console.colored(traceback.format_exc(), 'red') return -1 else: return 1
def test_3(): result = [] def testfunc1(): try: len(None) except: pass try: len(None) except: pass result.append(True) def testfunc2(): testfunc1() testfunc1() profile.runctx("testfunc2()", locals(), locals(), TESTFN) vereq(result, [True, True]) os.unlink(TESTFN)
def _try_post_init(self): """_try_post_init-> return None, or exception string if method fails Try to exec all the __postInitExecs """ try: if _PROFILE: import profile profile.runctx("self._post_init()", globals(), locals(), '_post_init.prof') else: self._post_init() except: return list(sys.exc_info()) + [str(traceback.format_exc())] self._isReady = True return None
def main(): """ Main program cycle """ print console.colored("""\nThis script will migrate your Indico DB to a new version. We recommend that this operation be executed while the web server is down, in order to avoid concurrency problems and DB conflicts.\n\n""", 'yellow') parser = argparse.ArgumentParser(description='Execute migration') parser.add_argument('--dry-run', '-n', dest='dry_run', action='store_true', help='Only show which migration tasks would be executed') parser.add_argument('--run-only', dest='specified', default='', help='Specify which step(s) to run (comma-separated)') parser.add_argument('--run-from', dest='run_from', default='', help='Specify FROM which step to run (inclusive)') parser.add_argument('--prev-version', dest='prevVersion', help='Previous version of Indico (used by DB)', default=__version__) parser.add_argument('--profile', dest='profile', help='Use profiling during the migration', action='store_true') args = parser.parse_args() if args.dry_run or console.yesno("Are you sure you want to execute the migration now?"): try: if args.profile: import profile, random, os proffilename = os.path.join(Config.getInstance().getTempDir(), "migration%s.prof" % str(random.random())) result = None profile.runctx("""result=runMigration( prevVersion=parse_version(args.prevVersion), specified=filter(lambda x: x, map(lambda x: x.strip(), args.specified.split(','))), run_from=args.run_from, dry_run=args.dry_run)""", globals(), locals(), proffilename) return result else: with make_app().app_context(): return runMigration(prevVersion=parse_version(args.prevVersion), specified=filter(None, map(str.strip, args.specified.split(','))), run_from=args.run_from, dry_run=args.dry_run) except ControlledExit: return 1 except (Exception, SystemExit, KeyboardInterrupt): print console.colored("\nMigration failed! DB may be in an inconsistent state:", 'red', attrs=['bold']) print console.colored(traceback.format_exc(), 'red') return -1 else: return 1
def profile_that(blabla, gl=lambda: globals(), l=lambda: locals()): f = TMP_FILE() filename = f.get_filename("txt") rep = profile.runctx(blabla, gl(), l(), filename=filename) stats_ = pstats.Stats(filename) f.delete() return stats_
def run(self): try: loop = asyncio.get_event_loop() ipfactory = get_ip.ipFactory(self.q, self.iprange) testip = Test_Ip(loop, ipfactory) loop.create_task(testip.Server()) profile.runctx("loop.run_until_complete(testip.SuccessStop())", globals(), locals()) # loop.run_until_complete(testip.SuccessStop()) except (KeyboardInterrupt, SystemExit) as e: loop.create_task(testip.stop()) loop.run_until_complete(testip.SuccessStop()) finally: loop.close() print("Task exit")
def do_profile(cmd, globals, locals, sort_order, callers): fd, fn = tempfile.mkstemp() try: if hasattr(profile, 'runctx'): profile.runctx(cmd, globals, locals, fn) else: raise NotImplementedError('No profiling support under Python 2.3') stats = pstats.Stats(fn) stats.strip_dirs() # calls,time,cumulative and cumulative,calls,time are useful stats.sort_stats(*sort_order or ('cumulative', 'calls', 'time')) if callers: stats.print_callers() else: stats.print_stats() finally: os.remove(fn)
def do_profile(cmd, globals, locals, sort_order, callers): fd, fn = tempfile.mkstemp() try: if hasattr(profile, "runctx"): profile.runctx(cmd, globals, locals, fn) else: raise NotImplementedError("No profiling support under Python 2.3") stats = pstats.Stats(fn) stats.strip_dirs() # calls,time,cumulative and cumulative,calls,time are useful stats.sort_stats(*sort_order or ("cumulative", "calls", "time")) if callers: stats.print_callers() else: stats.print_stats() finally: os.remove(fn)
def runTest(self): globs = { 'pyadfinput': self._testname + ".pyadf", 'testobj': self, 'testing_force_openbabel': self._forceopenbabel } if not self._profile: execfile(os.path.join(self._pyadfpath, 'pyadf'), globs, {}) else: profile.runctx( "execfile (pyadf_path)", globs, {'pyadf_path': os.path.join(self._pyadfpath, 'pyadf')}, "pyadf_profile") del globs
def _process_retry_do(self, profile): profile_name, res = '', '' try: # old code gets parameters from call # new code utilizes of flask.request if len(inspect.getargspec(self._checkParams).args) < 2: cp_result = self._checkParams() else: cp_result = self._checkParams(self._reqParams) if isinstance(cp_result, (current_app.response_class, Response)): return '', cp_result func = getattr(self, '_checkParams_' + request.method, None) if func: cp_result = func() if isinstance(cp_result, (current_app.response_class, Response)): return '', cp_result except NoResultFound: # sqlalchemy .one() not finding anything raise NotFoundError(_('The specified item could not be found.'), title=_('Item not found')) self._checkProtection() func = getattr(self, '_checkProtection_' + request.method, None) if func: func() security.Sanitization.sanitizationCheck(self._target, self._reqParams, self._aw, self._doNotSanitizeFields) if self._doProcess: if profile: profile_name = os.path.join( Config.getInstance().getTempDir(), 'stone{}.prof'.format(random.random())) result = [None] profiler.runctx('result[0] = self._process()', globals(), locals(), profile_name) res = result[0] else: res = self._process() return profile_name, res
def _process_retry_do(self, profile): profile_name, res = '', '' try: # old code gets parameters from call # new code utilizes of flask.request if len(inspect.getargspec(self._checkParams).args) < 2: cp_result = self._checkParams() else: cp_result = self._checkParams(self._reqParams) if isinstance(cp_result, (current_app.response_class, Response)): return '', cp_result func = getattr(self, '_checkParams_' + request.method, None) if func: cp_result = func() if isinstance(cp_result, (current_app.response_class, Response)): return '', cp_result except NoResultFound: # sqlalchemy .one() not finding anything raise NotFoundError(_('The specified item could not be found.'), title=_('Item not found')) rv = self.normalize_url() if rv is not None: return '', rv self._checkProtection() func = getattr(self, '_checkProtection_' + request.method, None) if func: func() security.Sanitization.sanitizationCheck(self._target, self._reqParams, self._aw, self._doNotSanitizeFields) if self._doProcess: if profile: profile_name = os.path.join(Config.getInstance().getTempDir(), 'stone{}.prof'.format(random.random())) result = [None] profiler.runctx('result[0] = self._process()', globals(), locals(), profile_name) res = result[0] else: res = self._process() return profile_name, res
def _indexQueryTest(self): def executeIndexQueryTest(): times = [] i = 0 while i < indexQueriesPerTest: start = time.time() cstart = time.clock() self._indexQuery() cend = time.clock() end = time.time() times.append((end - start, cend - cstart)) i = i + 1 self.average, self.deviation, self.caverage, self.cdeviation = processResults(times) if doProfile: profile.runctx("executeIndexQueryTest()", {}, {'executeIndexQueryTest' : executeIndexQueryTest}, 'indexQuery.prof') else: exec "executeIndexQueryTest()" in {}, {'executeIndexQueryTest' : executeIndexQueryTest}
def main(args): import os if args[1] == "pc": import profile pr = profile.Profile() for i in range(5): print pr.calibrate(100000) return elif args[1] == "p": import profile ret = 0 # profile.Profile.bias = 5.26e-6 profile.runctx("ret = main2(args[1:])", globals(), locals()) return ret elif args[1].startswith("h"): import hotshot, hotshot.stats import warnings warnings.filterwarnings("ignore") tmp = os.tempnam() try: l = args[1].startswith("hl") p = hotshot.Profile(tmp, l) ret = p.runcall(main2, args[1:]) p.close() p = None if l: if args[1] == "hl2": _dump_hotshot_lineinfo2(tmp) else: _dump_hotshot_lineinfo(tmp) else: hotshot.stats.load(tmp).print_stats() finally: try: os.remove(tmp) except OSError: pass return ret return main2(args)
def main(args): code = args['<code>'] infer = args['--infer'] n = int(args['-n']) for i in range(n): run(code, i, infer=infer) if args['--precision']: pstats.f8 = f8 jedi.set_debug_function(notices=args['--debug']) if args['--omit']: run(code, n, infer=infer) else: profile.runctx('run(code, n, infer=infer)', globals(), locals(), sort=args['-s'])
def profile(cmd, globals, locals, sort_order, callers): # pragma: no cover # runs a command under the profiler and print profiling output at shutdown import os import profile import pstats import tempfile fd, fn = tempfile.mkstemp() try: profile.runctx(cmd, globals, locals, fn) stats = pstats.Stats(fn) stats.strip_dirs() # calls,time,cumulative and cumulative,calls,time are useful stats.sort_stats(*sort_order or ('cumulative', 'calls', 'time')) if callers: stats.print_callers(.3) else: stats.print_stats(.3) finally: os.remove(fn)
def main(): #Usage if len(sys.argv) != 3: print "Usage: CRADLE.py <domain file> <observations file> \n" sys.exit() try: planLibrary = readDomain() print planLibrary except: print "Usage: Domain File Corrupt\n" sys.exit() try: observations = readObservations(planLibrary) except: print "Usage: Observations File Corrupt\n" sys.exit() profile.runctx('myMain(planLibrary, observations)', globals(), locals())
def profile(cmd, globals, locals, sort_order, callers): import profile import pstats import tempfile fd, fn = tempfile.mkstemp() try: if hasattr(profile, 'runctx'): profile.runctx(cmd, globals, locals, fn) else: raise NotImplementedError('No profiling support under Python 2.3') stats = pstats.Stats(fn) stats.strip_dirs() # calls,time,cumulative and cumulative,calls,time are useful stats.sort_stats(*sort_order or ('cumulative', 'calls', 'time')) if callers: stats.print_callers(.3) else: stats.print_stats(.3) finally: os.remove(fn)
def main(): """ Main program cycle """ print console.colored("""\nThis script will migrate your Indico DB to a new version. We recommend that this operation be executed while the web server is down, in order to avoid concurrency problems and DB conflicts.\n\n""", 'yellow') parser = argparse.ArgumentParser(description='Execute migration') parser.add_argument('--with-rb', dest='useRBDB', action='store_true', help='Use the Room Booking DB') parser.add_argument('--run-only', dest='specified', default='', help='Specify which step(s) to run (comma-separated)') parser.add_argument('--prev-version', dest='prevVersion', help='Previous version of Indico (used by DB)', default=__version__) parser.add_argument('--profile', dest='profile', help='Use profiling during the migration', action='store_true') args = parser.parse_args() if console.yesno("Are you sure you want to execute the " "migration now?"): try: if args.profile: import profile, random, os proffilename = os.path.join(Config.getInstance().getTempDir(), "migration%s.prof" % str(random.random())) result = None profile.runctx("""result=runMigration(withRBDB=args.useRBDB, prevVersion=parse_version(args.prevVersion), specified=filter(lambda x: x, map(lambda x: x.strip(), args.specified.split(','))))""", globals(), locals(), proffilename) return result else: return runMigration(withRBDB=args.useRBDB, prevVersion=parse_version(args.prevVersion), specified=filter(lambda x: x, map(lambda x: x.strip(), args.specified.split(',')))) except: print console.colored("\nMigration failed! DB may be in " " an inconsistent state:", 'red', attrs=['bold']) print console.colored(traceback.format_exc(), 'red') else: return 1
def _addBookingTest(self): def executeAddBookingTest(): times = [] i = 0 while i < bookingsPerTest: confId = self._getConfIdForAdding() start = time.time() cstart = time.clock() self._addBooking(confId) cend = time.clock() end = time.time() times.append((end - start, cend - cstart)) self._updateValidIdsAfterAddingBooking(confId) i = i + 1 self.average, self.deviation, self.caverage, self.cdeviation = processResults(times) if doProfile: profile.runctx("executeAddBookingTest()", {}, {'executeAddBookingTest' : executeAddBookingTest}, 'addBooking.prof') else: exec "executeAddBookingTest()" in {}, {'executeAddBookingTest' : executeAddBookingTest}
def process(self): """ Processes the request, analyzing the parameters, and feeding them to the _getAnswer() method (implemented by derived classes) """ g.rh = self sentry_set_tags({'rh': self.__class__.__name__}) self._process_args() self._check_access() if self.CHECK_HTML: try: security.Sanitization.sanitizationCheck( self._params, ['requestInfo']) except HtmlForbiddenTag as e: raise HTMLSecurityError('ERR-X0', 'HTML Security problem. {}'.format(e)) if self._doProcess: if config.PROFILE: import profile, pstats, random proffilename = os.path.join(config.TEMP_DIR, "service%s.prof" % random.random()) result = [None] profile.runctx("result[0] = self._getAnswer()", globals(), locals(), proffilename) answer = result[0] stats = pstats.Stats(proffilename) stats.sort_stats('cumulative', 'time', 'calls') stats.dump_stats( os.path.join(config.TEMP_DIR, "IndicoServiceRequestProfile.log")) os.remove(proffilename) else: answer = self._getAnswer() return answer
def __call__(self, req): """Default handler function for mod_python. Mostly this function passes control to L{handleRequest}.""" Logger.setRequest(req) try: options = req.get_options() if options.has_key("pyWebMvcProfile") and \ options["pyWebMvcProfile"].lower() in ("true", "on", "yes"): import profile outFile = open("/tmp/pywebmvc.profile","a") outFile.write( """============================================================ Profile results for %s ============================================================ """ % (req.uri)) sys.stdout = outFile try: profile.runctx('locals()["rt"] = self.handleRequest(req)',globals(),locals()) return locals()["rt"] finally: sys.stdout = sys.__stdout__ outFile.close() elif options.has_key("pyWebMvcTrace") and \ options["pyWebMvcTrace"].lower() in ("true", "on", "yes"): tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix], trace=1, count=0) sys.stdout = apacheOut try: tracer.runctx('locals()["rt"] = self.handleRequest(req)',globals(),locals()) return locals()["rt"] finally: sys.stdout = sys.__stdout__ else: result = self.handleRequest(req) self.cleanupRequest(req) return result finally: Logger.setRequest(None)
def rebuild_osm(filepath, context): from io_osm.osm_types import OSM if debug: debugger.start(log) if debug: debugger.debug("OSM import started: %r..." % filepath) debugger.debug("parsing xml to dom ...") # deactive undo for better performance and less memory usage global_undo = context.user_preferences.edit.use_global_undo context.user_preferences.edit.use_global_undo = False xml = parse(filepath) root = xml.documentElement # quit edit mode if enabled and deselect all objects editMode(context.scene, False) deselectObjects(context.scene) osm = OSM(root) if profiler: import profile import time profile.runctx('osm.generate(True)', { 'debug': debug, 'debugger': debugger, 'log': log }, {'osm': osm}, 'profile_results_' + time.strftime("%y-%m-%d-%H-%M-%S")) else: osm.generate(True) xml.unlink() # reset undo preference context.user_preferences.edit.use_global_undo = global_undo
def reference_viewer(sys_argv): """Create reference viewer from command line.""" viewer = ReferenceViewer(layout=default_layout) viewer.add_default_plugins() viewer.add_separately_distributed_plugins() # Parse command line options with argparse module from argparse import ArgumentParser argprs = ArgumentParser(description="Run the Ginga reference viewer.") viewer.add_default_options(argprs) argprs.add_argument('-V', '--version', action='version', version='%(prog)s {}'.format(version.version)) (options, args) = argprs.parse_known_args(sys_argv[1:]) if options.display: os.environ['DISPLAY'] = options.display # Are we debugging this? if options.debug: import pdb pdb.run('viewer.main(options, args)') # Are we profiling this? elif options.profile: import profile print(("%s profile:" % sys_argv[0])) profile.runctx('viewer.main(options, args)', dict(options=options, args=args, viewer=viewer), {}) else: viewer.main(options, args)
def main(): args = docopt(__doc__) config = configuration.from_file(open(args['--config'])) start = lambda: run(config, args['<engine>'], args['<store>'], args['<dump_path>'], args['--logger'], args['--threads']) try: if args['--profile']: stats = profile.runctx("start()", globals(), locals()) else: start() finally: if args['--profile']: print(stats)
def process( self, params ): """ """ profile = Config.getInstance().getProfile() proffilename = "" res = "" MAX_RETRIES = 10 retry = MAX_RETRIES textLog = [] self._startTime = datetime.now() # clear the context ContextManager.destroy() ContextManager.set('currentRH', self) #redirect to https if necessary if self._checkHttpsRedirect(): return DBMgr.getInstance().startRequest() self._startRequestSpecific2RH() # I.e. implemented by Room Booking request handlers textLog.append("%s : Database request started"%(datetime.now() - self._startTime)) Logger.get('requestHandler').info('[pid=%s] Request %s started (%s)' % (os.getpid(),id(self._req), self._req.unparsed_uri)) # notify components that the request has started self._notify('requestStarted', self._req) forcedConflicts = Config.getInstance().getForceConflicts() try: while retry>0: if retry < MAX_RETRIES: # notify components that the request is being retried self._notify('requestRetry', self._req, MAX_RETRIES - retry) try: Logger.get('requestHandler').info('\t[pid=%s] from host %s' % (os.getpid(), self.getHostIP())) try: # clear the fossile cache at the start of each request fossilize.clearCache() # delete all queued emails GenericMailer.flushQueue(False) DBMgr.getInstance().sync() # keep a link to the web session in the access wrapper # this is used for checking access/modification key existence # in the user session self._aw.setIP( self.getHostIP() ) self._aw.setSession(self._getSession()) #raise(str(dir(self._websession))) self._setSessionUser() self._setLang(params) if self._getAuth(): if self._getUser(): Logger.get('requestHandler').info('Request %s identified with user %s (%s)' % (id(self._req), self._getUser().getFullName(), self._getUser().getId())) if not self._tohttps and Config.getInstance().getAuthenticatedEnforceSecure(): self._tohttps = True if self._checkHttpsRedirect(): return #if self._getUser() != None and self._getUser().getId() == "893": # profile = True self._reqParams = copy.copy( params ) self._checkParams( self._reqParams ) self._checkProtection() security.Sanitization.sanitizationCheck(self._target, self._reqParams, self._aw, self._doNotSanitizeFields) if self._doProcess: if profile: import profile, pstats proffilename = os.path.join(Config.getInstance().getTempDir(), "stone%s.prof" % str(random.random())) result = [None] profile.runctx("result[0] = self._process()", globals(), locals(), proffilename) res = result[0] else: res = self._process() # Save web session, just when needed sm = session.getSessionManager() sm.maintain_session(self._req, self._websession) # notify components that the request has finished self._notify('requestFinished', self._req) # Raise a conflict error if enabled. This allows detecting conflict-related issues easily. if retry > (MAX_RETRIES - forcedConflicts): raise ConflictError self._endRequestSpecific2RH( True ) # I.e. implemented by Room Booking request handlers DBMgr.getInstance().endRequest( True ) Logger.get('requestHandler').info('Request %s successful' % (id(self._req))) #request succesfull, now, doing tas that must be done only once try: GenericMailer.flushQueue(True) # send emails self._deleteTempFiles() except: Logger.get('mail').exception('Mail sending operation failed') pass break except MaKaCError, e: #DBMgr.getInstance().endRequest(False) res = self._processError(e) except (ConflictError, POSKeyError): import traceback # only log conflict if it wasn't forced if retry <= (MAX_RETRIES - forcedConflicts): Logger.get('requestHandler').warning('Conflict in Database! (Request %s)\n%s' % (id(self._req), traceback.format_exc())) self._abortSpecific2RH() DBMgr.getInstance().abort() retry -= 1 continue except ClientDisconnected: Logger.get('requestHandler').warning('Client Disconnected! (Request %s)' % id(self._req) ) self._abortSpecific2RH() DBMgr.getInstance().abort() retry -= 1 time.sleep(10-retry) continue except KeyAccessError, e: #Key Access error treatment res = self._processKeyAccessError( e ) self._endRequestSpecific2RH( False ) DBMgr.getInstance().endRequest(False)
for i in range(n): s += i for i in xrange(n): s += i for i in range(n): s *= i for i in xrange(n): s *= i return s # print calculate(10000) # [1] # profile.run(statement='calculate(10000)') # [2] profile.runctx(statement='calculate(10000)', globals=globals(), locals=locals()) """ 6 function calls in 0.003 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.002 0.002 0.002 0.002 00_profile_demo.py:26(calculate) 2 0.000 0.000 0.000 0.000 :0(range) 1 0.000 0.000 0.000 0.000 :0(setprofile) 1 0.000 0.000 0.002 0.002 <string>:1(<module>) 1 0.000 0.000 0.003 0.003 profile:0(calculate(10000)) 0 0.000 0.000 profile:0(profiler) """
# encoding:utf-8 import profile from profile_fibonacci_memoized import fib, fib_seq if __name__ == '__main__': profile.runctx('print fib_seq(n); print', globals(), {'n': 20})
# profile_runctx.py import profile from profile_fibonacci_memoized import fib, fib_seq if __name__ == '__main__': profile.runctx( 'print(fib_seq(n)); print()', globals(), {'n': 20}, )