In performance testing, most of the time we are dealing with ASCII data, that's because html messages are in ASCII. There are cases when we have to deal with binary data, either within HTTP payload or in a protocol that involves binary message. There are two areas of processing:
- Process incoming message (from server) that contains binary data
- Encode message in binary format before sending it to server.
For point 1, we need to have a way to convert a binary data to string. As a hypothetical example, let's suppose an incoming message in the form of "\x00\x01\x00\x04\x00\x00\x03\xe8" (8 bytes in total). We need to grab the last 4 bytes (which may vary with messages) and change it to decimal format, in this case, 1000 (0x3e8 is 1000).
For point 2, we need a simple way to convert numbers in ASCII format into its binary format.
These issues seem simple, and they can happen in the real world (think of each VUser being a sensor, reporting temperature info to master server), but sadly many performance test platforms don't support it well. With netgend platform, it's fairly easy.
Here is sample script to handle incoming message.
//get incoming (binary) message and put it in variable "msg"
x = subtr(msg, 4,4);
//this will grab 4 bytes from 4th byte in msg
x = unpack("N", x);
//now x will contain the decimal value corresponding to the 4 bytes
//now you can process the value, say, sleep for x millisecond
sleep(x);
Creating binary messages and sending them are equally easy.
//suppose we want to send "\x10\x00\x00\0x04" followed by 4 bytes of
//temperature in float format
temp = rand() * 100;
msg = "\x10\x00\x00\0x04"
x = pack("f", temp);
msg = "${msg}${x}";
//Now we can send it
send(msg);
What about if we need to send a message that has both binary part and ASCII part? Not a problem.
msg = "John Smith";
buff = pack("n", length(msg));
//the above line will pack number "1" in two byte big-endean format
buff = combine(buff, msg);
send(buff);
The above are quite trivial in the eyes of many scripting languages, however, it becomes less so when you think about many concurrent instances are all receiving and sending messages independently. Of course, the test scenario can quickly get more complicated when VUsers need to check many binary fields, do sleep or send messages in nested conditions/loops.
No comments:
Post a Comment