Hi I have a web application which needs to be polled by a perl script. This perl script will be used as a plugin in Nagios so it has to perform as fast as possible.
The http request itself is just a simple http post.
My first choice would be LWP::UserAgent, but it appears to be quite slow. I compared the speed of LWP::UserAgent by letting my Perl script use /usr/bin/curl binary instead, which is to my suprize a lot faster.
Query using LWP::UserAgent
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
my $client = new LWP::UserAgent;
my $json = new JSON;
my %request=('commandlet'=>'memory','output'=>'json');
my $json_request = $json->encode(\%request);
my $web_request = $client->post('http://localhost:12345/query',{ usern
+ame => 'default',
password => 'changeme',
json => $json_request
});
print $web_request->content;
Query using Curl:
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
my $json = new JSON;
my %request=('commandlet'=>'memory','output'=>'json');
my $json_data=$json->encode(\%request);
my $command=sprintf"/usr/bin/curl -d username='%s' -d password='%s' --
+data-urlencode json='%s' http://localhost:12345/query 2>/dev/null|","
+default","changeme",$json_data;
open FH,$command;
while (<FH>){
print $_;
}
close FH;
If I write a bash script which iterates 100 times over both scripts I get following results:
Using curl:
real 0m4.006s
user 0m3.060s
sys 0m0.740s
Using LWP
real 0m12.978s
user 0m11.405s
sys 0m1.184s
I'm running Ubuntu 9.1 and LWP::UserAgent has version "5.829"
Has anyone an idea why LWP::UserAgent is so slow, or is this rather normal?
How could I speed up a simple post like this without the need to use the external binary curl all the time?
Thanks,
Jelle
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.