Tuesday, February 21, 2012

Using Python minidom module for XML generation

Creating an XML document in Python using the minidom module is quite straightforward.  For simplicity sake, I'll base the code on Hadoop's XML config file.

import sys
from xml.dom.minidom import Document

doc = Document()

configuration = doc.createElement("configuration")

xsl = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"configuration.xsl\"")

doc.appendChild(xsl)

prop = doc.createElement("property")

# START LOOP HERE
name = doc.createElement("name")
name_text = doc.createTextNode("my name")
name.appendChild(name_text)

prop.appendChild(name)

value = doc.createElement("value")
value_text = doc.createTextNode("my value")
value.appendChild(value_text)

prop.appendChild(value)
# END LOOP HERE

configuration.appendChild(prop)

doc.appendChild(configuration)

print doc.toprettyxml()

The output is as follows:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
    <property>
        <name>
            my name
        </name>
        <value>
            my value
        </value>
    </property>
</configuration>

Friday, February 17, 2012

Scrollback in GNU Screen

This has always evaded me: How do you do scroll up the screenbuffer when using screen?  Found the answer right here:

http://www.linuxscrew.com/2008/11/14/faq-how-to-scrollback-in-gnu-screen/

I've tried the Ctrl + A + [ method; but not Ctrl + Esc as I'm using Windows as my main machine and Ctrl + Esc can also be used as the Windows key.

Just hope I remember this :)

Thursday, February 16, 2012

Measuring network throughput with iperf

The iperf tool has been around for ages and yeah, I've been late to the game :)

To install iperf in Ubuntu, it's a simple apt-get away:
$ sudo apt-get install iperf
To run the test, you'll need 2 machines.  One to act as the server and the other as the client.  Start by starting the server:
$ iperf -s
------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 85.3 KByte (default)
------------------------------------------------------------
If there are no errors, we can then start the test by executing iperf in the client machine:
$ iperf -c server -t 10
------------------------------------------------------------
Client connecting to server, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
[  3] local 192.168.0.100 port 40913 connected with 192.168.0.101 port 5001
[ ID] Interval       Transfer     Bandwidth
[  3]  0.0-10.0 sec    164 MBytes    138 Mbits/sec
The argument -t 10 states that we want the test to run for only 10 seconds. 

There are other options which you can play with like:

    -d ==> Do a bidirectional test simultaneously
    -u ==> use UDP rather than TCP
   
View the list of options by executing:
$ iperf --help
There's also a GUI wrapper written in Java for iperf available at http://code.google.com/p/xjperf/.  It offers a nice GUI which allows you to choose different options to run iperf with and also displays output from iperf in a nice graph: