Load test SQL with Artillery.io
Based on the Lambda Engine by Orchestrated.io.
Important:
- The plugin requires Artillery
1.5.8-3
or higher. - This plugin uses AnyDB as an underlying method to abstract the DB connectors. You need to install the appropiate connector to your database.
# If Artillery is installed globally:
npm install -g artillery-engine-sql
- Set the
engine
property of the scenario tosql
. - Set the target to either a string or an object (it's passed directly to AnyDB) as indicated here.
- Use
query
in your scenario to execute the SQL statement. You can pass either a string or an object.
You can pass either a SQL Query string to read from. The artillery template will be applied upon it, so statements like 'SELECT * from {{ var }}' are supported.
When stating the query as an object the parameter 'statement' is required and all the rest are optional. The parameter 'statement' has support for files and also for template statements. The interfaces for 'beforeRequest' and 'afterResponse' are the same as in the HTTP engine, where 'requestParams' is the target configuration and the query itself. You can modify the query dynamically in beforeRequest before it's executed.
- query:
-
statement: 'SELECT * from test where id = ?'
values:
- 45
beforeRequest: "before_fn"
afterResponse: "after_fn"
config:
target: "driver://user:pass@hostname/database"
phases:
arrivalCount: 10
duration: 1
engines:
sql: {}
scenarios:
- name: "SQL query"
engine: "sql"
flow:
- loop:
- query: 'SELECT * from test'
- think: 1
count: 100
(See example.yml for a complete example.)
You can use the above approach to read long SQL statements from a file, see the following example snippet:
scenarios:
- name: "DB Query"
engine: "sql"
flow:
- query:
beforeRequest: "loadQuery"
statement: "./query.sql"
In 'query.sql' you would have the SQL statement, as follows:
Select * from test
In your processor file, you would have something like this:
interface UserContext {
vars: { [k: string]: string | number };
}
interface DBParams {
query: string;
args: string[];
afterResponse: Function;
beforeRequest: Function;
target: Object;
}
function loadQuery(params: DBParams, context: UserContext, ee: EventListener, next: (err?: Error) => void) {
let filePath = params.query;
if (filePath !== path.basename(filePath)) {
// the query is a path
if (!path.isAbsolute(filePath) && !fs.existsSync(filePath)) {
// it may be relative to us
filePath = path.resolve(__dirname, filePath);
}
if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {
params.query = fs.readFileSync(filePath).toString();
}
}
next();
}
module.exports = { loadQuery: loadQuery };
artillery run my_script.yml