In performance testing, one of the most important things is to do parameterization. Sometimes, parameterization is done by loading data from a csv file.
The simplest example in my view is a csv file where each row contains a username and password. How can we emulate user logging in from these username/password pairs?
function userInit() {
var list = readCSVFile("users.csv");
var i = 0 ;
}
function VUSER () {
item = list[i];
//here item is an array variable that contains the fields of the row in csv file
i ++;
http.POSTData = "username=${item[0]}&pass=${item[1]}";
action(http,"http://www.example.com/login?");
}
Notes.- variable with a "var" in front of it is a global variable -- it can be accessed/operated by all VUSERs.
- "list[i]" in VUSER() function means i'th element of the list, in another word, it's i'th row of the csv file. Since each row contains 2 elements, so "list[i]" (hence "item") is itself an array.
- "item[0]", "item[1]" are the first two fields of that row. In our case, they are username and password.
Sometimes, when data is uniform, we don't need a csv file. For example, if we want to emulate test users like userxxxxx, where xxxxx ranges from 00000, 00001, ... to 99999, we don't have to populate a csv file with these usernames and load it into test.
function userInit() {
var i = 0 ;
}
function VUSER () {
user = sprintf("%05d", i);
i ++;
http.POSTData = "username=${user}&pass=letmein";
action(http,"http://www.example.com/login?");
}
Sadly, there are some test platform that requires you to create a csv file even in this case!what if your csv file consists of a list of products and you just want to randomly pick one and query your e-commerce web site?
function userInit() {
var list = readCSVFile("products.csv");
var listLen = getSize(list);
}
function VUSER () {
i = randNumber(0, listLen - 1);
item = list[i];
action(http,"http://www.example.com/query?product=${item[0]}");
}
There are some questions asked on stackoverflow where each VUsers needs to go through all the rows in csv file. This is so simple that it's sad that it's not supported well on some other platforms.
function userInit() {
var list = readCSVFile("products.csv");
var listLen = getSize(list);
}
function VUSER () {
for (id = 0; id < listLen; id ++ ) {
item = list[id][0];
action(http,"http://www.example.com/query?product=${item[0]}");
}
}
Of course, you can make it fancier, by adding a think-time in the csv file every other line. In another word, after user does query, he pauses for the specified millisecond before proceeding. The script is essentially the same as above. The difference is, for the odd numbered lines, the item is the number of milliseconds to sleep.
function userInit() {
var list = readCSVFile("products.csv");
var listLen = getSize(list);
}
function VUSER () {
for(id = 0; id < listLen; id ++ ) {
item = list[id][0];
odd = id % 2;
if (odd) {
sleep(item);
} else {
action(http,"http://www.example.com/query?product=${item}");
}
}
}
You may have recognized the power of csv file by now and wonder if you could make it a "CSV-driven test". Absolutely! This will work well where a good test script is written by you and other user can vary his csv file to control the behavior of the test.
No comments:
Post a Comment