line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::RedisTop::Redis; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
5
|
1
|
|
|
1
|
|
41830
|
use IO::Socket::INET; |
|
1
|
|
|
|
|
34233
|
|
|
1
|
|
|
|
|
9
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub new { |
8
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
9
|
0
|
|
|
|
|
|
my (%args) = @_; |
10
|
|
|
|
|
|
|
|
11
|
0
|
|
0
|
|
|
|
my $host = $args{host} || '127.0.0.1'; |
12
|
0
|
|
0
|
|
|
|
my $port = $args{port} || '6379'; |
13
|
0
|
|
|
|
|
|
my $pass = $args{pass}; |
14
|
|
|
|
|
|
|
|
15
|
0
|
|
|
|
|
|
my $server = "$host:$port"; |
16
|
0
|
0
|
|
|
|
|
my $s = IO::Socket::INET->new( |
17
|
|
|
|
|
|
|
PeerAddr => $server, |
18
|
|
|
|
|
|
|
Proto => 'tcp', |
19
|
|
|
|
|
|
|
) or die "[$server] socket connect error: $!"; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# auth |
22
|
0
|
0
|
|
|
|
|
if ($pass) { |
23
|
0
|
|
|
|
|
|
$s->print("AUTH $pass \r\n"); |
24
|
0
|
0
|
|
|
|
|
<$s> || die "[$server] socket auth error: $!"; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my $self = bless { |
28
|
|
|
|
|
|
|
socket => $s, |
29
|
|
|
|
|
|
|
}, $class; |
30
|
0
|
|
|
|
|
|
$self; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub info { |
34
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my $buf = $self->command('INFO'); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
my $stats = {}; |
39
|
0
|
|
|
|
|
|
for my $row (split(/\r\n/, $buf)) { |
40
|
0
|
0
|
|
|
|
|
next if $row =~ /^#/; |
41
|
0
|
0
|
|
|
|
|
next if $row =~ /^$/; |
42
|
0
|
|
|
|
|
|
my ($key, $val) = split(/:/, $row); |
43
|
0
|
|
|
|
|
|
$stats->{$key} = $val; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
return $stats; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub config { |
50
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
51
|
0
|
|
|
|
|
|
return $self->command("CONFIG", "GET", "*"); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub slowlog { |
55
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
56
|
0
|
|
|
|
|
|
return $self->command("SLOWLOG", "LEN"); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub command { |
60
|
0
|
|
|
0
|
0
|
|
my ($self, @command) = @_; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
my $s = $self->{socket}; |
63
|
0
|
|
|
|
|
|
my $req = $self->request(@command); |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
$s->send($req); |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
my $ret = $s->recv( my $buffer, 131072 ); |
68
|
0
|
|
|
|
|
|
my @lines = split(/\r\n/, $buffer); |
69
|
0
|
|
|
|
|
|
my $header = $self->row_parser($lines[0]); |
70
|
0
|
0
|
|
|
|
|
return $buffer if $header->{type} eq 'itemlen'; |
71
|
0
|
0
|
|
|
|
|
return $header->{value} if $header->{type} eq 'number'; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my %stats = (); |
74
|
0
|
|
|
|
|
|
my $key; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
for my $line (@lines) { |
77
|
0
|
|
|
|
|
|
my $row = $self->row_parser($line); |
78
|
0
|
0
|
|
|
|
|
if($row->{type} eq 'line') { |
79
|
0
|
0
|
|
|
|
|
unless($key) { |
80
|
0
|
|
|
|
|
|
$key = $row->{value}; |
81
|
|
|
|
|
|
|
} else { |
82
|
0
|
|
|
|
|
|
$stats{$key} = $row->{value}; |
83
|
0
|
|
|
|
|
|
$key = undef; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
return \%stats; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub row_parser { |
92
|
0
|
|
|
0
|
0
|
|
my ($self, $row) = @_; |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
if($row =~ /^(\*|\$|\:)+(\w+)/) { |
95
|
0
|
0
|
|
|
|
|
if($1 eq '*') { # header |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
96
|
|
|
|
|
|
|
return { |
97
|
0
|
|
|
|
|
|
type => 'rowlen', |
98
|
|
|
|
|
|
|
value => $2, |
99
|
|
|
|
|
|
|
}; |
100
|
|
|
|
|
|
|
} elsif($1 eq '$') { # next length |
101
|
|
|
|
|
|
|
return { |
102
|
0
|
|
|
|
|
|
type => 'itemlen', |
103
|
|
|
|
|
|
|
value => $2, |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
} elsif($1 eq ':') { # number |
106
|
|
|
|
|
|
|
return { |
107
|
0
|
|
|
|
|
|
type => 'number', |
108
|
|
|
|
|
|
|
value => $2, |
109
|
|
|
|
|
|
|
}; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
$row =~ s/\r\n//; |
114
|
|
|
|
|
|
|
return { |
115
|
0
|
|
|
|
|
|
type => 'line', |
116
|
|
|
|
|
|
|
value => $row, |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub request { |
121
|
0
|
|
|
0
|
0
|
|
my ($self, @args) = @_; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my $nl = "\015\012"; |
124
|
0
|
|
|
|
|
|
my $req = sprintf('*%d%s', scalar(@args), $nl); |
125
|
0
|
|
|
|
|
|
$req .= sprintf('$%d%s%s%s', length($_), $nl, $_, $nl) for @args; |
126
|
0
|
|
|
|
|
|
return $req; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |