forked from chrisridd/opendj-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slowops
executable file
·248 lines (201 loc) · 5.65 KB
/
slowops
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/perl -w
# Find particular operations and display their etime distribution
use strict;
use warnings;
$::VERSION = 0.3;
sub version()
{
print STDERR "$0 version $::VERSION\n";
exit 0;
}
sub usage()
{
print STDERR "Usage: $0 [-s] [-j] [-e etime] [op]\n";
exit 1;
}
my %opt = (
's' => 0, # Summary mode
'j' => 0, # JSON logs
'e' => -1, # Etime
'h' => 0, # Help
'v' => 0, # Version
);
my $op = 'SEARCH';
while (@ARGV) {
my $arg = shift @ARGV;
if ($arg =~ m{^-s$|^--summary$}) {
$opt{'s'} = 1;
} elsif ($arg =~ m{^-j$|^--json$}) {
# JSON::XS is significantly faster than JSON::PP
if (eval{require JSON::XS}) {
$opt{j} = JSON::XS->new;
} elsif (eval{require JSON::PP}) {
$opt{j} = JSON::PP->new;
} else {
die "Cannot locate a JSON module\n";
}
} elsif ($arg =~ m{^-e$|^--etime$}) {
$opt{e} = shift @ARGV;
usage unless defined $opt{e};
} elsif ($arg =~ m{^-h$|^--help$}) {
$opt{h} = 1;
} elsif ($arg =~ m{^-v$|^--version$}) {
$opt{v} = 1;
} else {
$op = $arg;
last;
}
}
version if $opt{v};
usage if $opt{h};
my %details;
my %quantize;
my $scale = 0;
# Calculating mean
my $sum = 0;
my $values = 0;
# Calculating median
my $lowest = undef;
my $highest = undef;
# Calculating mode
my %mode;
sub updatestats
{
my $etime = shift;
my $details = shift;
$sum += $etime;
$values++;
$lowest = $etime unless defined($lowest);
$highest = $etime unless defined($highest);
$lowest = $etime if $etime < $lowest;
$highest = $etime if $etime > $highest;
$mode{$etime} = 0 unless exists $mode{$etime};
$mode{$etime}++;
my $bucket = 1;
do {
$quantize{$bucket} = [] unless exists $quantize{$bucket};
if ($etime <= $bucket) {
push @{$quantize{$bucket}}, $details;
if (scalar(@{$quantize{$bucket}}) > $scale) {
$scale = scalar(@{$quantize{$bucket}});
}
return;
}
$bucket *= 2;
} while (1);
}
while (<>) {
chomp;
# JSON log format
if ($opt{'j'}) {
my $record = $opt{j}->decode($_);
next unless $record->{request}->{operation} eq $op;
my $etime = $record->{response}->{elapsedTime};
print "$_\n" if $opt{'e'} != -1 && $etime >= $opt{'e'};
updatestats($etime, $_) if $opt{'s'};
next;
}
# Traditional log format
if (my ($tuple) = m{$op REQ.*(conn=-?\d+ op=\d+ msgID=\d+)}) {
$details{$tuple} = $_;
next;
}
# Traditional log format
if (my ($tuple,$remainder) = m{$op RES .*(conn=-?\d+ op=\d+ msgID=\d+)( +.*)}) {
if (exists $details{$tuple}) {
my $details = $details{$tuple};
delete $details{$tuple};
my $etime = "";
$etime = $1 if $remainder =~ m{ etime=(\d+)};
if ($opt{'e'} != -1 && $etime >= $opt{'e'}) {
print "$details$remainder\n";
}
updatestats($etime, $details) if $opt{'s'};
}
next;
}
# Combined log format
if (my ($tuple,$remainder) = m{$op .*(conn=-?\d+ op=\d+ msgID=\d+)( +.*)}) {
my $etime = "";
$etime = $1 if $remainder =~ m{ etime=(\d+)};
if ($opt{'e'} != -1 && $etime >= $opt{'e'}) {
print "$_\n";
}
updatestats($etime, $_) if $opt{'s'};
next;
}
}
exit 0 unless $opt{'s'};
format STDOUT_TOP =
value ------------------------Distribution------------------------ count
.
format =
@>>>>> |@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<
$::bucket, $::bar, $::count
.
foreach our $bucket (sort {$a <=> $b} (keys %quantize)) {
our $count = scalar(@{$quantize{$bucket}});
our $bar = "@" x ($count * 60.0 / $scale);
write;
}
if ($values > 0) {
my $key_max = undef;
my $val_max = 0;
foreach (keys %mode) {
if ($mode{$_} > $val_max) {
$val_max = $mode{$_};
$key_max = $_;
}
}
printf "Mean: %.02f\n", $sum / $values;
printf "Median: %.02f\n", ($highest - $lowest) / 2;
printf "Mode: %.02f\n", $key_max;
}
1;
__END__
=head1 NAME
slowops - analyze operation performance in OpenDJ log files
=head1 SYNOPSIS
slowops [-j] -s op
slowops [-j] -e min op
slowops -h
slowops -v
=head1 DESCRIPTION
OpenDJ access log files record the elapsed time ("etime") taken by each LDAP
operation. However if the server is busy finding slow operations is awkward, and
finding any trends in the data is difficult.
This tool analyzes OpenDJ access logs in a couple of ways. The statistics mode
shows a graph of operation performance, and some basic figures like
mean/mode/median. The etime mode reports all operations that take longer than a
certain time to complete.
=head1 OPTIONS
=over
=item B<-j> parse JSON-formatted logs.
=item B<-h> display help text.
=item B<-v> report the tool version.
=item B<-s> report statistics after reading all the data. The graph output is a
power-of-two frequency distribution, modelled on dtrace(1)'s quantize function.
=item B<-e> report operations which have etimes larger than C<I<min>>.
=item B<op> the LDAP operation being analyzed, which should be one of C<BIND>,
C<SEARCH>, C<ADD>, C<DELETE>, C<MODIFY>, C<MODIFYDN>.
=back
=head1 EXAMPLE
$ slowops -s ADD < access
value ------------------------Distribution------------------------ count
1 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 193
2 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 317
4 | 3
8 | 3
16 | 1
Mean: 1.68
Median: 4.50
Mode: 2.00
=head1 SEE ALSO
L<dtrace(1)>
L<http://opendj.forgerock.org>
=head1 AUTHOR
Chris Ridd E<lt>[email protected]<gt>
=head1 COPYRIGHT
Copyright (c) 2012-2017 Chris Ridd. All rights reserved. This tool is free
software; you can redistribute it and/or modify it under the same terms as Perl
itself.