Exemplo n.º 1
0
def raw_sql_profiler():
    profiler = Profile()
    for _ in range(10):
        profiler.runctx(
            "list(HttcOrder.objects.raw('''SELECT * FROM httc_order WHERE mk_contract_id = 1000 ORDER BY price'''))",
            locals(), globals())
    convert(profiler.getstats(), 'raw_sql_profiler.kgrind')
Exemplo n.º 2
0
def profile(file_base, func):
    profiler = cProfile.Profile()
    ret = profiler.runcall(func)
    profile_path = os.path.join(TEST_OPTS.TMP_DIR,
                                'callgrind.out.' + file_base)
    pyprof2calltree.convert(profiler.getstats(), profile_path)
    return ret
Exemplo n.º 3
0
 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))
Exemplo n.º 4
0
def profile(proc, func, car='toyota'):
  segment, fingerprint = CARS[car]
  segment = segment.replace('|', '/')
  rlog_url = f"{BASE_URL}{segment}/rlog.bz2"
  msgs = list(LogReader(rlog_url)) * int(os.getenv("LOOP", "1"))

  os.environ['FINGERPRINT'] = fingerprint

  def run(sm, pm, can_sock):
    try:
      if can_sock is not None:
        func(sm, pm, can_sock)
      else:
        func(sm, pm)
    except ReplayDone:
      pass

  # Statistical
  sm, pm, can_sock = get_inputs(msgs, proc, fingerprint)
  with pprofile.StatisticalProfile()(period=0.00001) as pr:
    run(sm, pm, can_sock)
  pr.dump_stats(f'cachegrind.out.{proc}_statistical')

  # Deterministic
  sm, pm, can_sock = get_inputs(msgs, proc, fingerprint)
  with cProfile.Profile() as pr:
    run(sm, pm, can_sock)
  pyprof2calltree.convert(pr.getstats(), f'cachegrind.out.{proc}_deterministic')
Exemplo n.º 5
0
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')
Exemplo n.º 6
0
def _write_profile(profiler, cprofile_path, callgrind_path):
    # Imports deferred to avoid loading modules
    # in memory since usually only one part of this
    # integration is used at a time
    from pyprof2calltree import convert  # pylint: disable=import-outside-toplevel

    profiler.create_stats()
    profiler.dump_stats(cprofile_path)
    convert(profiler.getstats(), callgrind_path)
Exemplo n.º 7
0
def ViewProfile(path: str):
    """生成QCacheGrind可视化的文件
    Arguments:
        path {str} -- 保存路径
    """
    global g_Profile
    path += ".view"
    # visualize(g_Profile.getstats())
    convert(g_Profile.getstats(), path)
Exemplo n.º 8
0
def profile_create_offices_for_departement(departement):
    """
    Run create_offices_for_departement with profiling.
    """
    profiler = Profile()
    command = "create_offices_for_departement('%s')" % departement
    profiler.runctx(command, locals(), globals())
    relative_filename = 'profiling_results/create_index_dpt%s.kgrind' % departement
    filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                            relative_filename)
    convert(profiler.getstats(), filename)
Exemplo n.º 9
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        if not request.REQUEST.get('profile'):
            return view_func(request, *view_args, **view_kwargs)

        profile = cProfile.Profile()

        result = profile.runcall(view_func, request, *view_args, **view_kwargs)

        short_url = request.path.replace('/footprint/api/v1/', '').replace('//', '/').replace('/', '_')
        pyprof2calltree.convert(profile.getstats(), 'profile_%s_%s.kgrind' % (short_url, int(time.time())))

        return result
Exemplo n.º 10
0
 def disable(self):
     """Disaables profiling"""
     self.cprofile.disable()
     strIO = StringIO.StringIO()
     sortby = 'cumulative'
     ps = pstats.Stats(self.cprofile, stream=strIO).sort_stats(sortby)
     LOGGER.info('Profiler disabled, data written to %s', self.path)
     from xpedite.dependencies import Package, DEPENDENCY_LOADER
     if DEPENDENCY_LOADER.load(Package.PyProf2Calltree):  # pylint: disable=no-member
         from pyprof2calltree import convert  # pylint: disable=import-error
         convert(ps, self.path)
         LOGGER.info(
             'Open the report in KCachegrind to see the profile report')
Exemplo n.º 11
0
def profile_code():

    pr = cProfile.Profile()
    pr.enable()

    with TemporaryDirectory() as tmpdir:
        run_analysis(tmpdir)

    pr.disable()

    visualize(pr.getstats())                            # run kcachegrind
    convert(pr.getstats(), 'profiling_results.kgrind')  # save for later

    assert 0
Exemplo n.º 12
0
def run_subcommand_crawl(opts):
    setup_logging(
        network_logs=opts.network_logs)  #, control_logs=opts.control_logs)
    cls = get_crawler(opts.crawler_id)
    extra_data = {}
    for key in cls.extra_cli_args():
        opt_key = 'extra_%s' % key.replace('-', '_')
        extra_data[key] = getattr(opts, opt_key)
    bot = cls(
        network_threads=opts.network_threads,
        extra_data=extra_data,
    )
    try:
        if opts.profile:
            import cProfile
            import pyprof2calltree
            import pstats

            profile_file = 'var/%s.prof' % opts.crawler_id
            profile_tree_file = 'var/%s.prof.out' % opts.crawler_id

            prof = cProfile.Profile()
            try:
                prof.runctx('bot.run()', globals(), locals())
            finally:
                stats = pstats.Stats(prof)
                stats.strip_dirs()
                pyprof2calltree.convert(stats, profile_tree_file)
        else:
            bot.run()
    except KeyboardInterrupt:
        bot.fatal_error_happened.set()
    print('Stats:')
    for key, val in sorted(bot.stat.total_counters.items()):
        print(' * %s: %s' % (key, val))
    if bot._run_started:
        print('Elapsed: %s' %
              format_elapsed_time(time.time() - bot._run_started))
    else:
        print('Elapsed: NA')
    if bot.fatal_error_happened.is_set():
        sys.exit(1)
    else:
        sys.exit(0)
Exemplo n.º 13
0
def update_data_profiling_wrapper(create_full,
                                  create_partial,
                                  disable_parallel_computing=False):
    if Profiling.ACTIVATED:
        logger.info("STARTED run with profiling")
        profiler = Profile()
        profiler.runctx(
            "update_data(create_full, create_partial, disable_parallel_computing)",
            locals(), globals())
        relative_filename = 'profiling_results/create_index_run.kgrind'
        filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                relative_filename)
        convert(profiler.getstats(), filename)
        logger.info(
            "COMPLETED run with profiling: exported profiling result as %s",
            filename)
    else:
        logger.info("STARTED run without profiling")
        update_data(create_full, create_partial, disable_parallel_computing)
        logger.info("COMPLETED run without profiling")
Exemplo n.º 14
0
        def wrapper(*args, **kwargs):
            prof = None

            # -- BEFORE WRAP --
            if file is not None:
                prof = cProfile.Profile()
                prof.enable()
            # -- BEFORE WRAP --

            f = func(*args, **kwargs)

            # -- AFTER WRAP --
            if file is not None:
                prof.disable()
                prof.dump_stats(file)
                stats = prof.getstats()
                # pyprof2calltree.visualize(stats)
                pyprof2calltree.convert(stats, f'qcachegrind_{file}')
            # -- AFTER WRAP --

            return f
Exemplo n.º 15
0
def runctx(cmdstr, globals={}, locals={}, outpath=None, executable=None):
    tmp = tempfile.mktemp()
    if outpath is not None:
        path = os.path.splitext(outpath)[0] + '.callgrind'
        dirpath = os.path.dirname(path)
        if not fileutil.exists(dirpath):
            os.makedirs(dirpath.replace('\\', '/'))

        cProfile.runctx(cmdstr, globals, locals, filename=tmp)
        pyprof2calltree.convert(pstats.Stats(tmp), path)

        if executable is not None:
            subprocess.Popen([executable, path])
        os.unlink(tmp)
        return path

    cProfile.runctx(cmdstr, globals, locals, filename=tmp)
    pyprof2calltree.convert(pstats.Stats(tmp), tmp)
    if executable is not None:
        subprocess.Popen([executable, tmp])
    return tmp
Exemplo n.º 16
0
def main():
    N = 100000  # Number of tests

    # Different argument types
    variants = [
        (1, 3),
        ((1, 3), ),
        ("fox", ),
    ]

    args = []

    for i in range(N):
        args.append(variants[randint(0, len(variants) - 1)])

    def run_ovl():
        for arg in args:
            func_ovl(*arg)

    def run_normal():
        for arg in args:
            func_normal(*arg)

    print("Running benchmark...")

    time_ovl = timeit.timeit(run_ovl, number=1) / N
    time_normal = timeit.timeit(run_normal, number=1) / N

    profiler = Profile()
    profiler.runctx("run_ovl()", globals(), locals())
    convert(profiler.getstats(),
            "C:/Users/andreasxp/Desktop/callgrind.profile")

    print(f"Average over {N} runs:")
    print(
        f"Overloaded function:     {time_ovl * 1000000:.2f} mcs ({time_ovl / time_normal:.2f}x)"
    )
    print(f"Non-overloaded function: {time_normal * 1000000:.2f} mcs")
Exemplo n.º 17
0
def runctx(cmdstr, globals={}, locals={}, outpath=None, executable=None):
    tmp = tempfile.mktemp()
    target = tmp

    # profile to a file
    if outpath is not None:
        target = fileutil.FilePath(outpath).ensureExt('callgrind')

        # ensure out folder exists
        target.parent().ensureExists()

    # profile into out file
    cProfile.runctx(cmdstr, globals, locals, filename=tmp)
    pyprof2calltree.convert(pstats.Stats(tmp), target)

    # open
    if executable is not None:
        subprocess.Popen([executable, target])

    # clean up & return result
    if tmp != target:
        os.unlink(tmp)
    return target
Exemplo n.º 18
0
def main():
    setup_logging()
    parser = OptionParser()
    parser.add_option('-t', '--test', help='Run only specified tests')
    parser.add_option('--grab-transport', default='pycurl')
    parser.add_option('--network-service', default='threaded')
    parser.add_option('--test-grab', action='store_true',
                      default=False, help='Run tests for Grab::Spider')
    parser.add_option('--test-spider', action='store_true',
                      default=False, help='Run tests for Grab')
    parser.add_option('--test-all', action='store_true',
                      default=False,
                      help='Run tests for both Grab and Grab::Spider')
    parser.add_option('--backend-mongodb', action='store_true',
                      default=False,
                      help='Run extra tests that depends on mongodb')
    parser.add_option('--backend-redis', action='store_true',
                      default=False,
                      help='Run extra tests that depends on redis')
    parser.add_option('--backend-mysql', action='store_true',
                      default=False,
                      help='Run extra tests that depends on mysql')
    parser.add_option('--backend-postgresql', action='store_true',
                      default=False,
                      help='Run extra tests that depends on postgresql')
    parser.add_option('--mp-mode', action='store_true', default=False,
                      help='Enable multiprocess mode in spider tests')
    parser.add_option('--profile', action='store_true', default=False,
                      help='Do profiling')
    parser.add_option('-v', '--verbose', action='store_true', default=False,
                      help='Enable verbose logging')
    opts, _ = parser.parse_args()

    GLOBAL['grab_transport'] = opts.grab_transport
    GLOBAL['network_service'] = opts.network_service

    if opts.backend_mongodb:
        GLOBAL['backends'].append('mongodb')

    if opts.backend_redis:
        GLOBAL['backends'].append('redis')

    if opts.backend_mysql:
        GLOBAL['backends'].append('mysql')

    if opts.backend_postgresql:
        GLOBAL['backends'].append('postgresql')

    test_list = []

    if opts.test_all:
        test_list += GRAB_TEST_LIST
        test_list += SPIDER_TEST_LIST

    if opts.test_grab:
        test_list += GRAB_TEST_LIST

    if opts.test_spider:
        test_list += SPIDER_TEST_LIST

    if opts.test:
        test_list += [opts.test]

    if opts.verbose:
        from grab.spider.base import logger_verbose
        logger_verbose.setLevel(logging.DEBUG)

    GLOBAL['mp_mode'] = opts.mp_mode

    # Check tests integrity
    # Ensure that all test modules are imported correctly
    for path in test_list:
        __import__(path, None, None, ['foo'])

    loader = unittest.TestLoader()
    suite = unittest.TestSuite()
    for path in test_list:
        mod_suite = loader.loadTestsFromName(path)
        for some_suite in mod_suite:
            for test in some_suite:
                if (not hasattr(test, 'backend') or
                        test.backend in GLOBAL['backends']):
                    suite.addTest(test)

    runner = unittest.TextTestRunner()

    if opts.profile:
        import cProfile
        import pyprof2calltree
        import pstats

        profile_tree_file = 'var/test.prof.out'
        prof = cProfile.Profile()
        result = prof.runcall(runner.run, suite)
        stats = pstats.Stats(prof)
        stats.strip_dirs()
        pyprof2calltree.convert(stats, profile_tree_file)
    else:
        result = runner.run(suite)

    th_list = list(threading.enumerate())
    print('Active threads (%d):' % len(th_list))
    for th in th_list:
        print('Thread: %s (isAlive:%s)' % (th, th.isAlive()))

    if result.wasSuccessful():
        sys.exit(0)
    else:
        sys.exit(1)
Exemplo n.º 19
0
def process_command_line():
    # Add current directory to python path
    cur_dir = os.path.realpath(os.getcwd())
    sys.path.insert(0, cur_dir)

    parser = ArgumentParser()
    parser.add_argument('action', type=str)
    parser.add_argument('--logging-level', default='debug')
    parser.add_argument('--lock-key')
    #parser.add_argument('--ignore-lock', action='store_true', default=False)
    parser.add_argument('--settings', type=str, default='settings')
    parser.add_argument('--env', type=str)
    parser.add_argument('--profile', action='store_true', default=False)

    args, trash = parser.parse_known_args()

    config = build_root_config()
    if config and config['GRAB_DJANGO_SETTINGS']:
        os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
        # Turn off DEBUG to prevent memory leaks
        from django.conf import settings
        settings.DEBUG = False

    # Setup logging
    logging_level = getattr(logging, args.logging_level.upper())
    #if args.positional_args:
    #command_key = '_'.join([args.action] + args.positional_args)
    #else:
    #command_key = args.action
    # TODO: enable logs
    setup_logging(args.action, logging_level, clear_handlers=True)

    # Setup action handler
    action_name = args.action
    try:
        # First, try to import script from the grab package
        action_mod = __import__('grab.script.%s' % action_name, None, None,
                                ['foo'])
    except ImportError as ex:
        if (unicode(ex).startswith('No module named')
                and action_name in unicode(ex)):
            pass
        else:
            logging.error('', exc_info=ex)
        # If grab does not provides the script
        # try to import it from the current project
        try:
            action_mod = __import__('script.%s' % action_name, None, None,
                                    ['foo'])
        except ImportError as ex:
            logging.error('', exc_info=ex)
            sys.stderr.write('Could not import %s script' % action_name)
            sys.exit(1)

    if hasattr(action_mod, 'setup_arg_parser'):
        action_mod.setup_arg_parser(parser)
    args, trash = parser.parse_known_args()

    # TODO: enable lock-file processing
    #lock_key = None
    #if not args.slave:
    #if not args.ignore_lock:
    #if not args.lock_key:
    #if hasattr(action_mod, 'setup_lock_key'):
    #lock_key = action_mod.setup_lock_key(action_name, args)
    #else:
    #lock_key = command_key
    #else:
    #lock_key = args.lock_key
    #if lock_key is not None:
    #lock_path = 'var/run/%s.lock' % lock_key
    #print 'Trying to lock file: %s' % lock_path
    #assert_lock(lock_path)

    #logger.debug('Executing %s action' % action_name)
    try:
        if args.profile:
            import cProfile
            import pyprof2calltree
            import pstats

            profile_file = 'var/%s.prof' % action_name
            profile_tree_file = 'var/%s.prof.out' % action_name

            prof = cProfile.Profile()
            prof.runctx('action_mod.main(**vars(args))', globals(), locals())
            stats = pstats.Stats(prof)
            stats.strip_dirs()
            pyprof2calltree.convert(stats, profile_tree_file)
        else:
            action_mod.main(**vars(args))
    except Exception as ex:
        logging.error('Unexpected exception from action handler:', exc_info=ex)
Exemplo n.º 20
0
def main():
    logging.basicConfig(level=logging.DEBUG)
    parser = OptionParser()
    parser.add_option('-t', '--test', help='Run only specified tests')
    parser.add_option('--transport', default='pycurl')
    parser.add_option('--test-grab', action='store_true',
                      default=False, help='Run tests for Grab::Spider')
    parser.add_option('--test-spider', action='store_true',
                      default=False, help='Run tests for Grab')
    parser.add_option('--test-all', action='store_true',
                      default=False,
                      help='Run tests for both Grab and Grab::Spider')
    parser.add_option('--backend-mongo', action='store_true',
                      default=False,
                      help='Run extra tests that depends on mongodb')
    parser.add_option('--backend-redis', action='store_true',
                      default=False,
                      help='Run extra tests that depends on redis')
    parser.add_option('--backend-mysql', action='store_true',
                      default=False,
                      help='Run extra tests that depends on mysql')
    parser.add_option('--backend-postgresql', action='store_true',
                      default=False,
                      help='Run extra tests that depends on postgresql')
    parser.add_option('--mp-mode', action='store_true', default=False,
                      help='Enable multiprocess mode in spider tests')
    parser.add_option('--profile', action='store_true', default=False,
                      help='Do profiling')
    opts, args = parser.parse_args()

    GLOBAL['transport'] = opts.transport

    if opts.backend_mongo:
        GLOBAL['backends'].append('mongo')

    if opts.backend_redis:
        GLOBAL['backends'].append('redis')

    if opts.backend_mysql:
        GLOBAL['backends'].append('mysql')

    if opts.backend_postgresql:
        GLOBAL['backends'].append('postgresql')

    prepare_test_environment()
    test_list = []

    if opts.test_all:
        test_list += GRAB_TEST_LIST
        test_list += SPIDER_TEST_LIST

    if opts.test_grab:
        test_list += GRAB_TEST_LIST

    if opts.test_spider:
        test_list += SPIDER_TEST_LIST

    if opts.test:
        test_list += [opts.test]

    GLOBAL['mp_mode'] = opts.mp_mode

    # Check tests integrity
    # Ensure that all test modules are imported correctly
    for path in test_list:
        __import__(path, None, None, ['foo'])

    loader = unittest.TestLoader()
    suite = unittest.TestSuite()
    for path in test_list:
        mod_suite = loader.loadTestsFromName(path)
        for some_suite in mod_suite:
            for test in some_suite:
                if (not hasattr(test, '_backend') or
                        test._backend in GLOBAL['backends']):
                    suite.addTest(test)

    runner = unittest.TextTestRunner()
    #start_server()


    if opts.profile:
        import cProfile
        import pyprof2calltree
        import pstats

        profile_tree_file = 'var/test.prof.out'
        prof = cProfile.Profile()
        result = prof.runcall(runner.run, suite)
        stats = pstats.Stats(prof)
        stats.strip_dirs()
        pyprof2calltree.convert(stats, profile_tree_file)
    else:
        result = runner.run(suite)
    clear_test_environment()
    if result.wasSuccessful():
        sys.exit(0)
    else:
        sys.exit(1)
Exemplo n.º 21
0
         "../testdata/NCAc070874.fits",
         "../testdata/NCAc070875.fits",
         "../testdata/NCAc070876.fits",
         "../testdata/NCAc070877.fits",
         "../testdata/NCAc070878.fits", "../testdata/NCAc070879.fits", "../testdata/NCAc070880.fits",
         "../testdata/NCAc070881.fits", "../testdata/NCAc070882.fits", "../testdata/NCAc070883.fits",
         "../testdata/NCAc070884.fits",
         "../testdata/NCAc070885.fits", "../testdata/NCAc070886.fits", "../testdata/NCAc070887.fits",
         "../testdata/NCAc070888.fits",
         "../testdata/NCAc070889.fits", "../testdata/NCAc070890.fits",
         "../testdata/NCAc070891.fits"],
        output="")

    pr.disable()
    pr.dump_stats('multicore.stats')
    convert(pr.getstats(), 'multicore.kgrind')

if single or both:
    prsingle = cProfile.Profile()
    prsingle.enable()

    ir_reduce.Pool = ir_reduce.PoolDummy
    ir_reduce.do_everything.__globals__['Pool'] = ir_reduce.PoolDummy

    ir_reduce.do_everything(
        ['../testdata/bad_cold5.fits', '../testdata/bad_hot2.fits', '../testdata/bad_zero_sci.fits'],
        ['../testdata/FlatJ.fits'],
        ["../testdata/NCAc070865.fits", "../testdata/NCAc070866.fits", "../testdata/NCAc070867.fits",
         "../testdata/NCAc070868.fits",
         "../testdata/NCAc070869.fits",
         "../testdata/NCAc070870.fits",
Exemplo n.º 22
0
import logging
from cProfile import Profile
from atpase import atpase, bgt

FILENAME = "atpase_profile.kgrind"
LOG = "profile.log"
profiler = Profile()
logger = bgt.logger
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename=LOG, mode='w')
logger.addHandler(handler)

try:
    profiler.runcall(atpase)
except KeyboardInterrupt as ex:
    handler.flush()
    raise ex
from pyprof2calltree import convert, visualize

convert(profiler.getstats(), FILENAME)
visualize(profiler.getstats())
Exemplo n.º 23
0
    sm = SubMaster(msgs, trigger, sub_socks)
    pm = PubMaster()
    return sm, pm


if __name__ == "__main__":
    segment, fingerprint = CARS['toyota']
    segment = segment.replace('|', '/')
    rlog_url = f"{BASE_URL}{segment}/rlog.bz2"
    msgs = list(LogReader(rlog_url))

    # Statistical
    sm, pm = get_inputs(msgs, 'locationd')
    with pprofile.StatisticalProfile()(period=0.00001) as pr:
        try:
            locationd_thread(sm, pm)
        except ReplayDone:
            pass
    pr.dump_stats('cachegrind.out.locationd_statistical')

    # Deterministic
    sm, pm = get_inputs(msgs, 'controlsd')
    with cProfile.Profile() as pr:
        try:
            locationd_thread(sm, pm)
        except ReplayDone:
            pass
    pyprof2calltree.convert(pr.getstats(),
                            'cachegrind.out.locationd_deterministic')
Exemplo n.º 24
0
        "C_lambda": C_LAMBDA_TRUE,
        "delta_S": DELTA_S_TRUE
    }

    price_path = preisSim(param_true)

    # p_true_SS = all_summary_stats(price_path, price_path)

    return (price_path)


p_true_SS = main()
p_true_SS.to_csv("new_test_case.csv", index=False, header=False)

original = pd.read_csv("original_test_case.csv", header=None)

new_case = pd.read_csv("new_test_case.csv", header=None)

print(
    all(
        round(original.apply(float, axis=1), 0) == round(
            new_case.apply(float, axis=1), 0)))

profiler = Profile()
profiler.runctx("main()", locals(), globals())
#
from pyprof2calltree import convert, visualize

visualize(profiler.getstats())  # run kcachegrind
convert(profiler.getstats(), 'profiling_results.kgrind')  # save for later
Exemplo n.º 25
0
    def profiled_handler(*args, **kwargs):

        global _request_id

        # Acquire a unique identifier for the request
        with _lock:
            _request_id += 1
            id = _request_id

        name = cherrypy.request.path_info.strip("/").replace("/", "-")
        name += "." + str(id)

        # Create the execution context for the profiler
        local_context = {
            "handler": handler,
            "args": args,
            "kwargs": kwargs,
            "rvalue": None
        }

        # Run the handler for the current request through the profiler.
        # Profile data is either shown on standard output or stored on the
        # indicated file.
        stats_file = join(stats_path, "%s.stats" %
                          name) if stats_path else None

        try:
            runctx("rvalue = handler(*args, **kwargs)", globals(),
                   local_context, stats_file)
        finally:

            # If pyprof2calltree is available, use it to export the profiler stats
            # from Python's own format to 'calltree' (which can be parsed by
            # kcachegrind and others)
            calltree_file = None

            if stats_file is not None and convert is not None:
                calltree_file = join(stats_path, "%s.calltree" % name)
                convert(stats_file, calltree_file)

            if profiler_action == "view":

                if not viewer:
                    raise ValueError(
                        "No value defined for the "
                        "tools.handler_profiler.viewer setting; can't use the "
                        "'view' profiler action")

                cmd = shlex.split(viewer % {
                    "stats": stats_file,
                    "calltree": calltree_file
                })
                env = os.environ.copy()
                env.setdefault("DISPLAY", ":0")
                proc = Popen(cmd, env=env)

            if profiler_action == "download":
                return serve_file(calltree_file or stats_file,
                                  "application/octet-stream", "attachment")

        return local_context["rvalue"]
Exemplo n.º 26
0
    can_sock = SubSocket(msgs, 'can')
    return sm, pm, can_sock


if __name__ == "__main__":
    segment, fingerprint = CARS['toyota']
    segment = segment.replace('|', '/')
    rlog_url = f"{BASE_URL}{segment}/rlog.bz2"
    msgs = list(LogReader(rlog_url))

    os.environ['FINGERPRINT'] = fingerprint

    # Statistical
    sm, pm, can_sock = get_inputs(msgs, 'controlsd')
    with pprofile.StatisticalProfile()(period=0.00001) as pr:
        try:
            controlsd_thread(sm, pm, can_sock)
        except ReplayDone:
            pass
    pr.dump_stats('cachegrind.out.controlsd_statistical')

    # Deterministic
    sm, pm, can_sock = get_inputs(msgs, 'controlsd')
    with cProfile.Profile() as pr:
        try:
            controlsd_thread(sm, pm, can_sock)
        except ReplayDone:
            pass
    pyprof2calltree.convert(pr.getstats(),
                            'cachegrind.out.controlsd_deterministic')
Exemplo n.º 27
0
def process_command_line():
    # Add current directory to python path
    cur_dir = os.path.realpath(os.getcwd())
    sys.path.insert(0, cur_dir)

    parser = ArgumentParser()
    parser.add_argument('action', type=str)
    parser.add_argument('--logging-level', default='debug')
    parser.add_argument('--lock-key')
    #parser.add_argument('--ignore-lock', action='store_true', default=False)
    parser.add_argument('--env', type=str)
    parser.add_argument('--profile', action='store_true', default=False)

    args, trash = parser.parse_known_args()

    config = load_config()
    logging_level = getattr(logging, args.logging_level.upper())
    setup_logging(args.action, logging_level, clear_handlers=True)

    if config['global'].get('django_settings_module'):
        os.environ['DJANGO_SETTINGS_MODULE'] = config['global'][
            'django_settings_module']
        # Disable django DEBUG feature to prevent memory leaks
        from django.conf import settings
        settings.DEBUG = False

    if config['global'].get('django_setup') == 'yes':
        import django
        django.setup()

    # Setup action handler
    action_name = args.action
    action_mod = None

    for path in config['global']['search_path']:
        imp_path = '%s.%s' % (path, action_name)
        if module_is_importable(imp_path):
            action_mod = __import__(imp_path, None, None, ['foo'])

    if action_mod is None:
        sys.stderr.write('Could not find the package to import %s module\n' %
                         action_name)
        sys.exit(1)

    if hasattr(action_mod, 'setup_arg_parser'):
        action_mod.setup_arg_parser(parser)
    args_obj, trash = parser.parse_known_args()

    args = vars(args_obj)

    for key, val in config.get('script:%s' % action_name, {}).items():
        args[key] = val

    if hasattr(action_mod, 'get_lock_key'):
        lock_key = action_mod.get_lock_key(**args)
    else:
        lock_key = args['lock_key']

    if lock_key is not None:
        lock_path = 'var/run/%s.lock' % lock_key
        logging.debug('Trying to lock file: {}'.format(lock_path))
        assert_lock(lock_path)

    try:
        if args['profile']:
            import cProfile
            import pyprof2calltree
            import pstats

            profile_file = 'var/%s.prof' % action_name
            profile_tree_file = 'var/%s.prof.out' % action_name

            prof = cProfile.Profile()
            try:
                prof.runctx('action_mod.main(**args)', globals(), locals())
            finally:
                stats = pstats.Stats(prof)
                stats.strip_dirs()
                pyprof2calltree.convert(stats, profile_tree_file)
        else:
            action_mod.main(**args)
    except Exception as ex:
        logging.error('Unexpected exception from action handler:', exc_info=ex)
Exemplo n.º 28
0
    if n == 0 or n == 1:
        return
    f(0)
    f(1)
    if n == 2:
        return
    f(2)


if __name__ == '__main__':
    yappi.start()
    main()
    #foo()
    #f(5)
    #frecursive(5)
    yappi.stop()

    filename = 'callgrind.yappi'
    yappi.get_func_stats().save(filename, type='callgrind')
    test_print('\nWritten callgrind file to %s\n' % filename)
    yappi.get_func_stats().print_all()

    if sys.hexversion < 0x03000000:
        import cProfile
        cProfile.run('main()', 'fooprof')
        import pstats
        p = pstats.Stats('fooprof')
        #p.strip_dirs().sort_stats(-1).print_stats()
        from pyprof2calltree import convert
        convert('fooprof', 'callgrind.cprofile')
Exemplo n.º 29
0
def _write_profile(profiler, cprofile_path, callgrind_path):
    profiler.create_stats()
    profiler.dump_stats(cprofile_path)
    convert(profiler.getstats(), callgrind_path)
Exemplo n.º 30
0
    # exit()
    run_type = 1 #"test_put"
    uct_search = UCTSearch()

    if run_type == 1:
        uct_search.uct_search(copy.deepcopy(uct_search.current_state_set), uct_search.possible_actions_session)
    elif run_type == 2:
        cProfile.run("uct_search.uct_search(copy.deepcopy(uct_search.current_state_set), uct_search.possible_actions_session, iteration_limit=10)", "profilestats")
        p = pstats.Stats('profilestats')
        # p.strip_dirs().sort_stats('cumulative').print_stats()
        p.sort_stats('cumulative').print_stats()
    elif run_type == 3:
        profiler = cProfile.Profile()
        profiler.run("uct_search.uct_search(copy.deepcopy(uct_search.current_state_set), uct_search.possible_actions_session, iteration_limit=10)")
        visualize(profiler.getstats())
        convert(profiler.getstats(), 'profiling_results.kgrind')
    elif run_type == "test_put":
        p_a = [(342, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand'), ('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (372, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (379, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (386, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (438, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (490, [('open_storage_with_hand', 'cupboard1', 'r_hand'), ('close_storage_with_hand', 'cupboard1', 'r_hand')]), (492, [('open_storage_with_hand', 'cupboard1', 'l_hand'), ('close_storage_with_hand', 'cupboard1', 'l_hand')]), (524, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (527, [('open_storage_with_hand', 'cupboard1', 'r_hand'), ('close_storage_with_hand', 'cupboard1', 'r_hand')]), (528, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand')]), (531, [('open_storage_with_hand', 'cupboard1', 'l_hand'), ('close_storage_with_hand', 'cupboard1', 'l_hand')]), (759, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (786, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (787, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (794, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (805, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (819, [('open_storage_with_hand', 'cupboard1', 'r_hand'), ('close_storage_with_hand', 'cupboard1', 'r_hand')]), (821, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (822, [('open_storage_with_hand', 'cupboard1', 'l_hand'), ('close_storage_with_hand', 'cupboard1', 'l_hand')]), (831, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (842, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (868, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand')]), (892, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (897, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (904, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (1255, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (1256, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (1277, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (1347, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1349, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1353, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (1355, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1357, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1361, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand')]), (1368, [('open_storage_with_hand', 'cupboard1', 'l_hand'), ('close_storage_with_hand', 'cupboard1', 'l_hand')]), (1387, [('open_storage_with_hand', 'drawer1', 'r_hand'), ('close_storage_with_hand', 'drawer1', 'r_hand'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1394, [('open_storage_with_hand', 'drawer1', 'l_hand'), ('close_storage_with_hand', 'drawer1', 'l_hand')]), (1395, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1408, [('open_storage_with_hand', 'cupboard1', 'r_hand'), ('close_storage_with_hand', 'cupboard1', 'r_hand')]), (1417, [('open_storage_with_hand', 'cupboard1', 'r_hand'), ('close_storage_with_hand', 'cupboard1', 'r_hand')]), (1426, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1460, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1468, [('open_storage_with_hand', 'cupboard1', 'r_hand'), ('close_storage_with_hand', 'cupboard1', 'r_hand'), ('open_storage_with_hand', 'cupboard1', 'l_hand'), ('close_storage_with_hand', 'cupboard1', 'l_hand')]), (1473, [('cut', 'plastic_paper_bag1', 'knife1'), ('put_in_hand', 'knife1', 'r_hand'), ('put_out_of_hand', 'knife1', 'r_hand'), ('cut', 'r_hand', 'knife1'), ('cut', 'drawer1', 'knife1')]), (1478, [('cut', 'cupboard1', 'knife1'), ('cut', 'plastic_paper_bag1', 'knife1')]), (1488, [('open_storage_with_hand', 'drawer1', 'r_hand'), ('close_storage_with_hand', 'drawer1', 'r_hand')]), (1489, [('cut', 'plastic_paper_bag1', 'knife1'), ('cut', 'cupboard1', 'knife1')]), (1497, [('cut', 'counter1', 'knife1')]), (1500, [('cut', 'bread1', 'knife1'), ('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand'), ('open_storage_with_hand', 'cupboard1', 'r_hand'), ('close_storage_with_hand', 'cupboard1', 'r_hand')]), (1518, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand')]), (1534, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1540, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (1543, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (1551, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1560, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1603, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1617, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1620, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1647, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1649, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1658, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1670, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1767, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1773, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1778, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1780, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1783, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1788, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (1789, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (1791, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand')]), (1793, [('cut', 'bread1', 'knife1')]), (1794, [('cut', 'bread1', 'knife1')]), (1795, [('cut', 'plate1', 'knife1'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1802, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand'), ('cut', 'bread1', 'knife1'), ('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1803, [('cut', 'plastic_paper_bag1', 'knife1')]), (1805, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1807, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1821, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1823, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (1826, [('put_in_hand', 'knife1', 'r_hand'), ('put_out_of_hand', 'knife1', 'r_hand'), ('cut', 'r_hand', 'knife1')]), (1841, [('open_storage_with_hand', 'cupboard1', 'l_hand'), ('close_storage_with_hand', 'cupboard1', 'l_hand')]), (1873, [('open_storage_with_hand', 'cupboard1', 'l_hand'), ('close_storage_with_hand', 'cupboard1', 'l_hand')]), (1879, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand'), ('cut', 'drawer1', 'knife1'), ('open_storage_with_hand', 'drawer1', 'l_hand'), ('close_storage_with_hand', 'drawer1', 'l_hand'), ('open_storage_with_hand', 'drawer1', 'r_hand'), ('close_storage_with_hand', 'drawer1', 'r_hand')]), (1882, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand'), ('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1883, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1892, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1896, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1899, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1912, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (1913, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1923, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1924, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (1931, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1933, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1966, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (1968, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (1983, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (1998, [('put_in_hand', 'cuttingboard1', 'l_hand'), ('put_out_of_hand', 'cuttingboard1', 'l_hand')]), (2024, [('put_in_hand', 'cuttingboard1', 'l_hand'), ('put_out_of_hand', 'cuttingboard1', 'l_hand')]), (2025, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (2026, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand')]), (2027, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2030, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand'), ('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2031, [('put_in_hand', 'knife1', 'r_hand'), ('put_out_of_hand', 'knife1', 'r_hand'), ('cut', 'r_hand', 'knife1'), ('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2033, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (2047, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (2049, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2050, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand'), ('cut', 'plastic_paper_bag1', 'knife1'), ('cut', 'plate1', 'knife1')]), (2051, [('cut', 'bread1', 'knife1')]), (2052, [('cut', 'cuttingboard1', 'knife1')]), (2055, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2063, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2070, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand')]), (2071, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand'), ('put_in_hand', 'cuttingboard1', 'l_hand'), ('put_out_of_hand', 'cuttingboard1', 'l_hand')]), (2072, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (2077, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2078, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2082, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2192, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2194, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2197, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2199, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2202, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2204, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2270, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2285, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2296, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2303, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2310, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2362, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2365, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2382, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2384, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2479, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2491, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('cut', 'bread1', 'knife1'), ('cut', 'bread1', 'knife1')]), (2518, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand'), ('put_in_hand', 'cuttingboard1', 'l_hand'), ('put_out_of_hand', 'cuttingboard1', 'l_hand')]), (2519, [('put_in_hand', 'cuttingboard1', 'l_hand'), ('put_out_of_hand', 'cuttingboard1', 'l_hand')]), (2520, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (2521, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2530, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2568, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2569, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2572, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (2573, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (2581, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2594, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2600, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (2601, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2602, [('put_in_hand', 'cuttingboard1', 'l_hand'), ('put_out_of_hand', 'cuttingboard1', 'l_hand')]), (2603, [('cut', 'bread1', 'knife1')]), (2608, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (2609, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand')]), (2610, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'knife1', 'r_hand'), ('put_out_of_hand', 'knife1', 'r_hand'), ('cut', 'r_hand', 'knife1')]), (2612, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand'), ('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2614, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2619, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (2626, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2627, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2628, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand'), ('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (2630, [('put_in_hand', 'knife1', 'r_hand'), ('put_out_of_hand', 'knife1', 'r_hand'), ('cut', 'r_hand', 'knife1')]), (2631, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2635, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2639, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2640, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2656, [('cut', 'bread1', 'knife1')]), (2657, [('put_in_hand', 'knife1', 'r_hand'), ('put_out_of_hand', 'knife1', 'r_hand'), ('cut', 'r_hand', 'knife1')]), (2659, [('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand'), ('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand'), ('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2660, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2661, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2679, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2680, [('put_in_hand', 'plastic_paper_bag1', 'r_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'r_hand')]), (2681, [('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand'), ('put_in_hand', 'bread1', 'r_hand'), ('put_out_of_hand', 'bread1', 'r_hand')]), (2682, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand'), ('put_in_hand', 'plate1', 'r_hand'), ('put_out_of_hand', 'plate1', 'r_hand')]), (2684, [('put_in_hand', 'knife1', 'r_hand'), ('put_out_of_hand', 'knife1', 'r_hand'), ('cut', 'r_hand', 'knife1'), ('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2693, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2702, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (2703, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand'), ('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (2705, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (2706, [('put_in_hand', 'plastic_paper_bag1', 'l_hand'), ('put_out_of_hand', 'plastic_paper_bag1', 'l_hand')]), (2712, [('put_in_hand', 'cuttingboard1', 'l_hand'), ('put_out_of_hand', 'cuttingboard1', 'l_hand')]), (2724, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (2732, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (2733, [('put_in_hand', 'bread1', 'l_hand'), ('put_out_of_hand', 'bread1', 'l_hand')]), (2744, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (2752, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2753, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2771, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (2773, [('put_in_hand', 'plate1', 'l_hand'), ('put_out_of_hand', 'plate1', 'l_hand')]), (2816, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2821, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2848, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2862, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2908, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2922, [('put_in_hand', 'knife1', 'l_hand'), ('put_out_of_hand', 'knife1', 'l_hand'), ('cut', 'l_hand', 'knife1')]), (2941, [('put_in_hand', 'cuttingboard1', 'r_hand'), ('put_out_of_hand', 'cuttingboard1', 'r_hand')]), (2942, [('put_in_hand', 'cuttingboard1', 'l_hand'), ('put_out_of_hand', 'cuttingboard1', 'l_hand')]), (2946, [('put_in_hand', 'knife1', 'r_hand'), ('put_out_of_hand', 'knife1', 'r_hand'), ('cut', 'r_hand', 'knife1')]), (2951, [('open_storage_with_hand', 'cupboard1', 'r_hand'), ('close_storage_with_hand', 'cupboard1', 'r_hand')])]
        uct_search.uct_search(copy.deepcopy(uct_search.current_state_set), p_a)
    else:
        print("provide valid run_type number")

    G = uct_search.create_nx_graph()
    draw_search_tree_mod.draw_tree_nx(G, 'final')
    print("done")

#TODO make DAG
#TODO make state lookup
#TODO draw graph https://plot.ly/python/tree-plots/

#TODO are nop state really that good?