- 论坛徽章:
- 0
|
本帖最后由 purejava 于 2010-08-05 18:00 编辑
刚接触perl一周,用来分析性能测试过程中的文本文件-其实就是vmstat打出的日志,想统计在所有行中第几个字段的平均值。
可是问题总是出现在使用变量获取第n个字段值的时候,取出来总是18446744073709551615这个最大值,这是为什么呢?
解析的数据类似这样:- [2010-08-05 15:44:10] procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
- [2010-08-05 15:44:10] r b swpd free buff cache si so bi bo in cs us sy id wa st
- [2010-08-05 15:44:10] 1 0 144336 28822840 400604 2927656 0 0 0 11 0 0 27 2 71 0 0[code]
复制代码 我的脚本:parsevm
- #!/usr/bin/perl
- use strict;
- #第一个参数为要分析的文件,后面的参数为要统计行中第几个字段
- my $filename = shift @ARGV;
- unless (open TARFILE, $filename) {
- die "can not open file: $filename";
- }
- my @indexs = @ARGV;
- print "You want to parse (@indexs) of fields in file: $filename\n";
- # %count记录某字段的总和
- #%items记录某字段的个数
- my ($target, $line, @fields, %count, %items);
- #依次读取每一行,并根据传入的字段索引使用两个hashtable进行总数和个数的统计,hashtable的key就是字段所引
- while (chomp($line = <TARFILE>)) {
- @fields = split (/\s+/, $line);
- foreach $target (@indexs) {
- if ( ($fields[$target] = ~ /^\d+$/)) {
- #print "$target , $fields[$target], $fields[13]\n";
- #问题就处在这一行,取$fields[$target]时取出的总是一个最大值18446744073709551615
- $count{$target} += $fields[$target];
- print "current value is : $fields[$target]\n";
- $items{$target} += 1;
- print "count: $count{$target}, length: $items{$target}\n";
- }
- }
- }
- print "Now analysis over.\n";
- #开始计算每个字段的平均值
- foreach $target (@indexs) {
- my $avg = $count{$target}/$items{$target};
- print "index [$target]: all: $count{$target}, count: $items{$target}, avg: $avg\n";
- }
复制代码 调用方法: 分析第13、15个字段的平均值:
# perl parsevm vmstat.log 13 15
样本数据:vmstat.log[code][2010-08-05 15:44:10] procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
[2010-08-05 15:44:10] r b swpd free buff cache si so bi bo in cs us sy id wa st
[2010-08-05 15:44:10] 1 0 144336 28822840 400604 2927656 0 0 0 11 0 0 27 2 71 0 0
[2010-08-05 15:44:13] 0 0 144336 28822888 400604 2927664 0 0 0 601 1031 605 1 0 99 0 0
[2010-08-05 15:44:16] 0 0 144336 28800692 400604 2927668 0 0 0 0 3529 3365 1 0 99 0 0
[2010-08-05 15:44:19] 0 0 144336 28800692 400604 2927668 0 0 0 401 1047 473 0 0 100 0 0
[2010-08-05 15:44:22] 0 0 144336 28800692 400604 2927668 0 0 0 11 1027 487 0 0 100 0 0
[2010-08-05 15:44:22] procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
[2010-08-05 15:44:22] r b swpd free buff cache si so bi bo in cs us sy id wa st
[2010-08-05 15:44:22] 1 0 144336 28800428 400604 2927668 0 0 0 11 0 0 27 2 71 0 0
[2010-08-05 15:44:25] 0 0 144336 28800824 400604 2927668 0 0 0 0 1028 571 1 0 99 0 0
[2010-08-05 15:44:28] 0 0 144336 28800824 400604 2927668 0 0 0 48 1031 498 0 0 100 0 0
[2010-08-05 15:44:31] 1 0 144336 28209552 400608 2927668 0 0 0 91 1026 588 0 1 98 0 0
[2010-08-05 15:44:34] 1 0 144336 26553620 400628 2927684 0 0 0 405 1185 1151 3 4 91 2 0
[2010-08-05 15:44:34] procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
[2010-08-05 15:44:34] r b swpd free buff cache si so bi bo in cs us sy id wa st
[2010-08-05 15:44:34] 2 0 144336 26553604 400628 2927684 0 0 0 11 0 0 27 2 71 0 0
[2010-08-05 15:44:37] 0 0 144336 26542504 400636 2927712 0 0 0 0 1385 1535 2 0 98 0 0
[2010-08-05 15:44:40] 0 0 144336 26542132 400636 2927712 0 0 0 101 1030 841 0 0 100 0 0
[2010-08-05 15:44:43] 0 0 144336 26542256 400636 2927712 0 0 0 0 1028 837 0 0 100 0 0
[2010-08-05 15:44:46] 1 0 144336 26517992 400636 2927720 0 0 0 104 1110 1294 2 0 98 0 0[code] |
|