PTS is compatible with JavaScript ES2015 (ES6)+ syntax and provides additional functions to help you quickly orchestrate performance testing scenarios in script mode.
You can describe the request orchestration, variable definition, result assertion, common functions, and other logic required for your performance testing scenario by using JavaScript code in the online editor of the console. For detailed API documentation, see PTS API. PTS also provides various types of script templates (such as common usage of various protocols and some common functions) in Sample Templates for Common Scripts on the right side of the script editor in the console for your reference to write your own scripts.
Script Structure
A PTS scenario script can consist of importing dependency modules, defining global variables, defining global Options, defining functions, and defining checkpoints. For detailed script content, see the following section.
Importing Dependency Modules
After importing the required modules, you can use the APIs in the modules.
import http from 'pts/http';
import { check, sleep } from 'pts';
Defining Global Variables
If global variables are needed, they can be defined outside the functions. For example:
const globalVar = "var"
const globalObj = {
"k": "v",
}
export default function () {
console.log(globalVar);
console.log(globalObj.k);
};
Defining Global Options
You can control the default behavior of the engine through global Options.
export const option = {
http: {
http2: true,
maxIdleConns: 50,
basicAuth: {
username: 'user',
password: 'passwd',
}
},
tlsConfig: {
'localhost': {
insecureSkipVerify: false,
rootCAs: [open('ca.crt')],
certificates: [{cert: open('client.crt'), key: open('client.key')}]
}
}
}
Defining Functions
The logic executed by each concurrent user (VU) in each iteration is defined in the main function (default function).
In addition to the main functions, you can also define the preprocessing (setup) and post-processing (teardown) functions, as shown below:
The preprocessing function runs once after each performance testing starts.
The post-processing function runs once before each performance testing ends.
const global = { stage: "global" };
export function setup() {
return { stage: "setup" };
}
export default function(data) {
console.log(JSON.stringify(global));
console.log(JSON.stringify(data));
}
export function teardown(data) {
console.log(JSON.stringify(global));
console.log(JSON.stringify(data));
}
Defining Checkpoints
You can determine whether a request is successful from a business perspective by configuring checkpoints.
import http from 'pts/http';
import { check } from 'pts';
export default function () {
const resp1 = http.get('http://httpbin.org/get', {
headers: {
Connection: 'keep-alive',
'User-Agent': 'pts-engine',
},
query: {
name1: 'value1',
name2: 'value2',
},
});
console.log(resp1.json().args.name1);
check('status is 200', () => resp1.statusCode === 200);
check('body.args.name1 equals value1', () => resp1.json().args.name1 === 'value1');
}
Lifecycle
Preprocessing (setup) and post-processing (teardown) functions: They run once per performance testing machine.
Code for defining global variables: It runs once per VU. Some static file reading and other operations are recommended to be defined in global, so that each concurrency only needs to read the file once. This avoids opening files repeatedly during the scenario iteration.
Code for the main function (default): It runs once per iteration for each VU, and each VU will continuously iterate until it reaches the duration limit or iteration limit configured in the current performance testing.
For example, when there are two VUs on a performance testing machine, the flowchart is as follows:
Note:
For the concept of VU, see FAQs.