#!/usr/bin/perl use Getopt::Long; ############################################## # name: graph.pl # description: this file reads the BT files resulted by DBT-1 # and generates graph file(s). # version: 0.11 # Copyright (C) 2005 Hongwei Liu # Sumisho Computer Systems Corporation # Date: 19th July, 2005 generated # 25th July, 2005 modified ############################################### $BT_type; $graph_file; $plot_format; $title; @subdirectories; $dirname; $preview; $pattern; ###########main########### process_command_line(); if($BT_type==1) { BT_graph($graph_file,$plot_format,$title,@subdirectories); } else { my $interaction_file; my $cc=0; foreach $interaction_file(@subdirectories) { my $subnumber=scalar(@subdirectories); my $number_out; $cc++; if($subnumber>1){ $number_out=$graph_file; $number_out=~s/\./\-$cc./; } else { $number_out=$graph_file; } Inter_graph($number_out,$plot_format,$title,$interaction_file); } } ##############Sub########## sub by_number { my $aa=$a; my $bb=$b; $aa=~s/\D+//; $bb=~s/\D+//; $aa=~s/\.\D+//; $bb=~s/\.\D+//; $aa<=>$bb; } sub BT_graph { my $filename; my $temp_out; my $temp_BT_file; my $out_name; my $out_format; my $BT_title; my @subdirectories; my $BT_title; my $process_result; ($out_name,$out_format,$BT_title,@subdirectories)=@_; foreach $filename(@subdirectories) { $temp_out="/tmp/"."tempBT".int(rand(10000)); $process_result=BT_process($filename,$temp_out); #read BT value if($process_result!=-1) { @temp_BT_file=(@temp_BT_file,$temp_out); } } if(scalar(@temp_BT_file)>0) { plot_BT_files($out_name,$out_format,$BT_title,@temp_BT_file); ####report results#### print "\n-------------summary---------------\n"; print "plot command file:plot_BT.gnu\n"; scalar(@temp_BT_file)>1?print "temp BT data files:@temp_BT_file\n":print "temp BT data file:@temp_BT_file\n"; print "graph file: $out_name.\n"; return 0; } else { print "\n-------------No BT file found---------------\n"; } ###the definition of sub routines #######sub BT_process####### sub BT_process { my @entries; my $filename; my @subdir; my $User_number; my $text; my($target_dir,$result_data_file)=@_; #print "target_dir=$target_dir,result_data_file=$result_data_file\n"; opendir(DirH, "$target_dir") or die "can not open the dir for read:$target_dir\n $!"; @entries=readdir(DirH); foreach $filename(@entries) { if(-d $target_dir."/".$filename) { #if it is a directory, add it the the list @subdir=(@subdir,$filename) unless ($filename eq "." || $filename eq ".." || $filename!~/$pattern/); } } if(scalar(@subdir)==0) { return -1; } @subdir=sort by_number @subdir; #sort the list by the number of directory #print "=====>test1\n@subdir\n"; open (FH_gnu_data_file, ">$result_data_file") or die "can not open file for writing:$gnu_datafilename\n $!\n"; foreach $filename(@subdir) { #print "Processing $target_dir/$filename/BT..."; open(FH,"< $target_dir/$filename/BT") or die "can not find BT file in $target_dir/$filename\n$!\n"; #now read the BT value from data file and generate #the formated data file for gnuplot $text=; while($text) { $text=; #match the "bogotransactions" string if($text=~m/\d+ bogotransactions per second/) { $text=~s/ bogotransactions per second//; $User_number=$filename; $User_number=~s/\D+//; $User_number=~s/\.\D+//; #print "User:$User_number\tBT:$text\n"; print FH_gnu_data_file "$User_number\t$text"; } } close(FH); #print "ok.\n"; } close(FH_gnu_data_file); closedir(DirH); return 0; } #######end of BT_process#### #######sub plot_BT_files#### sub plot_BT_files { my @plot_command="plot"; my $tmp_number; my $i; my $title; my @BT_data_file; my $out_name; my $out_format; ($out_name,$out_format,$title, @BT_data_file)=@_; open (FH_gnu_command_file, "> plot_BT.gnu") or die "can not open file for writing:plot_BT.gnu\n $!\n"; #print FH_gnu_command_file "unset key\n"; if(defined($title)) { print FH_gnu_command_file "set title \"$title\"\n"; } else { print FH_gnu_command_file "set title \"DBT-1 Result:BT/s\"\n"; } print FH_gnu_command_file "set xlabel \"User\"\n"; print FH_gnu_command_file "set ylabel \"BT\"\n"; print FH_gnu_command_file "set key right bottom\n"; $tmp_number=@BT_data_file; for($i=0;$i<$tmp_number;$i++) { @plot_command=(@plot_command,"\"".$BT_data_file[$i]."\""." u 1:2 t \"".$subdirectories[$i]."\" "."w linespoints"); #@plot_command=(@plot_command,"\"".@temp_BT_file[$i]."\""." u 1:2 t \"\" "."w l"); @plot_command=(@plot_command,","); } pop(@plot_command); if(defined($preview)) { print FH_gnu_command_file "@plot_command\n"; print FH_gnu_command_file "pause -1 \"Hit any key to save the graph...\"\n"; if($out_format eq "postscript eps") { print FH_gnu_command_file "set terminal $out_format solid color\n"; } else { print FH_gnu_command_file "set terminal $out_format\n"; } print FH_gnu_command_file "set output \"$out_name\"\n"; print FH_gnu_command_file "replot\n"; } else { print FH_gnu_command_file "set terminal $out_format\n"; print FH_gnu_command_file "set output \"$out_name\"\n"; print FH_gnu_command_file "@plot_command\n"; } close (FH_gnu_command_file); system ('gnuplot plot_BT.gnu'); } #######end of plot_BT_files### } sub Inter_graph { my $out_file_name; my $out_file_format; my $interaction_title; my $dirname; my $gnu_interaction_datafilename="/tmp/"."tempBT_interaction".int(rand(10000)); my $gnu_interaction_command="/tmp/"."tempBT_interaction".int(rand(10000)).".gnu"; ($out_file_name,$out_file_format,$interaction_title,$dirname)=@_; my $file_number=process_interaction_files($dirname,$gnu_interaction_datafilename); if($file_number!=-1) { plot_interaction_files($file_number,$out_file_name,$out_file_format,$interaction_title,$gnu_interaction_command,$gnu_interaction_datafilename); #report results print "\n-------------summary---------------\n"; print "process $dirname:\n"; print "data file: $gnu_interaction_datafilename\n"; print "plot command file:$gnu_interaction_command\n"; print "graph file: $out_file_name.\n"; } else { print "\n-------------No BT file found---------------\n"; } sub process_interaction_files { my $i; my $j; my $k; my $l; my $result; my @entries; my $action_file_number; my $filename; my $User_number; my @action_data; my @action_data_line; my @subdir; my $gnu_data; my ($dirname,$gnu_data)=@_; opendir(DirH, $dirname) or die "can not open the dir for read:$dirname\n $!"; @entries=readdir(DirH); #get the subdirectory list; #and bypass the use-less sub-directory #ended with '-n' foreach $filename(@entries) { if(-d $dirname."/".$filename) { #if it is a directory, add it the the list @subdir=(@subdir,$filename) unless ($filename eq "." || $filename eq ".." || $filename!~/$pattern/); } } #sort the list according to the number in directories' name if(scalar(@subdir)==0) { return -1; } @subdir=sort by_number @subdir; #let user confirm #print "=====>test2\n@subdir\n"; #open data file for output. open (FH_gnu_data_file, ">$gnu_data") or die "can not open file for writing:$gnu_data\n $!\n"; ######now process the interaction data #the number of interaction files need to be processed $action_file_number=@subdir; @action_data_line=(0..$action_file_number); #for ($i=0; $i<$action_file_number;$i++){ #create 2-demensional array # push @action_data,[ @action_data_line]; #} $i=0; $j=0; foreach $filename(@subdir) { #print "Processing $dirname/$filename/BT..."; open(FH,"< $dirname/$filename/BT") or die "can not find BT file in $dirname/$filename\n$!\n"; #now read the BT value from data file and generate #the formated data file for gnuplot $j=0; push @action_data,[ @action_data_line]; $text=; while($text) { $text=; #pattern: string number number if($text=~m/\D+\d+\s+\d+/) { $text=~s/\D+\d+.\d+\s+//; chop($text); $action_data[$j][$i]=$text; $j++; } } close(FH); $i++; #print "ok.\n"; } for ($k=0;$k<$j;$k++) { for($l=0;$l<$i;$l++) { print FH_gnu_data_file "$action_data[$k][$l] "; } print FH_gnu_data_file "\n"; } close (FH_gnu_data_file); closedir(DirH); return $i; } ####end of process_interaction_files#### ####sub: plot_interaction_files#### sub plot_interaction_files { my @plot_command="plot"; my $l; my $title; my $plot_format; my $graph_file; my $plot_file; my $gnu_data; my($fn,$graph_file,$plot_format,$title,$plot_file,$gnu_data)=@_; open (FH_gnu_command_file, "> $plot_file") or die "can not open file for writing:$plot_file\n $!\n"; #print FH_gnu_command_file "unset key\n"; if(defined($title)) { print FH_gnu_command_file "set title \"$title\"\n"; } else { print FH_gnu_command_file "set title \"Interaction\"\n"; } print FH_gnu_command_file "set xlabel \"\"\n"; print FH_gnu_command_file "set ylabel \"\"\n"; print FH_gnu_command_file "set rmargin 10\n"; #this is not a good solution, to write fixed code here. #we should read items from BT file, but now tentatively... print FH_gnu_command_file "set xtics rotate by -45 (\"Admin Confirm\" 0,\"Admin Request\" 1,\"Best Sellers\" 2,\"Buy Confirm\" 3,\"Buy Request\" 4,\"Customer Registration\" 5,\"Home\" 6,\"New Products\" 7,\"Order Display\" 8,\"Order Inquiry\" 9,\"Product Detail\" 10,\"Search Request\" 11,\"Search Results\" 12,\"Shopping Cart\" 13)\n"; $tmp_number=@temp_BT_file; for($l=1;$l<=$fn;$l++) { @plot_command=(@plot_command,"\"".$gnu_data."\""." u :$l t \"\" "."w l"); #@plot_command=(@plot_command,"\"".@temp_BT_file[$i]."\""." u 1:2 t \"\" "."w l"); @plot_command=(@plot_command,","); } pop(@plot_command); if(defined($preview)) { print FH_gnu_command_file "@plot_command\n"; print FH_gnu_command_file "pause -1 \"Hit any key to save the graph...\"\n"; if($plot_format eq "postscript eps") { print FH_gnu_command_file "set terminal $plot_format solid color\n"; } else { print FH_gnu_command_file "set terminal $plot_format\n"; } print FH_gnu_command_file "set output \"$graph_file\"\n"; print FH_gnu_command_file "replot\n"; } else { print FH_gnu_command_file "set terminal $plot_format\n"; print FH_gnu_command_file "set output \"$graph_file\"\n"; print FH_gnu_command_file "@plot_command\n"; } close (FH_gnu_command_file); system ('gnuplot',$plot_file); } ####end of plot_interaction_files#### } sub usage { print "usage: graph [-type=] [-title=] [-v] [-dir=<work dir>] [-sub=<sub-dir1>..-sub=<sub-dirn>][-p=\"pattern\"][-out=<graphfilename>] [-format=<image file format>]\n"; exit; } sub process_command_line { my $result; my @entries; my $filename; my $i; my $scalar; my $graph_type; $result=GetOptions("type:s"=>\$graph_type,"dir:s"=>\$dirname,"sub=s@"=>\@subdirectories,"out=s"=>\$graph_file,"format=s"=>\$plot_format,"title:s"=>\$title,"v:s"=>\$preview,"p:s"=>\$pattern) or usage; if(!defined($graph_type)) { $BT_type=1; } else { if($graph_type eq 'B') { $BT_type=1; } elsif($graph_type eq 'I') { $BT_type=0; } else { usage; } } if(defined($dirname)) #user specified a work directory { if($dirname eq '') #"NULL" work directory=current directory { $dirname="."; } } #did not define a directory means the -sub should deal with the context: #if -sub also did not defined--guess the user want to use current as work directory #and process all sub-directories if($BT_type==1) #if the graph type is BT { if(defined(@subdirectories)) { if(defined($dirname)) #user specified a work dir, so add this to sub-dir { #to make a valid absolute path $scalar=@subdirectories; for ($i=0;$i<$scalar;$i++) { $subdirectories[$i]=$dirname."/".$subdirectories[$i]; } } } else { if(!defined($dirname)) { $dirname="."; } opendir(DirH, "$dirname") or die "can not open the dir you specified for read:$dirname\n $!"; @entries=readdir(DirH); foreach $filename(@entries) { if(-d $dirname."/".$filename) { #if it is a directory, add it the the list @subdirectories=(@subdirectories,$dirname."/".$filename) unless ($filename eq "." || $filename eq ".."); } } close(DirH); } } else #if the graph type is Interaction { if(!defined(@subdirectories)) { @subdirectories=('.'); } } if(!defined($plot_format)) { $plot_format="postscript eps"; } if(!defined($graph_file)) { if($BT_type==1) { $graph_file="BT_graph.eps"; } else { $graph_file="Interaction_graph.eps"; } } if(defined($pattern)) #if the user defined direcitories pattern { #then convert it into regular expressions $pattern=~s/\./\\\./g; #'.'==>'\.' $pattern=~s/\*/.*/g; #'*'==>'.+' $pattern=~s/#/\\d/g; #'?'==>'.*' $pattern='^'.$pattern; } else { $pattern="."; } }