-
Notifications
You must be signed in to change notification settings - Fork 32
/
bench-sample
executable file
·75 lines (63 loc) · 1.51 KB
/
bench-sample
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
#!/usr/bin/perl
# public domain
# benchmark sampler by dormando.
use warnings;
use strict;
use IO::Socket::INET;
use Time::HiRes;
$|++;
my $host = shift @ARGV;
my $MAX_RUNS = shift @ARGV;
my $SAMPLE_TIME = shift @ARGV || 2;
my @TO_SAMPLE = @ARGV;
unless (@TO_SAMPLE) {
@TO_SAMPLE = ('cmd_get');
}
my $longest = 0;
for (@TO_SAMPLE) {
$longest = length($_) if length($_) > $longest;
}
$longest++;
my $s = connect_to($host);
die $@ unless $s;
my %avgs = ();
my $runs = 0;
while (1) {
sample();
last if ($MAX_RUNS == -1 ||
($MAX_RUNS != 0 && $runs == $MAX_RUNS));
}
sub sample {
my $one = mc_info($s);
my $one_time = Time::HiRes::time;
sleep $SAMPLE_TIME;
my $two = mc_info($s);
my $two_time = Time::HiRes::time;
my $elapsed = $two_time - $one_time;
$runs++;
for my $stat (@TO_SAMPLE) {
my $per_sec = ($two->{$stat} - $one->{$stat}) / $elapsed;
if (!$avgs{$stat}) {
$avgs{$stat} = $per_sec;
} else {
$avgs{$stat} += $per_sec;
}
printf "%-${longest}s avg/s (%2s/s sample): %-20s [%s]\n", $stat,
$SAMPLE_TIME, $per_sec, $avgs{$stat} / $runs;
}
}
sub mc_info {
my $s = shift;
print $s "stats\r\n";
my %stats = ();
while (my $line = <$s>) {
last if $line =~ m/^END/;
if ($line =~ m/^STAT (\S+) (\d+)/) {
$stats{$1} = $2;
}
}
return \%stats;
}
sub connect_to {
return IO::Socket::INET->new(PeerAddr => $_[0], Timeout => 3);
}