class ManagerProfilePyinstrument(ManagerProfile): def __init__(self, sync: bool): self.sync = sync self.profiler = Profiler() def start(self): self.profiler.start() return nullcontext() def stop_and_write(self, path_profile: str, is_docker: bool, api: str, render_browser: bool = False): self.profiler.stop() mode = "sync" if self.sync else "async" filename = f"pyinstrument_profile_{mode}_{api}.html" if not is_docker: output_html = self.profiler.output_html() self._write_output_file(path_profile, output_html, filename=filename) if render_browser: self.profiler.open_in_browser() print(self.profiler.output_text(unicode=True, color=True)) def _write_output_file(self, path_profile: str, output_html: str, filename: str): output_html_path = self._prepare_output_path(path_profile, filename) with open(output_html_path, "w") as file: file.write(output_html)
def instrument(html_output=False): """Run a statistical profiler""" try: from pyinstrument import Profiler # pylint: disable=import-error except ImportError: print("Failed to run profiler, pyinstrument is not installed") yield return profiler = Profiler() profiler.start() yield profiler.stop() if html_output: profiler.open_in_browser() return print(profiler.output_text(unicode=True, color=True))
# Power spectra # ------------- if TEST_POWERSPECTRA: if state.rb_sig_I.is_full: fftw_PS_sig_I.compute_spectrum_dB(state.rb_sig_I) if state.rb_filt_I.is_full: fftw_PS_filt_I.compute_spectrum_dB(state.rb_filt_I) if state.rb_mix_X.is_full: fftw_PS_mix_X.compute_spectrum_dB(state.rb_mix_X) if state.rb_mix_Y.is_full: fftw_PS_mix_Y.compute_spectrum_dB(state.rb_mix_Y) if state.rb_R.is_full: fftw_PS_R.compute_spectrum_dB(state.rb_R) if RUN_PYINSTRUMENT: profiler.stop() profiler.open_in_browser() # print(profiler.output_text(unicode=True, color=True)) print("%5.3f %5.3f" % (state.sig_I_avg, state.filt_I_avg)) print("%5.3f %5.3f" % (state.R_avg, state.T_avg)) tock = Time.perf_counter() print("Number of blocks simulated: %i" % N_sim_blocks) print("Avg time per block: %.1f ms" % ((tock - tick) / N_sim_blocks * 1000))