()
Performance test with locust: parse command line options and run commands.
Performance test with locust: parse command line options and run commands.
Performance test with locust : parse command line options and run commands .
def main_locust():
    """ Performance test with locust: parse command line options and run commands.
    """
    # monkey patch ssl at beginning to avoid RecursionError when running locust.
    from gevent import monkey; monkey.patch_ssl()

    import multiprocessing
    import sys
    from httprunner import logger

    try:
        from httprunner import locusts
    except ImportError:
        msg = "Locust is not installed, install first and try again.\n"
        msg += "install command: pip install locustio"
        print(msg)
        exit(1)

    sys.argv[0] = 'locust'
    if len(sys.argv) == 1:
        sys.argv.extend(["-h"])

    if sys.argv[1] in ["-h", "--help", "-V", "--version"]:
        locusts.start_locust_main()
        sys.exit(0)

    # set logging level
    if "-L" in sys.argv:
        loglevel_index = sys.argv.index('-L') + 1
    elif "--loglevel" in sys.argv:
        loglevel_index = sys.argv.index('--loglevel') + 1
    else:
        loglevel_index = None

    if loglevel_index and loglevel_index < len(sys.argv):
        loglevel = sys.argv[loglevel_index]
    else:
        # default
        loglevel = "WARNING"

    logger.setup_logger(loglevel)

    # get testcase file path
    try:
        if "-f" in sys.argv:
            testcase_index = sys.argv.index('-f') + 1
        elif "--locustfile" in sys.argv:
            testcase_index = sys.argv.index('--locustfile') + 1
        else:
            testcase_index = None

        assert testcase_index and testcase_index < len(sys.argv)
    except AssertionError:
        print("Testcase file is not specified, exit.")
        sys.exit(1)

    testcase_file_path = sys.argv[testcase_index]
    sys.argv[testcase_index] = locusts.parse_locustfile(testcase_file_path)

    if "--processes" in sys.argv:
        """ locusts -f locustfile.py --processes 4
        """
        if "--no-web" in sys.argv:
            logger.log_error("conflict parameter args: --processes & --no-web. \nexit.")
            sys.exit(1)

        processes_index = sys.argv.index('--processes')

        processes_count_index = processes_index + 1

        if processes_count_index >= len(sys.argv):
            """ do not specify processes count explicitly
                locusts -f locustfile.py --processes
            """
            processes_count = multiprocessing.cpu_count()
            logger.log_warning("processes count not specified, use {} by default.".format(processes_count))
        else:
            try:
                """ locusts -f locustfile.py --processes 4 """
                processes_count = int(sys.argv[processes_count_index])
                sys.argv.pop(processes_count_index)
            except ValueError:
                """ locusts -f locustfile.py --processes -P 8888 """
                processes_count = multiprocessing.cpu_count()
                logger.log_warning("processes count not specified, use {} by default.".format(processes_count))

        sys.argv.pop(processes_index)
        locusts.run_locusts_with_processes(sys.argv, processes_count)
    else:
        locusts.start_locust_main()
def main_locust ( ) : # monkey patch ssl at beginning to avoid RecursionError when running locust. from gevent import monkey monkey . patch_ssl ( ) import multiprocessing import sys from httprunner import logger try : from httprunner import locusts except ImportError : msg = "Locust is not installed, install first and try again.\n" msg += "install command: pip install locustio" print ( msg ) exit ( 1 ) sys . argv [ 0 ] = 'locust' if len ( sys . argv ) == 1 : sys . argv . extend ( [ "-h" ] ) if sys . argv [ 1 ] in [ "-h" , "--help" , "-V" , "--version" ] : locusts . start_locust_main ( ) sys . exit ( 0 ) # set logging level if "-L" in sys . argv : loglevel_index = sys . argv . index ( '-L' ) + 1 elif "--loglevel" in sys . argv : loglevel_index = sys . argv . index ( '--loglevel' ) + 1 else : loglevel_index = None if loglevel_index and loglevel_index < len ( sys . argv ) : loglevel = sys . argv [ loglevel_index ] else : # default loglevel = "WARNING" logger . setup_logger ( loglevel ) # get testcase file path try : if "-f" in sys . argv : testcase_index = sys . argv . index ( '-f' ) + 1 elif "--locustfile" in sys . argv : testcase_index = sys . argv . index ( '--locustfile' ) + 1 else : testcase_index = None assert testcase_index and testcase_index < len ( sys . argv ) except AssertionError : print ( "Testcase file is not specified, exit." ) sys . exit ( 1 ) testcase_file_path = sys . argv [ testcase_index ] sys . argv [ testcase_index ] = locusts . parse_locustfile ( testcase_file_path ) if "--processes" in sys . argv : """ locusts -f locustfile.py --processes 4
        """ if "--no-web" in sys . argv : logger . log_error ( "conflict parameter args: --processes & --no-web. \nexit." ) sys . exit ( 1 ) processes_index = sys . argv . index ( '--processes' ) processes_count_index = processes_index + 1 if processes_count_index >= len ( sys . argv ) : """ do not specify processes count explicitly
                locusts -f locustfile.py --processes
            """ processes_count = multiprocessing . cpu_count ( ) logger . log_warning ( "processes count not specified, use {} by default." . format ( processes_count ) ) else : try : """ locusts -f locustfile.py --processes 4 """ processes_count = int ( sys . argv [ processes_count_index ] ) sys . argv . pop ( processes_count_index ) except ValueError : """ locusts -f locustfile.py --processes -P 8888 """ processes_count = multiprocessing . cpu_count ( ) logger . log_warning ( "processes count not specified, use {} by default." . format ( processes_count ) ) sys . argv . pop ( processes_index ) locusts . run_locusts_with_processes ( sys . argv , processes_count ) else : locusts . start_locust_main ( )