-
Notifications
You must be signed in to change notification settings - Fork 32
/
latency-sampler
executable file
·151 lines (138 loc) · 4.08 KB
/
latency-sampler
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
#!/usr/bin/perl
# Copyright 2018 Dormando
# Licensed under same terms as mc-crusher.c
# First pass at a paced sampled fake-client latency aggregator.
#
# Goal: Show latency clustering while under load tests.
# Bubbles should appear in the histogram as slow requests.
# Requests faster than the pacing minimum sleep until the next increment
# (ie: if < 1ms, sleep until 1ms has passed before sending next request)
#
# Uses blocking IO to simplify timing: This is the absolute time it takes for
# a request/response cycle, rather than how long a request stayed queued
# before being accessed in an event loop on the client side.
#
# Correlate with throughput numbers and system CPU metrics to determine if
# bubbles are system pauses or slow requests.
#
# Future:
# This beats what I had before, but what I want is a threadpool and a pacer
# thread, dispatching up to N outstanding blocking requests.
# Watching how backlogs queue, dequeue, and overflow can easily visualize
# hangs vs slow requests. Can also round robin between client connections
# (worker threads on the server).
use warnings FATAL => 'all';
use strict;
use IO::Socket::INET;
use Time::HiRes qw/time sleep usleep/;
use Getopt::Long qw/:config no_ignore_case/;
use FindBin;
my $server = '127.0.0.1';
my $port = '11211';
my $prefix = 'foo';
my $key_max = 5000;
my $random = 0;
my $dump_rate = 3;
my $pace_time = 1000; # microseconds
my $max_bucket = 8;
$|++;
if (!Getopt::Long::GetOptions(
'server|s=s' => \$server,
'port|p=s' => \$port,
'prefix|f=s' => \$prefix,
'key_max|m=i' => \$key_max,
'random|r' => \$random,
'pace_time=i' => \$pace_time,
'dump_rate=i' => \$dump_rate,
)) {
die "Bad arguments";
}
# record all samples and dump at the end
my @samples = ();
my @hist = ();
my @global_hist = ();
my $out_of_bounds = 0;
$SIG{INT} = sub {
print "--- FINAL ---\n";
for (0..$max_bucket) {
$global_hist[$_] += $hist[$_];
}
print_hist('global');
print "---\n";
for (@samples) {
print $_->[0], ',', $_->[1], "\n";
}
exit;
};
sub print_hist {
my $global = shift;
print "--------------------\n";
my @hdr = ('1us', '10us', '100us', '1ms', '10ms', '100ms', '1s',
'10s', '100s');
for (0..scalar(@hist)-1) {
my $h = shift @hdr;
printf "%5s ", $h if $h;
if ($global) {
printf " %d\n", $global_hist[$_];
} else {
printf " %d\n", $hist[$_];
}
}
if ($out_of_bounds > 0) {
print "OOB: $out_of_bounds\n";
}
for (0..$max_bucket) {
$global_hist[$_] += $hist[$_];
$hist[$_] = 0;
}
}
# If the request processing took less than a millisecond, sleep for a bit.
sub pace {
return if $pace_time == 0;
my $start = shift;
my $now = time();
my $e = ($now - $start) * 1000000;
if ($e < $pace_time) {
usleep($pace_time - $e);
}
}
# Build a dumpable histogram
my $sock = IO::Socket::INET->new(PeerAddr => "$server:$port",
Timeout => 4);
die "$server:$port $!" unless $sock;
for (0..$max_bucket) {
$hist[$_] = 0;
$global_hist[$_] = 0;
}
# We seed the random value after the request to make the pace timing simpler
my $next_rand = 1;
my $last_print = time();
while (1) {
my $start = time();
print $sock "get ${prefix}$next_rand\r\n";
my $val = <$sock>;
my $end = time();
# We don't time past this point since the header is written to the network
# at the same time as the value, and we don't want to be timing perl
# processing.
# FIXME: Parse the line and read full value
if ($val =~ m/^VALUE/) {
while ($val !~ m/^END/) {
$val = <$sock>;
}
}
my $e = int(($end - $start) * 1000000);
push(@samples, [$start, $e]);
my $bucket = int(log($e)/log(10));
# Egregiously slow requests
if ($bucket > $max_bucket) {
$out_of_bounds++;
}
$next_rand = int(rand($key_max))+1;
pace($start);
$hist[$bucket]++;
if ($dump_rate && $start - $last_print >= $dump_rate) {
print_hist();
$last_print = time();
}
}