【发布时间】:2015-01-20 20:18:10
【问题描述】:
目前我有一个需要提取日志的脚本。下面是 Perl 代码 sn-p: 脚本遍历每个服务器文件夹并 grep 必要的信息。问题是当日志数量可能很大时,脚本可能需要很长时间才能完成。瓶颈是这条线:
@leaf_lines = qx($grep -l "stagename = $current_stage" $grep_path| xargs $grep "Keywords")
我想知道是否有任何方法可以加快此操作? 脚本运行在每个 CPU 8 核和 8G 内存的服务器上,有没有办法使用这些资源?
my $grep = ($leaflog_zipped) ? "zgrep" : "grep" ;
my %leaf_info;
my @stage = ("STAGE1", "STAGE1", "STAGE3");
foreach my $leaf_dir (@leaf_dir_list){
my $grep_path = $log_root_dir . "/$leaf_dir/*" ;
foreach my $current_stage (@stage){
my @leaf_lines;
@leaf_lines = qx($grep -l "stagename = $current_stage" $grep_path| xargs $grep "Keywords"); ## how to improve the grep speed?
foreach (@leaf_lines){
if(...){
$leaf_info{$current_stage}{xxx} = xxxx;
}
}
}
}
【问题讨论】: