Performance Visualization with Gnuplot

Wed 23 September 2009
  • 装备 tags:
  • awk
  • foss
  • gnuplot
  • linux published: true comments: true

Gnuplot is considered to be one of the most famous plotting tools on both linux and windows. With Gnuplot, generation of charts becomes agile and easy. Gnuplot supports sorts of terminals range from gui, image to printer. To enable png terminal support, we will build gnuplot with following steps. (However, on most linux distributions you don't have to do these manually, install gnuplot with package manager is doubtlessly the best choice for most cases.)

Well, for a naked machine, it won't be too long to type commands in gnuplot.

Installing libpng

linpng provides functionality to write png images.
Grab source form libpng's website: wget http://downloads.sourceforge.net/project/libpng/00-libpng-stable/1.2.39/libpng-1.2.39.tar.gz?use_mirror=ncu

Uncompress the package: tar xfz libpng-1.2.39.tar.gz

cd to result directory and build it: cd libpng-1.2.39
./configure
make
make install

Installing libgd

gd provides api for programmer to draw images like Graphics2d in java. Gnuplot uses gd to draw charts on images.
Grab source from gd's website: wget http://www.libgd.org/releases/gd-2.0.35.tar.gz

Uncompress it: tar xfz gd-2.0.35.tar.gz

build it: cd gd-2.0.35
./configure
make
make install

Compiling and building Gnuplot

Grab source from sourceforge: wget http://downloads.sourceforge.net/project/gnuplot/gnuplot/4.2.6/gnuplot-4.2.6.tar.gz?use_mirror=ncu

Uncompress and compile: tar xfz gnuplot-4.2.6.tar.gz
cd gnuplot
./configure
make
make install

if "'@LIBICONV@' not found" encountered during make, just edit src/Makefile to replace -l@LIBICONV@ to -liconv, and make again.
Try to start gnuplot simply with: gnuplot

Now you may be stopped by error message like libiconv.so.2 not found, then you have to copy it from somewhere(such as /usr/local/lib) to /usr/lib, the it will work.

Using Gnuplot

As the installation completed, we start to use it.

vmstat is a great utility shows you the status of your process queue, memory, swap, io rates, interrupts and cpu usage. With command-line arguments, we can dump these data in a fixed interval and fixed count: vmstat 5 10

awk is a well-known editor to analyze and extract text from input, we can filter specified fields with awk like this: vmstat 5 10 | awk 'NR > 2 {print NR-2, $13}' The header was ignored by the conditional expression NR>2 which NR stands for Number-of-Row

The output is just enough for gnuplot, now use pipe to connect them together: vmstat 5 10 | awk 'NR > 2 {print NR, $13}' | gnuplot -e "set terminal png; set output 'v.png'; plot '-' u 1:2 t 'cpu' w linespoints;"

To gather more information at one time, a more complex command is needed: vmstat 10 360 | awk 'NR > 2 {print NR, $4, $9, $10, $13}' > vmstat.dat ; gnuplot -e "set terminal png;set output 'vmstat.png';set grid; set multiplot;set size 0.5, 0.5;set origin 0, 0;plot 'vmstat.dat' u 1:2 t 'freemem' w linespoints;set size 0.5, 0.5;set origin 0.5, 0;plot 'vmstat.dat' u 1:3 t 'bi' w linespoints;set size 0.5, 0.5;set origin 0, 0.5;plot 'vmstat.dat' u 1:4 t 'bo' w linespoints;set size 0.5, 0.5;set origin 0.5, 0.5;plot 'vmstat.dat' u 1:5 t 'cpu' w linespoints;unset multiplot;"

Unfortunately, gnuplot in Ubuntu jaunty is at version of 4.2.4, which doesn't support -e option. So you cannot send gnuplot commands directly in command-line. The solution is split it to several steps: vmstat 10 360 | awk 'NR > 2 {print NR, $4, $9, $10, $13}' > vmstat.dat
gnuplot
>set terminal png;
>set output 'vmstat.png';
>set grid;
>set multiplot;
>set size 0.5, 0.5;
>set origin 0, 0;
>plot 'vmstat.dat' u 1:2 t 'freemem' w linespoints;
>set size 0.5, 0.5;
>set origin 0.5, 0;
>plot 'vmstat.dat' u 1:3 t 'bi' w linespoints;
>set size 0.5, 0.5;
>set origin 0, 0.5;plot 'vmstat.dat' u 1:4 t 'bo' w linespoints;
>set size 0.5, 0.5;
>set origin 0.5, 0.5;
>plot 'vmstat.dat' u 1:5 t 'cpu' w linespoints;
>unset multiplot;

This picture is a stat result of performance test this afternoon

For more detailed tutorials, check the article on ibm dw: http://www.ibm.com/developerworks/library/l-gnuplot/