#!/bin/bash # # Simple bash script for parsing and evaluating a CSV file # (C) 2006 Jan Altenberg # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License Version # 2 as published by the Free Software Foundation. GNUPLOT=`which gnuplot` split_values () { if [ "$2" = "save_old_state" ] then OLD_TIME=$TIME OLD_CH1=$CH1 OLD_CH2=$CH2 TIME=`echo $1 | cut -f1 -d,` CH1=`echo $1 | cut -f2 -d,` CH2=`echo $1 | cut -f3 -d,` else TIME=`echo $1 | cut -f1 -d,` CH1=`echo $1 | cut -f2 -d,` CH2=`echo $1 | cut -f3 -d,` fi } # We need to convert the TIME value in a format which is readable by bc ;-( # 1e-04 -> 1 / (10^4) # 1e+04 -> 1 * (10^4) convert_time_value () { VALUE=`echo $1 | sed 's/e-\(.*\)$/\/(10\^\1)/g' | sed 's/e+\(.*\)$/*(10^\1)/'` echo $VALUE } print_help () { echo "Usage: $0 -f CSV-file -p PLOT-file" echo " -f CSV-file (where your data is stored)" echo " -p PLOT-file (where the plot image will be stored)" } # Parsing command line options while getopts "f:p:v" OPTION do case $OPTION in f ) FILE=$OPTARG;; p ) PLOT_FILE=$OPTARG;; v ) VERBOSE="ON";; * ) print_help; exit 1;; esac done if [ ! "$FILE" ] then print_help exit 1 fi if [ ! -e $FILE ] then echo "Input file does not exist!" exit 1 fi if [ ! "$PLOT_FILE" ] then print_help exit 1 fi if [ ! "$GNUPLOT" ] then echo "Please install gnuplot ! !" exit 1 fi TMPFILE=/tmp/latencies_$$ RESULT_TMPFILE=/tmp/latencies_results_$$ echo "Parsing CSV file..." echo " Removing table head..." cat $FILE | sed '/.*TIME.*/d' > $TMPFILE echo " Seperating values..." for LINE in $(cat $TMPFILE) do if [ ! "$TIME_START" ] then if [ ! "$TIME" ] then split_values $LINE continue else split_values $LINE "save_old_state" if [ "`echo "scale=2; $CH1 - $OLD_CH1" | bc`" != "0" ] then TIME_START=`convert_time_value $TIME` TIME="" else continue fi fi else split_values $LINE "save_old_state" if [ "`echo "scale=2; $CH2 - $OLD_CH2" | bc`" != "0" ] then TIME_END=`convert_time_value $TIME` [ "$VERBOSE" = "ON" ] && echo " Found latency -> $TIME_END - $TIME_START" echo "scale=10; ($TIME_END - $TIME_START)" | bc | sed 's/^\./0\./' >> $RESULT_TMPFILE TIME_START="" TIME_END="" TIME="" else continue fi fi done echo "Generating plot..." cat $RESULT_TMPFILE | sort -n | uniq -c | sed 's/^ *//' | sed s'/0*$//'> $TMPFILE cp $TMPFILE $RESULT_TMPFILE XMAX=`cat $RESULT_TMPFILE | cut -f2 -d' ' | sort -rn | head -n1` YMAX=`cat $RESULT_TMPFILE | cut -f1 -d' ' | sort -rn | uniq | head -n1` XRANGE=`expr "$XMAX * 2"` YRANGE=`expr "$YMAX * 2"` echo " XMAX $XMAX" echo " YMAX $YMAX" echo -e "set title \"Latency test\"\n\ set terminal pbm color\n\ set xrange [0:$XRANGE]\n\ set yrange [0:$YRANGE]\n\ set ylabel\"n\"\n\ set style fill solid 0.250000 border\n\ set output \"$PLOT_FILE\" plot \"$RESULT_TMPFILE\" using 2:1 \"%lf%lf\" notitle with boxes" | gnuplot -persist display $PLOT_FILE