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
|
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
use lib dirname (__FILE__);
use TestUtils;
use TestHTTPD;
use File::Temp;
sub proxy {
my $config = shift;
exec(@_, '../src/sniproxy', '-f', '-c', $config);
}
sub worker($$$$) {
my ($hostname, $path, $port, $requests) = @_;
for (my $i = 0; $i < $requests; $i++) {
system('curl',
'-s', '-S',
'-H', "Host: $hostname",
'-o', '/dev/null',
"http://localhost:$port/$path");
if ($? == -1) {
die "failed to execute: $!\n";
} elsif ($? & 127) {
printf STDERR "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
exit 255;
} elsif ($? >> 8) {
exit $? >> 8;
}
}
# Success
exit 0;
}
sub connection_dump_files() {
my $dir = '/tmp';
opendir(my $dh, $dir)
or die("opendir(): $!");
my %files = map { $dir . '/' . $_ => 1 } grep { /^sniproxy-connections-.{6}$/ } readdir($dh);
closedir($dh);
return \%files;
}
sub dump_connections_worker($) {
my ($proxy_pid) = @_;
my $existing_dump_files = connection_dump_files();
while (kill('USR1', $proxy_pid)) {
# Sleep 100ms
select(undef, undef, undef, 0.25);
my @new_dump_files = grep { !$existing_dump_files->{$_} } keys %{connection_dump_files()};
die "sniproxy didn't dump connections" unless @new_dump_files;
foreach my $dump_file (@new_dump_files) {
unlink $dump_file;
}
}
exit 0;
}
sub main {
my $proxy_port = $ENV{SNI_PROXY_PORT} || 8080;
my $httpd_port = $ENV{TEST_HTTPD_PORT} || 8081;
my $workers = $ENV{WORKERS} || 10;
my $iterations = $ENV{ITERATIONS} || 10;
my $local_httpd = $ENV{LOCAL_HTTPD_PORT};
my $config = make_config($proxy_port, $local_httpd || $httpd_port);
my $proxy_pid = start_child('server', \&proxy, $config, @ARGV);
my $httpd_pid = start_child('server', \&TestHTTPD::httpd, port => $httpd_port) unless $local_httpd;
# Wait for proxy to load and parse config
wait_for_port(port => $httpd_port);
wait_for_port(port => $proxy_port);
start_child('dump-connections-worker', \&dump_connections_worker, $proxy_pid);
for (my $i = 0; $i < $workers; $i++) {
start_child('worker', \&worker, 'localhost', '', $proxy_port, $iterations);
}
# Wait for all our children to finish
wait_for_type('worker');
# Give the proxy a second to flush buffers and close server connections
sleep 1;
# For troubleshooting connections stuck in CLOSE_WAIT state
#kill 10, $proxy_pid;
#system("netstat -ptn | grep $proxy_pid\/sniproxy");
# For troubleshooting 100% CPU usage
#system("top -n 1 -p $proxy_pid -b");
# Orderly shutdown of the server
kill 15, $proxy_pid;
kill 15, $httpd_pid unless $local_httpd;
sleep 1;
# Delete our test configuration
unlink($config);
# Kill off any remaining children
reap_children();
}
main();
|