I couldn't find the original scripts (it was a good while ago and the point was proven then) so I've knocked up these scripts today which follow the same principle. If you can point out the errors in what I've done that would be most helpful.
Firstly, here's the trivial non-plack CGI:
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Lite;
my $cgi = CGI::Lite->new;
$cgi->parse_form_data;
print "Content-Type: text/plain\n\n";
$cgi->print_data;
exit;
Here's the equivalent PSGI:
use strict;
use warnings;
use Plack::Request;
my $app = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my %params = %{$req->parameters};
my $body = '';
while (my ($k, $v) = each %params) {
$body .= "$k = $v\n";
}
my $res = $req->new_response;
$res->status (200);
$res->headers ({ 'Content-Type' => 'text/plain' });
$res->body ($body);
$res->finalize;
}
And finally the wrapper to turn the PSGI into a CGI:
#!/usr/bin/perl
use strict;
use warnings;
use Plack::Loader;
my $app = Plack::Util::load_psgi("light.psgi");
Plack::Loader->auto->run($app);
To turn this into a "real" test I've run these through Apache with both client and server on the same host (ie. no network issues). The 2 cgi scripts were called with a single key-value pair in the query string (foo=bar, since you ask) and produced the same content as a result. Each script was called 11 times (1 as PoC and then another 10)
The timings come from the apache log (with %D) and are as follows (all values are microseconds).
| Normal CGI | CGI through Plack |
Maximum | 21371 | 93761 |
Minimum | 12494 | 80521 |
Mean | 14999 | 86481 |
So, looking at the mean request time the plack version is
5.77 times slower than the plain CGI.
Now, these are small samples and are wallclock times so YMMV and I encourage you to re-run the tests on your own platforms to see how they compare but this crude example reflects pretty closely what I originally saw and is enough of a penalty to be avoided (for me).
|