Tuesday, January 22, 2013

How to invoke JavaScript in JMeter?

A colleague complained that there is little documentation about how to invoke JavaScript in JMeter. Specifically, his problem was about how to use the session ID to compute a value to be passed to the server. After playing with JMeter for an hour, we managed to figure it out. This is how it works:

Since the session ID (JSESSIONID) is set as a cookie, we only need to figure how to retrieve the cookie in JMeter and pass it to the JavaScript. The first step is to enable cookies in JMeter so that they are accessible by using variables. In jmeter.properties, set CookieManager.save.cookie=true.

Next, make sure you add a HTTP Cookie Manager to your Thread Group. Add a BSF PostProcessor and write your JavaScript in the text area provided. You can also externalize your script to a file and specify the file name in the text field above the text area. You can access the JSESSIONID cookie this way:
var sid = "${COOKIE_JSESSIONID}";

To invoke a JavaScript function and set the return value as a variable:
var val = compute(sid);
vars.put("someKey", val);

You can access the value later as ${someKey}.

Tuesday, January 15, 2013

How to extract a range of lines from a large file?

It is not advisable to use vi to view a large file as on most system vi will create a swap file of similar size. It is better to extract the lines of interest to another file. Say you want to extract lines 20001 to 30000 from a log file. There are many ways to accomplish that and I am going to show you 2 of them here.

sed (my preferred way)
sed -n -e '20001,30000p' log.txt > /tmp/log-small.txt
-n means don't print anything unless the p command is used. -e means execute the following expression (those inside '....'). 20001,30000p means for lines 20001 to 30000 inclusive, execute the command p, which prints them to the console.

the head & tail way
head -30000 log.txt | tail -10000 > /tmp/log-small.txt

The head command will output lines 1 to 30000 and pipe to tail which will print the last 10000 lines, i.e. lines 20001 to 30000.
As you can see, the head & tail way requires some mental arithmetics and so it is not my preferred way.

Wednesday, January 09, 2013

How to find out Solaris version number?

Depending on what information you want, the following commands will provide different information about a Solaris server:
  • cat /etc/release
  • showrev
  • uname -a

Saturday, January 05, 2013

How to Get IP Address from MAC

One way to get the IP address of a remote host in the same subnet given a MAC address is by using the "arp -an" command. However, this will only work if the we have previously connected to the remote host.

With the help of nmap, we can find out the IP address. For example, the command below will scan the 10.1.1.0/24 subnet with nmap. After the scan we search through the output of arp. If the remote host that we are searching for is up, its MAC address will be displayed together with its IP address.

$ nmap -sP 192.168.0.0/24 > /dev/null
$ arp -an | grep -i "12:34:56:78:90:ab"
? (192.168.0.105) at 12:34:56:78:90:ab [ether] on wlan0