/
ab.pl
executable file
·69 lines (57 loc) · 1.5 KB
/
ab.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use Plack::Loader;
use Test::TCP;
use Getopt::Long;
my $app = 'eg/dot-psgi/Hello.psgi';
my $ab = 'ab -n 100 -c 10 -k';
my %backends = (
AnyEvent => 'AnyEvent',
Standalone => 0,
ServerSimple => 'HTTP::Server::Simple',
'Mojo::Prefork' => 'Mojo',
Coro => 'Net::Server::Coro',
'Danga::Socket' => 'Danga::Socket',
);
my @backends;
for my $impl (sort keys %backends) {
my $req = $backends{$impl};
push @backends, $impl if !$req or eval "require $req; 1";
}
warn "Testing implementations: ", join(", ", @backends), "\n";
GetOptions(
'app=s' => \$app,
'bench=s' => \$ab,
) or die;
&main;
sub main {
print "app: $app\n";
print "ab: $ab\n";
for my $impl_class (@backends) {
run_one($impl_class);
}
}
sub run_one {
my $impl_class = shift;
my $port = Test::TCP::empty_port();
print "-- impl_class: $impl_class\n";
my $pid = fork();
if ($pid > 0) { # parent
Test::TCP::wait_port($port);
print `$ab http://127.0.0.1:$port/ | grep 'Requests per '`;
kill 'TERM' => $pid;
wait();
} else {
my $handler = load_handler(Cwd::cwd() . "/". $app) or die ($! || $@);
my $impl = Plack::Loader->load($impl_class, port => $port);
$impl->run($handler);
$impl->run_loop if $impl->can('run_loop'); # run event loop
}
}
sub load_handler {
my $file = shift;
return do $file;
}