一次实现多个连接的轻松方法是用“prefork”替换“daemon”(它甚至可能比 hypnotoad 更有效):
从:
app->start('daemon', '-l', 'http://*:8000');
到:
app->start('prefork', '-l', 'http://*:8000');
您可以使用以下方法自行测试:
#!/usr/bin/perl -wl
use Mojolicious::Lite;
get '/' => sub {
my $self = shift;
for(my $wait = 10; $wait > 0; $wait--) {
sleep(1);
}
$self->render(text => "OK");
};
# Uncomment to test hypnotoad. execute: hypnotoad ./this_script.pl
#app->config(hypnotoad => {listen => ['http://*:8000']});
#app->start;
# Test Daemon: (Uncomment next line)
#app->start('daemon', '-l', 'http://*:8000');
# Test Prefork: (Uncomment next line)
app->start('prefork', '-l', 'http://*:8000');
这是基准:
$ ab -n 10 -c 10 -s 120 http://localhost:8000/ :
结果:
(totals) min mean[+/-sd] median max total time
daemon: 11008 92020 28464.5 101021 101022 101.022 seconds
hypnotoad: 10017 31525 18811.0 49030 49033 49.036 seconds
prefork: 20018 24020 5165.0 20020 30022 30.029 seconds
理想情况下,总时间将接近 10 秒......但我还没有找到改善这些时间的方法(还)。