Пример #1
0
def run(remote_server, local_host):
    stdout, stderr = remote_server.exec_command_sync(cmd)
    if len(stderr) > 0:
        raise ExecutionError(stderr)
    try:
        return float(
            stdout.split('queries:')[1].split('(')[1].split('per')[0].strip())
    except Exception as e:
        raise ExecutionError('Failed to parse sysbench result, because %s.' %
                             e)
Пример #2
0
def run(remote_server, local_host):
    """
    Because TPC-C would insert many tuples into database, we suggest that
    backup the raw data directory and restore it when run TPC-C benchmark some times.
    e.g.
    ```
        remote_server.exec_command_sync('mv ~/backup ~/gaussdb_data')
    ```

    The passed two parameters are both Executor instance, you can see it at client.py.
    :param remote_server: SSH object for remote database server.
    :param local_host: LocalExec object for local client host where run our tuning tool.
    :return: benchmark score, higher one must be better, be sure to keep in mind.
    """

    stdout, stderr = remote_server.exec_command_sync(['cd %s' % path, cmd])
    if len(stderr) > 0:
        raise ExecutionError(stderr)

    # Find the tpmC result.
    tpmC = None
    split_string = stdout.split()
    for i, st in enumerate(split_string):
        if "(NewOrders)" in st:
            tpmC = split_string[i + 2]
            break
    stdout, stderr = remote_server.exec_command_sync(
        "cat %s/benchmarksql-error.log" % path)
    nb_err = stdout.count("ERROR:")  # Penalty term.

    return float(tpmC) - 10 * nb_err  # You can modify the penalty factor.
Пример #3
0
def run(remote_server, local_host):
    """
    Because TPC-C would insert many tuples into database, we suggest that
    backup the raw data directory and restore it when run TPC-C benchmark some times.
    e.g.
    ```
        remote_server.exec_command_sync('mv ~/backup ~/gsdata')
    ```

    The passed two parameters are both Executor instance.
    :param remote_server: SSH object for remote database server.
    :param local_host: LocalExec object for local client host where run our tuning tool.
    :return: benchmark score, higher one must be better, be sure to keep in mind.
    """
    # Benchmark can be deployed on a remote server or a local server.
    # The process of generating the final report of the Benchmarksql-5.0 is separate from the test process.
    # Therefore, the `sleep` command needs to be added to wait to prevent the process from exiting prematurely.
    stdout, stderr = remote_server.exec_command_sync(['cd %s' % path, 'rm -rf benchmarksql-error.log', cmd, 'sleep 3'])
    if len(stderr) > 0:
        raise ExecutionError(stderr)

    # Find the tpmC result.
    tpmC = None
    split_string = stdout.split()
    for i, st in enumerate(split_string):
        if "(NewOrders)" in st:
            tpmC = split_string[i + 2]
            break
    stdout, stderr = remote_server.exec_command_sync(
        "cat %s/benchmarksql-error.log" % path)
    nb_err = stdout.count("ERROR:")  # Penalty term.
    return float(tpmC) - 10 * nb_err  # You can modify the penalty factor.
Пример #4
0
    def exec_command_on_host(self,
                             cmd,
                             timeout=None,
                             ignore_status_code=False):
        """
        The SSH connection is reused to execute shell commands on the Linux host.
        This method can be used to restart the database, collect database running environment information,
        and set database knobs.

        :param cmd: Shell command.
        :param timeout: Int type. Unit second.
        :param ignore_status_code: If the exit status code returned after the command is executed is unequal to 0,
                                    determine whether to ignore it. By default, it is not ignored, which may throw an
                                    exception, and some operations are insignificant or even have to be ignored.
        :return: Returns the standard output stream result after the command is executed.
        :raise: Raise ExecutionError if found error.
        """
        stdout, stderr = self.ssh.exec_command_sync(cmd, timeout=timeout)

        if len(stderr) > 0 or self.ssh.exit_status != 0:
            error_msg = "An error occurred when executing the command '%s'. " \
                        "The error information is: %s, the output information is %s." % (cmd, stderr, stdout)
            if ignore_status_code and not (len(stderr) > 0
                                           and self.ssh.exit_status != 0):
                logging.warning(error_msg)
            else:
                raise ExecutionError(error_msg)
        return stdout
Пример #5
0
    def exec_statement(self, sql, timeout=None):
        """
        Connect to the remote database host through SSH,
        run the `gsql` command to execute SQL statements, and parse the execution result.

        p.s This is why we want users who log in to the Linux host
        to have environment variables that can find `gsql` commands.

        :param sql: SQL statement
        :param timeout: Int type. Unit second.
        :return: The parsed result from SQL statement execution.
        """
        command = "gsql -p {db_port} -U {db_user} -d {db_name} -W {db_user_pwd} -c \"{sql}\";".format(
            db_port=self.db_port,
            db_user=self.db_user,
            db_name=self.db_name,
            db_user_pwd=self.db_user_pwd,
            sql=sql)

        stdout, stderr = self.ssh.exec_command_sync(command, timeout)
        if len(stderr) > 0 or self.ssh.exit_status != 0:
            logging.error(
                "Cannot execute SQL statement: %s. Error message: %s.", sql,
                stderr)
            raise ExecutionError("Cannot execute SQL statement: %s." % sql)

        return to_tuples(stdout)
Пример #6
0
    def exec_statement(self, sql, timeout=None):
        """
        Connect to the remote database host through SSH,
        run the `gsql` command to execute SQL statements, and parse the execution result.

        p.s This is why we want users who log in to the Linux host
        to have environment variables that can find `gsql` commands.

        :param sql: SQL statement
        :param timeout: Int type. Unit second.
        :return: The parsed result from SQL statement execution.
        """
        command = "gsql -p {db_port} -U {db_user} -d {db_name} -W {db_user_pwd} -c \"{sql}\";".format(
            db_port=self.db_port,
            db_user=self.db_user,
            db_name=self.db_name,
            db_user_pwd=self.db_user_pwd,
            sql=sql)

        stdout, stderr = self.ssh.exec_command_sync(command, timeout)
        if len(stderr) > 0 or self.ssh.exit_status != 0:
            logging.error(
                "Cannot execute SQL statement: %s. Error message: %s.", sql,
                stderr)
            raise ExecutionError("Cannot execute SQL statement: %s." % sql)

        # Parse the result.
        result = re.sub(r'[-+]{2,}', r'', stdout)  # remove '----+----'
        result = re.sub(r'\|', r'', result)  # remove '|'
        result = re.sub(r'\(\d*[\s,]*row[s]*?\)', r'',
                        result)  # remove '(n rows)'
        result = re.sub(r'\n', r' ', result)
        result = result.strip()
        result = re.split(r'\s+', result)
        return result
Пример #7
0
def run(remote_server, local_host):
    """
    Because TPC-C would insert many tuples into database, we suggest that
    backup the raw data directory and restore it when run TPC-C benchmark some times.
    e.g.
    ```
        remote_server.exec_command_sync('mv ~/backup ~/gsdata')
    ```

    The passed two parameters are both Executor instance.
    :param remote_server: SSH object for remote database server.
    :param local_host: LocalExec object for local client host where run our tuning tool.
    :return: benchmark score, higher one must be better, be sure to keep in mind.
    """
    # Benchmark can be deployed on a remote server or a local server.
    # Here we set the terminal as a remote server.
    terminal = remote_server
    err_logfile = os.path.join(path, 'benchmarksql-error.log')
    cmd_files = shlex.split(cmd)
    if len(cmd_files) != 2:
        print(
            'Invalid configuration parameter `benchmark_cmd`. '
            'You should check the item in the configuration file.',
            file=sys.stderr)
        exit(-1)
    # Check whether these files exist.
    shell_file, conf_file = cmd_files
    shell_file = os.path.join(path, shell_file)
    conf_file = os.path.join(path, conf_file)
    _, stderr1 = terminal.exec_command_sync('ls %s' % shell_file)
    _, stderr2 = terminal.exec_command_sync('ls %s' % conf_file)
    if len(stderr1) > 0 or len(stderr2) > 0:
        print(
            'You should correct the parameter `benchmark_path` that the path contains several executable SQL files '
            'in the configuration file.')
        exit(-1)
    # Clean log file
    terminal.exec_command_sync('rm -rf %s' % err_logfile)
    # Run benchmark
    stdout, stderr = terminal.exec_command_sync('cd %s; %s %s' %
                                                (path, shell_file, conf_file))
    if len(stderr) > 0:
        raise ExecutionError(stderr)

    # Find the tpmC result.
    tpmC = None
    split_string = stdout.split()
    for i, st in enumerate(split_string):
        if "(NewOrders)" in st:
            tpmC = split_string[i + 2]
            break
    stdout, stderr = terminal.exec_command_sync(
        "cat %s/benchmarksql-error.log" % path)
    nb_err = stdout.count("ERROR:")  # Penalty term.
    return float(tpmC) - 10 * nb_err  # You can modify the penalty factor.
Пример #8
0
def run(remote_server, local_host) -> float:
    wait_seconds = utils.config['benchmark_period']
    if not wait_seconds:
        print("Not configured the parameter 'benchmark_period' in the configuration file.",
              file=sys.stderr)
        exit(-1)

    stdout, stderr = remote_server.exec_command_sync(cmd)
    if len(stderr) > 0:
        raise ExecutionError(stderr)
    prev_txn = int(utils.to_tuples(stdout)[0][0])

    time.sleep(wait_seconds)
    stdout, stderr = remote_server.exec_command_sync(cmd)
    if len(stderr) > 0:
        raise ExecutionError(stderr)
    current_txn = int(utils.to_tuples(stdout)[0][0])

    # Return TPS in this period.
    return (current_txn - prev_txn) / wait_seconds
Пример #9
0
def run(remote_server, local_host):
    find_file_cmd = "find . -type f -name '*.sql'"
    stdout, stderr = remote_server.exec_command_sync(
        ['cd %s' % path, find_file_cmd])
    if len(stderr) > 0:
        raise ExecutionError(stderr)
    files = stdout.strip().split('\n')
    time_start = time.time()
    for file in files:
        perform_cmd = cmd.format(file=file)
        stdout, stderr = remote_server.exec_command_sync(
            ['cd %s' % path, perform_cmd])
        if len(stderr) > 0:
            print(stderr)
    cost = time.time() - time_start
    return -cost