In earlier blogs, we have covered various cases on handling JSON messages and we saw how easy it was to extract any field(s) from a JSON message on the NetGend platform. Saw this stackoverflow question, it's quite interesting because it's more than just value-extraction.
The task is to analyze server responses (which are in the format of JSON) and find out how many unique types that each message contains. The following is a sample server response. We can see there are 3 unique types: 8, 9, 14.
[{"type":"8","id":"2093638401"},
{"type":"9","id":"20843301"},
{"type":"9","id":"20843306"},
{"type":"14","id":"20564501"}]
A complex solution for JMeter was suggested, it involved using multiple regular expressions, loop controller, complicated manipulations of the variables. It could be well above the level of a typical user. It's quite easy on NetGend platform:
function VUSER() {
action(http, "http://www.example.com");
a = fromJson(http.replyBody); //reply may be different for different users
types = {};
count = 0;
for (i=0; i<length(a); i++) {
type = a[i].type;
if (types.$type != 1) {
types.$type = 1;
count ++;
}
}
println(count);
}
The script is pretty simple, here is a summary of what the script does:- parse the http reponse "http.replyBody" using "fromJson" function,
- Since we know this JSON message is an array, we use the "for" loop to go through the elements, each element has the fields "type" and "id". We assign the type of the element ("a[i].type" in our case) to the variable "type",
- For each element, we use "if (types.$type != 1)" to check if we have seen the "type" before (e.g. if we have seen the value contained in "$type"), if not, we set "types.$type = 1" to mark this "type" as seen (hence we don't count it when we see this type again) and increment the count.
As we been emphasizing, using the regular expression to process JSON message is not a good solution, it's hard, cumbersome and error-prone. With the growing popularity of JSON message in web applications, it's high time for other test platform to implement a good support for it. If there is a set of questions to test the flexibility of a performance test platform, this one should be one of them.
No comments:
Post a Comment