通过Gnuplot生成ONE的仿真数据图表
在theONE模拟器的toolkit文件夹个有一个名叫getStats.pl的脚本,可以完成数据的图表输出,但是使用起来要手动输入一大堆参数,而且生成的是直方图,看起来不是很直观。
前天我花了一天时间学习perl,然后自己写了一个脚本,自动寻找当前目录的所有theONE的消息统计报告文件,然后将所有报告文件中的某一字段生成折线图。以下是文件的详细内容:
1 #plot-reports.pl
2 #extract the statistic data of a specific router of different parameters and then plot it
3 #2011.11.27
4 use 5.010;
5 use Cwd 'abs_path';
6 use strict;
7 use warnings;
8
9 #stat property name
10 my $stat_name = "delivery_prob";
11 #mapping from router name to stat files
12 my %router_stats;
13
14 #get the directory path thet this script lies in
15 my $script_path = abs_path($0);
16 unless($script_path =~ s/[0-9a-zA-Z-_]+\.pl//g){
17 print $script_path."cannot be parsed!\n";
18 exit;
19 }
20
21
22 opendir DIR,$script_path;
23
24 while(my $file = readdir(DIR)){
25 #extract router name & buffer size
26 my $router;
27 my $bsize;
28
29
30 $file =~ m/.+MessageStatsReport\.txt/ || next;
31 $file =~ m/(\d+)M.*-([\w]+Router)_MessageStatsReport\.txt$/;
32
33
34
35 $router = $2;
36 $bsize = $1;
37 $file = $script_path.$file;
38 #open the file and extract the value
39 open FH,"<$file";
40 my $value;
41 while(<FH>){
42 m/$stat_name/ && (($value) = m/$stat_name: (.+)/);
43 }
44 print "$router-$bsize: $value\n";
45 #open router.txt and write the value
46 $router_stats{$router}{$bsize} = $value;
47 close FH;
48 #print %{$router_stats{$router}};
49 #open FH,">>$stat_file";
50 #print FH "$bsize\t$value\n";
51 #close FH;
52 }
53 closedir(DIR);
54
55
56 sub by_number{
57 $a<=>$b
58 }
59 #traverse the router_stats and save the data into text files.
60 my %router_files;
61 foreach my $key(keys %router_stats){
62 print "\nkey:$key\n";
63
64 #$router_stats{$key} = sort $router_stats{$key};
65 my %new_hash = %{$router_stats{$key}};
66
67 my $stat_file = $key."-stat.txt";
68 $router_files{$key} = $stat_file;
69 open FH,">$stat_file";
70
71 my @bsize_list;
72 foreach my $size(keys %new_hash){
73 push @bsize_list,$size;
74 }
75 @bsize_list = sort by_number @bsize_list;
76 foreach(@bsize_list){
77 print FH "$_\t$new_hash{$_}\n";
78 }
79 close FH;
80 }
81 #start to print the plot file
82 my $plotfile = "gnuplot.txt";
83 my $imgfile = "output.png";
84 open FH,">$plotfile";
85 print FH "set title \"$stat_name\" \n";
86 print FH "set grid\n";
87 #print FH "set xrange [1:512]\n";
88 print FH "set key right top Left reverse width 0 box 3\n";
89 print FH "set xlabel \"Buffer Size\" 0,0\n";
90 print FH "set ylabel \"$stat_name\" 0,0\n";
91 print FH "plot ";
92 #print the router names
93 my $router_cnt = 0;
94 while( (my $key, my $val) = each %router_files){
95 if($router_cnt > 0){
96 print FH ",";
97 }
98 $router_cnt++;
99 print FH "\"$val\" using 2:xtic(1) title \"$key\" with linespoints " ;
100 }
101 print FH "\n";
102 print FH "set terminal png\n";
103 print FH "replot\n";
104 close FH;
105
106 #plot it and open the exported image file
107 system("gnuplot $plotfile > $imgfile") == 0 or die "Error running gnuplot: $?";
2 #extract the statistic data of a specific router of different parameters and then plot it
3 #2011.11.27
4 use 5.010;
5 use Cwd 'abs_path';
6 use strict;
7 use warnings;
8
9 #stat property name
10 my $stat_name = "delivery_prob";
11 #mapping from router name to stat files
12 my %router_stats;
13
14 #get the directory path thet this script lies in
15 my $script_path = abs_path($0);
16 unless($script_path =~ s/[0-9a-zA-Z-_]+\.pl//g){
17 print $script_path."cannot be parsed!\n";
18 exit;
19 }
20
21
22 opendir DIR,$script_path;
23
24 while(my $file = readdir(DIR)){
25 #extract router name & buffer size
26 my $router;
27 my $bsize;
28
29
30 $file =~ m/.+MessageStatsReport\.txt/ || next;
31 $file =~ m/(\d+)M.*-([\w]+Router)_MessageStatsReport\.txt$/;
32
33
34
35 $router = $2;
36 $bsize = $1;
37 $file = $script_path.$file;
38 #open the file and extract the value
39 open FH,"<$file";
40 my $value;
41 while(<FH>){
42 m/$stat_name/ && (($value) = m/$stat_name: (.+)/);
43 }
44 print "$router-$bsize: $value\n";
45 #open router.txt and write the value
46 $router_stats{$router}{$bsize} = $value;
47 close FH;
48 #print %{$router_stats{$router}};
49 #open FH,">>$stat_file";
50 #print FH "$bsize\t$value\n";
51 #close FH;
52 }
53 closedir(DIR);
54
55
56 sub by_number{
57 $a<=>$b
58 }
59 #traverse the router_stats and save the data into text files.
60 my %router_files;
61 foreach my $key(keys %router_stats){
62 print "\nkey:$key\n";
63
64 #$router_stats{$key} = sort $router_stats{$key};
65 my %new_hash = %{$router_stats{$key}};
66
67 my $stat_file = $key."-stat.txt";
68 $router_files{$key} = $stat_file;
69 open FH,">$stat_file";
70
71 my @bsize_list;
72 foreach my $size(keys %new_hash){
73 push @bsize_list,$size;
74 }
75 @bsize_list = sort by_number @bsize_list;
76 foreach(@bsize_list){
77 print FH "$_\t$new_hash{$_}\n";
78 }
79 close FH;
80 }
81 #start to print the plot file
82 my $plotfile = "gnuplot.txt";
83 my $imgfile = "output.png";
84 open FH,">$plotfile";
85 print FH "set title \"$stat_name\" \n";
86 print FH "set grid\n";
87 #print FH "set xrange [1:512]\n";
88 print FH "set key right top Left reverse width 0 box 3\n";
89 print FH "set xlabel \"Buffer Size\" 0,0\n";
90 print FH "set ylabel \"$stat_name\" 0,0\n";
91 print FH "plot ";
92 #print the router names
93 my $router_cnt = 0;
94 while( (my $key, my $val) = each %router_files){
95 if($router_cnt > 0){
96 print FH ",";
97 }
98 $router_cnt++;
99 print FH "\"$val\" using 2:xtic(1) title \"$key\" with linespoints " ;
100 }
101 print FH "\n";
102 print FH "set terminal png\n";
103 print FH "replot\n";
104 close FH;
105
106 #plot it and open the exported image file
107 system("gnuplot $plotfile > $imgfile") == 0 or die "Error running gnuplot: $?";
108 system("$imgfile") == 0 or die "Error opening file: $?";
这个脚本大大地简化了我的工作,只要scenario_name遵循我的约定,即%%Group.bufferSize%%-%%Group.router%%,所要做的仅仅是双击执行这个脚本,就可以直接呈现图表了。当然,前提是你的电脑上必须已经安装了perl和gnuplot,并且加入了环境变量中。
如果你对perl和正则表达式有一丁点了解,这个脚本也完全可以由你来定制:将需要统计的字段赋给$stat_name ,在第31行可以修改正则模式以匹配实际的文件名格式。
问题和改进建议请联系lcm.hust at gmail.com.
转载请注明来源。
EOF.