mysql

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package mysql implements the "mysql" protocol.Protocol driver, for load testing a MySQL or MariaDB server (or a database that speaks the same wire protocol, such as TiDB). It uses a pure Go client.

Each call to Do runs one SQL text, read from -body, and reads every row it returns. The connections are kept in a pool that all users share, as an application's connection pool is. Waiting for a free connection counts as part of the measured time, so give -opt pool at least as many connections as users when you want to measure the server and not the queue.

Options (set with -opt key=value):

username      The database user. It can also be given in the URL.
password_env  The name of an environment variable that holds the
              password. It needs a user. The password is never put on
              the command line, and it is refused in the URL.
database      The database. It can also be given as the path of the URL.
tls           preferred (default), false, true, or skip-verify.
              true checks the certificate and the host name. With
              -insecure, true does not check the certificate.
              preferred uses TLS when the server offers it, and plain
              text when it does not.
pool          The most connections the run opens (default 10).
allow_writes  true to let the SQL change data. The default is read-only:
              every new connection runs SET SESSION TRANSACTION READ ONLY,
              so a load test cannot change data by mistake. A statement
              that writes then fails, and the error says how to allow it.
              This is a safety net, not a lock: the SQL can still ask for
              a read-write transaction itself (START TRANSACTION READ WRITE).
              "set session transaction read write; insert ..." in one text
              works. The next call is blocked, because SET discards the
              connection.
query_mode    simple (default) or prepared. simple sends the SQL as one
              text, so it can hold several statements such as
              "begin; update ... where id = ?; select ...; commit",
              with or without args. VegaLoad puts the args into the text
              as quoted values. prepared prepares the statement once and
              reuses it, as most applications do. It runs one statement
              and does not keep a connection for the call, so it cannot
              run set, use, lock, xa, unlock, begin, or start.
args          A JSON array of values for the ? placeholders, such as
              '[42, "abc"]'.
min_rows      The result must have at least this many rows.
expect        Some value in the result must contain this text. Only the
              first max_rows rows are checked.
max_rows      How many rows a scenario script gets back (default 1000).
              Every row is read and counted either way.

The target is mysql://host[:port][/database] (or mariadb://), with port 3306 by default. The URL may end with ?tls=.... Any other setting after the "?" is refused.

A text of several statements reports rowsAffected as 0. The client library skips a result that has no columns, so the affected-row count of an update that shares the text with a select is not available. A single insert or update does report rowsAffected and lastInsertId.

Index

Constants

This section is empty.

Variables

View Source
var Options = []string{
	"username", "password_env", "database", "tls",
	"pool", "allow_writes", "query_mode", "args", "min_rows", "expect", "max_rows",
}

Options are the -opt keys this driver accepts. The command's protocol table uses the same list, so the two cannot differ.

Functions

This section is empty.

Types

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver is a MySQL protocol.Protocol.

func New

func New(target protocol.Target, timeout time.Duration) (*Driver, error)

New returns a ready-to-use Driver for target. target.URL is mysql://host[:port][/database]. The SQL is target.Body.

New returns an error only for a configuration problem, never for the server being unreachable, which Do reports per call. Creating the pool does not connect.

func NewConn

func NewConn(target protocol.Target, timeout time.Duration, password *string) (*Driver, error)

NewConn is New for a scenario script. It makes only the connection pool: the server, the login and the connection options. It needs no SQL. The job of each call is then set with Call. password is the password, which the script already holds, so password_env is not used with it.

func (*Driver) Call

func (d *Driver) Call(opts map[string]string, body []byte, timeout time.Duration) (*Driver, error)

Call returns a Driver for one call of a script: the same pool as d, with the job that opts and body describe. opts must hold the same connection options d was made with. The result shares d's pool, so closing it is d's job, not the caller's.

func (*Driver) Close

func (d *Driver) Close() error

Close implements protocol.Protocol. It closes the pool and any prepared statements. A Driver made with Call shares the pool, so Close on it does nothing.

func (*Driver) Do

func (d *Driver) Do(parent context.Context) (protocol.Result, error)

Do implements protocol.Protocol.

func (*Driver) Name

func (d *Driver) Name() string

Name implements protocol.Protocol.

func (*Driver) Run

func (d *Driver) Run(parent context.Context) (protocol.Result, Reply)

Run is Do, and it also returns the rows of the call. A load test does not need them. A scenario script does.

type Reply

type Reply struct {
	// Columns are the column names of the result.
	Columns []string
	// Rows are the first max_rows rows, each as plain values in column order.
	Rows [][]any
	// RowCount is how many rows the result had, including rows past max_rows.
	RowCount int
	// RowsAffected is how many rows a single insert, update or delete
	// changed. It is 0 when the text holds several statements: that count
	// is not available then.
	RowsAffected int64
	// LastInsertID is the id a single insert produced. It is 0 when the
	// text holds several statements.
	LastInsertID int64
}

Reply is what a call read, besides the result.

Directories

Path Synopsis
Package mysqltest is a small fake MySQL server for tests, in this package and in others.
Package mysqltest is a small fake MySQL server for tests, in this package and in others.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL