use strict; use warnings; use Benchmark qw(:all); use WWW::Curl::Easy; use HTTP::Cookies; use HTTP::Request::Common qw(POST); use LWP::UserAgent; my $cj = HTTP::Cookies->new(file => 'cookieslwp', autosave => 1); my $ua = LWP::UserAgent->new; $ua->cookie_jar($cj); $ua->default_header('Content-Type' => 'text/plain;charset=UTF-8'); my $curl_hdr = ''; my $curl_hdr_fh = undef; open $curl_hdr_fh, ">", \$curl_hdr; my $curlp = WWW::Curl::Easy->new(); $curlp->setopt(CURLOPT_NOPROGRESS, 1); # shut off the built-in progress meter $curlp->setopt(CURLOPT_HEADER, 0); # don't include hdr in body $curlp->setopt(CURLOPT_ENCODING, 'gzip'); $curlp->setopt(CURLOPT_PROXY, ''); # no proxy open $curl_hdr_fh, "+>", \$curl_hdr; $curlp->setopt(CURLOPT_WRITEHEADER, $curl_hdr_fh); $curlp->setopt(CURLOPT_COOKIEFILE, 'cookiescurl'); $curlp->setopt(CURLOPT_COOKIEJAR, 'cookiescurl'); # either of the following speeds curl up massively #$curlp->setopt(CURLOPT_TCP_NODELAY, 1); #$curlp->setopt(CURLOPT_FORBID_REUSE, 1); logincurl(); loginlwp(); timethese(1000, { curl => sub {postcurl2()}, lwp => sub {postlwp2()}}); sub loginlwp { my $url = 'http://xxx.yyy.zzz:82/v1'; my %parameters = (method => 'login', username => 'xxx', password => 'yyy'); my $request = POST $url, \%parameters; $request->content_type('application/x-www-form-urlencoded;charset=utf-8'); my $response = $ua->request($request); if (!$response->is_success) { die "Failed to get url - $response->code, $response->status_line"; } print $response->content, "\n"; } sub logincurl { seek $curl_hdr_fh, 0, 0; truncate $curl_hdr_fh, 0; my $url = 'http://xxx.yyy.zzz:82/v1'; $curlp->setopt(CURLOPT_URL, $url); my $form = WWW::Curl::Form->new; $form->formadd('method', 'login'); $form->formadd('username', 'xxx'); $form->formadd('password', 'yyy'); $curlp->setopt(CURLOPT_HTTPPOST, $form); my @hdrs = ('Expect:'); $curlp->setopt(CURLOPT_HTTPHEADER, \@hdrs); my $response_body; $curlp->setopt(CURLOPT_WRITEDATA,\$response_body); my $retcode = $curlp->perform; my $response_code = $curlp->getinfo(CURLINFO_HTTP_CODE); if ($retcode != 0 || ($response_code != 200 && $response_code != 304)) { die "Failed to get url - $response_code, ", $curlp->errbuf; } print $response_body, "\n\n"; } sub postlwp2 { my $url = 'http://xxx.yyy.zzz:82/v1/data/xxx.dat'; my %parameters = (method => 'xxx'); my $request = POST $url, \%parameters; $request->content_type('application/x-www-form-urlencoded;charset=utf-8'); my $response = $ua->request($request); if (!$response->is_success) { die "Failed to get url - $response->code, $response->status_line"; } # print $response->content, "\n"; } sub postcurl2 { seek $curl_hdr_fh, 0, 0; truncate $curl_hdr_fh, 0; my $url = 'http://xxx.yyy.zzz:82/v1/data/xxx.dat'; $curlp->setopt(CURLOPT_URL, $url); my $form = WWW::Curl::Form->new; $form->formadd('method', 'account'); $curlp->setopt(CURLOPT_HTTPPOST, $form); my @hdrs = ('Expect:'); $curlp->setopt(CURLOPT_HTTPHEADER, \@hdrs); my $response_body; $curlp->setopt(CURLOPT_WRITEDATA,\$response_body); my $retcode = $curlp->perform; my $response_code = $curlp->getinfo(CURLINFO_HTTP_CODE); if ($retcode != 0 || ($response_code != 200 && $response_code != 304)) { die "Failed to get url - $response_code, ", $curlp->errbuf; } #print $response_body, "\n"; } #### Benchmark: timing 1000 iterations of curl, lwp... curl: 48 wallclock secs ( 0.10 usr + 0.08 sys = 0.18 CPU) @ 5555.56/s (n =1000) (warning: too few iterations for a reliable count) lwp: 16 wallclock secs ( 4.68 usr + 0.69 sys = 5.37 CPU) @ 186.22/s (n= 1000) #### Benchmark: timing 1000 iterations of curl, lwp... curl: 12 wallclock secs ( 0.31 usr + 0.13 sys = 0.44 CPU) @ 2272.73/s (n =1000) lwp: 16 wallclock secs ( 4.66 usr + 0.55 sys = 5.21 CPU) @ 191.94/s (n= 1000)