public
Description: Small examples, or proof of concepts
Homepage:
Clone URL: git://github.com/melo/perl-examples.git
perl-examples / nfsd_report_bench / bench_splitters.pl
100755 135 lines (108 sloc) 3.423 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env perl
 
use strict;
use warnings;
use Benchmark qw( cmpthese timethese );
 
my $line = '1255947719 3a163121 897 01c633c7 2049 TCP request V3 2eff9a5e 6 getattr null 36 2';
 
system("ps l | egrep 'PID|$$' | grep -v egrep");
 
 
print "\nComparing split with regexp to extract data from lines:\n\n";
 
my @f;
my ($ts, $type, $op, $bytes);
 
cmpthese(0, {
    split => sub { @f = split(' ', $line) },
    regexp => sub {
        $_ = $line;
        /^(\d+)/gc; $ts = $1;
        /(\w)\sV/gc; $type = $1;
        /\s(\d+)\s/gc; $op = $1;
        /\w\s(\d+)\s/gc; $bytes = $1;
    },
});
 
print "\n\nMeasuring impact of the bookkeeping:\n\n";
 
my (@req_ops, @res_ops);
my (%write_ops, %write_bytes, %read_ops, %read_bytes);
my ($read, $write);
($type, $op, $ts, $bytes) = (undef, undef, 1255947719, 500);
 
timethese(0, {
    t_and_7 => sub {
        $type = 't'; $op = '7';
        
        if ($type eq 't') {
            $req_ops[$op]++;
            if ($op == 7) {
                $write += $bytes;
                $ts -= $ts % 3600; # Normalize by the hour.
                $write_ops{$ts}++;
                $write_bytes{$ts} += $bytes;
            }
        } elsif ($type eq 'e') {
            $res_ops[$op]++;
            if ($op == 6) {
                $read += $bytes;
 
                $ts -= $ts % 3600; # Normalize by the hour.
 
                $read_ops{$ts}++;
                $read_bytes{$ts} += $bytes;
            }
        }
    },
 
    t_and_other => sub {
        $type = 't'; $op = '1';
        
        if ($type eq 't') {
            $req_ops[$op]++;
            if ($op == 7) {
                $write += $bytes;
                $ts -= $ts % 3600; # Normalize by the hour.
                $write_ops{$ts}++;
                $write_bytes{$ts} += $bytes;
            }
        } elsif ($type eq 'e') {
            $res_ops[$op]++;
            if ($op == 6) {
                $read += $bytes;
 
                $ts -= $ts % 3600; # Normalize by the hour.
 
                $read_ops{$ts}++;
                $read_bytes{$ts} += $bytes;
            }
        }
    },
    
    e_and_6 => sub {
        $type = 'e'; $op = '6';
        
        if ($type eq 't') {
            $req_ops[$op]++;
            if ($op == 7) {
                $write += $bytes;
                $ts -= $ts % 3600; # Normalize by the hour.
                $write_ops{$ts}++;
                $write_bytes{$ts} += $bytes;
            }
        } elsif ($type eq 'e') {
            $res_ops[$op]++;
            if ($op == 6) {
                $read += $bytes;
 
                $ts -= $ts % 3600; # Normalize by the hour.
 
                $read_ops{$ts}++;
                $read_bytes{$ts} += $bytes;
            }
        }
    },
 
    e_and_other => sub {
        $type = 'e'; $op = '1';
        
        if ($type eq 't') {
            $req_ops[$op]++;
            if ($op == 7) {
                $write += $bytes;
                $ts -= $ts % 3600; # Normalize by the hour.
                $write_ops{$ts}++;
                $write_bytes{$ts} += $bytes;
            }
        } elsif ($type eq 'e') {
            $res_ops[$op]++;
            if ($op == 6) {
                $read += $bytes;
 
                $ts -= $ts % 3600; # Normalize by the hour.
 
                $read_ops{$ts}++;
                $read_bytes{$ts} += $bytes;
            }
        }
    },
});
 
print "\n\n";
system("ps l | egrep 'PID|$$' | grep -v egrep");