def _profile(func, config, *arguments, **kwargs): context_tuple = (func, arguments, kwargs) logging.info('Now running the actual function....') try: stats = runner.run_profilers(context_tuple, config, verbose=True) except runner.AmbiguousConfigurationError: logging.error('Profiler configuration %s is ambiguous. Please, remove' 'duplicates.' % config) sys.exit(_ERR_CODES['ambiguous_configuration']) except runner.BadOptionError as exc: logging.error(exc) sys.exit(_ERR_CODES['bad_option']) return stats
def run(self): log = get_logger(self.name) log.info("---> {} Starting --->".format(self.name)) profiling = os.getenv('PROFILING', '') vprof_options = set('cmph').intersection(set(profiling)) testname = os.getenv('TEST_NAME', 'sample_test') testid = os.getenv('TEST_ID', '') nodename = os.getenv('HOST_NAME', 'node') profdir = os.path.join('profiles', testname, testid, nodename) profpath = os.path.join(profdir, self.name) try: # First create this directory for profiling if not os.path.exists(profdir) and profiling: os.makedirs(profdir) # Profiling with gprof to create Node graph profile if 'n' in profiling: pr = cProfile.Profile() pr.enable() super().run() pr.create_stats() pr.dump_stats('{}.stats'.format(profpath)) # Profiling with vprof to create visualizations as specified in vprof_options elif len(vprof_options) > 0: vprof_options = ''.join(vprof_options) run_stats = runner.run_profilers((super().run, [], {}), vprof_options) with open('{}.json'.format(profpath), 'w+') as f: run_stats['version'] = pkg_resources.get_distribution( "vprof").version f.write(json.dumps(run_stats)) else: super().run() except Exception as e: err_msg = '\n' + OUTER_DELIM * DELIM_LEN err_msg += '\nException caught on ' + self.name + ':\n' + str(e) err_msg += '\n' + INNER_DELIM * DELIM_LEN err_msg += '\n' + traceback.format_exc() err_msg += '\n' + INNER_DELIM * DELIM_LEN err_msg += '\n' + OUTER_DELIM * DELIM_LEN log.error(err_msg) finally: log.info("<--- {} Terminating <---".format(self.name))
def main(): """Main function of the module.""" parser = argparse.ArgumentParser( prog=_PROGRAN_NAME, description=_MODULE_DESC, formatter_class=argparse.RawTextHelpFormatter) launch_modes = parser.add_mutually_exclusive_group(required=True) launch_modes.add_argument('-r', '--remote', dest='remote', action='store_true', default=False, help='launch in remote mode') launch_modes.add_argument('-i', '--input-file', dest='input_file', type=str, default='', help='render UI from file') launch_modes.add_argument('-c', '--config', nargs=2, dest='config', help=_CONFIG_DESC, metavar=('CONFIG', 'SRC')) parser.add_argument('-H', '--host', dest='host', default=_HOST, type=str, help='set internal webserver host') parser.add_argument('-p', '--port', dest='port', default=_PORT, type=int, help='set internal webserver port') parser.add_argument('-n', '--no-browser', dest='dont_start_browser', action='store_true', default=False, help="don't start browser automatically") parser.add_argument('-o', '--output-file', dest='output_file', type=str, default='', help='save profile to file') parser.add_argument('--debug', dest='debug_mode', action='store_true', default=False, help="don't suppress error messages") parser.add_argument('--version', action='version', version='vprof %s' % __version__) args = parser.parse_args() # Render UI from file. if args.input_file: with open(args.input_file) as ifile: saved_stats = json.loads(ifile.read()) if saved_stats['version'] != __version__: print('Incorrect profiler version - %s. %s is required.' % (saved_stats['version'], __version__)) sys.exit(_ERR_CODES['input_file_error']) stats_server.start(args.host, args.port, saved_stats, args.dont_start_browser, args.debug_mode) # Launch in remote mode. elif args.remote: stats_server.start(args.host, args.port, {}, args.dont_start_browser, args.debug_mode) # Profiler mode. else: config, source = args.config try: program_stats = runner.run_profilers(source, config, verbose=True) except runner.AmbiguousConfigurationError: print('Profiler configuration %s is ambiguous. ' 'Please, remove duplicates.' % config) sys.exit(_ERR_CODES['ambiguous_configuration']) except runner.BadOptionError as exc: print(exc) sys.exit(_ERR_CODES['bad_option']) if args.output_file: with open(args.output_file, 'w') as outfile: program_stats['version'] = __version__ outfile.write(json.dumps(program_stats, indent=2)) else: stats_server.start(args.host, args.port, program_stats, args.dont_start_browser, args.debug_mode)
def main(): """Visual profiler main function.""" parser = argparse.ArgumentParser( prog=_PROGRAN_NAME, description=_MODULE_DESC, formatter_class=argparse.RawTextHelpFormatter) launch_modes = parser.add_mutually_exclusive_group(required=True) launch_modes.add_argument('-r', '--remote', dest='remote', action='store_true', default=False, help='launch in remote mode') launch_modes.add_argument('-i', '--input-file', dest='input_file', type=str, default='', help='render visualization from file') launch_modes.add_argument('-c', '--config', nargs=2, dest='config', help=_CONFIG_DESC, metavar=('CONFIG', 'SRC')) parser.add_argument('-H', '--host', dest='host', default=_HOST, type=str, help='set internal webserver host') parser.add_argument('-p', '--port', dest='port', default=_PORT, type=int, help='set internal webserver port') parser.add_argument('-n', '--no-browser', dest='dont_start_browser', action='store_true', default=False, help="don't start browser automatically") parser.add_argument('-o', '--output-file', dest='output_file', type=str, default='', help='save profile to file') parser.add_argument('--debug', dest='debug_mode', action='store_true', default=False, help="don't suppress error messages") parser.add_argument('--version', action='version', version='vprof %s' % __version__) args = parser.parse_args() # Render visualizations from saved file. if args.input_file: with open(args.input_file) as ifile: saved_stats = json.loads(ifile.read()) if saved_stats['version'] != __version__: print('Incorrect profile version - %s. %s is required.' % ( saved_stats['version'], __version__)) sys.exit(_ERR_CODES['input_file_error']) stats_server.start(args.host, args.port, saved_stats, args.dont_start_browser, args.debug_mode) # Start in remote mode. elif args.remote: stats_server.start(args.host, args.port, {}, args.dont_start_browser, args.debug_mode) # Profiler mode. else: config, source = args.config try: program_stats = runner.run_profilers( # pylint: disable=redefined-variable-type source, config, verbose=True) except runner.AmbiguousConfigurationError: print('Profiler configuration %s is ambiguous. ' 'Please, remove duplicates.' % config) sys.exit(_ERR_CODES['ambiguous_configuration']) except runner.BadOptionError as exc: print(exc) sys.exit(_ERR_CODES['bad_option']) if args.output_file: with open(args.output_file, 'w') as outfile: program_stats['version'] = __version__ outfile.write(json.dumps(program_stats, indent=2)) else: stats_server.start( args.host, args.port, program_stats, args.dont_start_browser, args.debug_mode)