public
Description: Small examples, or proof of concepts
Homepage:
Clone URL: git://github.com/melo/perl-examples.git
perl-examples / nfsd_report_bench / max_line_rate.pl
100755 81 lines (61 sloc) 1.602 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env perl
 
use strict;
use warnings;
use Time::HiRes qw( gettimeofday tv_interval );
 
my $mask = shift || 18; ## report every 256k lines
my $size = shift || 2**20; ## 1Mb default
$mask = 2**$mask - 1;
 
my $lines = my $ll = 0;
 
$SIG{INT} = sub {
  report(1);
  exit;
};
 
system("ps l | egrep 'PID|$$' | grep -v egrep");
 
print "\n\nReading line, performance report every "
  . ($mask + 1)
  . " lines\n";
print "\nPress Ctrl-C to stop with a final report (pid $$)\n\n";
 
my $buf = '';
my $offset = 0;
my ($ts, $type, $op, $bytes);
my $t0 = my $tl = [gettimeofday];
my $read = my $rr = 0;
while () {
  my $n = sysread(\*STDIN, $buf, $size, length($buf));
 
  while ($buf =~ /(.+)\n/gc) {
    $lines++;
    $_ = $1;
    /^(\d+)/gc; $ts = $1;
    /(\w)\sV/gc; $type = $1;
    /\s(\d+)\s\w/gc; $op = $1;
    /\w\s(\d+)\s/gc; $bytes = $1;
 
    report() unless $lines & $mask;
  }
  last unless $n > 0;
 
  $buf = substr($buf, pos($buf));
  $read += $n;
}
 
report(1);
 
sub report {
  my ($final) = @_;
 
  my $now = [gettimeofday];
  my $d = tv_interval(($final ? $t0 : $tl), $now);
  $tl = $now;
 
  my $l = $lines;
  $l -= $ll unless $final;
  $ll = $lines;
 
  my $r = $read;
  $r -= $rr unless $final;
  $rr = $read;
  $r >>= 20;
 
  my $l_rate = $d ? int($l / $d) : 'n/a';
my $r_rate = $d ? $r / $d : 'n/a';
 
print "\n\nFINAL REPORT:\n\n" if $final;
print "elapsed $d, lines $lines, rate $l_rate lines/sec (read "
. ($read >> 20)
. " MB, rate $r_rate MB/sec)\n";
 
if ($final) {
print "\n\n";
system("ps l | egrep 'PID|$$' | grep -v egrep");
  }
}