LWP vs HTTP::Lite vs WWW::Curl
The winner is WWW::Curl. That was expected, now should you consider LWP convenience , only if you are ready to take the speed penalty . HTTP::Lite is not a bad choice ether.
Here the full results and the very short test script
Rate lwp lite curl
lwp 604/s -- -52% -85%
lite 1259/s 108% -- -68%
curl 3922/s 549% 211% --
Here the full results and the very short test script
#:~/code/perl$ perl www_bench.pl 10000
Compare COUNT: 10000
Benchmark: timing 10000 iterations of curl, lite, lwp...
curl: 13.813 wallclock secs ( 1.21 usr + 1.32 sys = 2.53 CPU) @ 3952.57/s (n=10000)
lite: 18.466 wallclock secs ( 5.62 usr + 1.69 sys = 7.31 CPU) @ 1367.99/s (n=10000)
lwp: 27.366 wallclock secs (14.90 usr + 1.35 sys = 16.25 CPU) @ 615.38/s (n=10000)
Rate lwp lite curl
lwp 615/s -- -55% -84%
lite 1368/s 122% -- -65%
curl 3953/s 542% 189% --
#!/usr/bin/perl
#
use strict;
use Benchmark qw(cmpthese disablecache timethese :hireswallclock);
use LWP::UserAgent;
use HTTP::Lite;
use WWW::Curl::Easy;
our $cnt = $ARGV[0] || 100;
our $url = $ARGV[1] || 'http://127.0.0.1/';
our $body = 0;
disablecache;
print " Compare COUNT: $cnt\n";
my $r = timethese($cnt, { lite => sub { &lite; } , curl => sub { &curl; }, lwp => sub { &lwp; } } );
cmpthese $r;
sub lwp {
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get($url);
if ($response->is_success) {
print $response->content if ($body); # or whatever
}else {
print $response->status_line;
}
}
sub lite {
my $http = new HTTP::Lite;
my $req = $http->request($url);
if ($req eq '200') {
my $b = $http->body();
print "********** LITE \n\n$b" if $body;
}
}
sub curl {
my $curl = new WWW::Curl::Easy;
$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL,$url);
my $response_body;
open (my $fileb, ">", \$response_body);
$curl->setopt(CURLOPT_WRITEDATA,$fileb);
my $retcode = $curl->perform;
if ($retcode == 0) {
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
# judge result and next action based on $response_code
print("************ CURL \\n\n $response_body\n") if $body;
} else {
print("An error happened: ".$curl->strerror($retcode)." ($retcode)\n");
}
}

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home