def process_response(self, request, response): if hasattr(request, 'profiler'): try: request.profiler.stop() output_html = request.profiler.output_html() profile_dir = getattr(settings, 'PYINSTRUMENT_PROFILE_DIR', None) if profile_dir: filename = '{total_time:.3f}s {path} {timestamp:.0f}.html'.format( total_time=request.profiler.root_frame().time(), path=request.get_full_path().replace('/', '_'), timestamp=time.time()) file_path = os.path.join(profile_dir, filename) if not os.path.exists(profile_dir): os.mkdir(profile_dir) with open(file_path, 'w') as f: f.write(output_html) if getattr(settings, 'PYINSTRUMENT_URL_ARGUMENT', 'profile') in request.GET: return HttpResponse(output_html) else: return response except NotMainThreadError: raise NotMainThreadError(not_main_thread_message) else: return response
def process_request(self, request): if getattr(settings, 'PYINSTRUMENT_URL_ARGUMENT', 'profile') in request.GET: self.profiler = Profiler() try: self.profiler.start() except NotMainThreadError: raise NotMainThreadError(not_main_thread_message) self.profiler = None
def stop(self): if self.use_signal: signal.setitimer(signal.ITIMER_REAL, 0.0, 0.0) try: signal.signal(signal.SIGALRM, signal.SIG_IGN) except ValueError: raise NotMainThreadError() else: sys.setprofile(None)
def process_request(self, request): profile_dir = getattr(settings, 'PYINSTRUMENT_PROFILE_DIR', None) use_signal = getattr(settings, 'PYINSTRUMENT_USE_SIGNAL', True) if getattr(settings, 'PYINSTRUMENT_URL_ARGUMENT', 'profile') in request.GET or profile_dir: profiler = Profiler(use_signal=use_signal) try: profiler.start() request.profiler = profiler except NotMainThreadError: raise NotMainThreadError(not_main_thread_message)
def process_response(self, request, response): if self.profiler: try: self.profiler.stop() return HttpResponse(self.profiler.output_html()) except NotMainThreadError: raise NotMainThreadError(not_main_thread_message) finally: self.profiler = None else: return response
def start(self): self.last_profile_time = timer() if self.use_signal: try: signal.signal(signal.SIGALRM, self._signal) except ValueError: raise NotMainThreadError() signal.setitimer(signal.ITIMER_REAL, self.interval, 0.0) else: sys.setprofile(self._profile)