Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

IO::Socket::INET slower than the LWP::UserAgent how so many?

by ibbackuptest (Initiate)
on Oct 20, 2007 at 16:32 UTC ( [id://646189]=perlquestion: print w/replies, xml ) Need Help??

ibbackuptest has asked for the wisdom of the Perl Monks concerning the following question:

test.pl
#!/usr/bin/perl use strict; use IO::Socket; use LWP::UserAgent; my $host = "bbs.chinaunix.net"; my $html = ""; my $start = ""; my $end = ""; my $ua = LWP::UserAgent -> new; $ua -> timeout( 30 ); $ua -> agent( 'Mozilla/4.76 [en] (Win98; U)' ); $ua -> default_header( 'Pragma' => 'no-cache', 'Accept' => '*/*'); $start = time; print "LWP strat " . $start . "\n"; my $resp = $ua -> get( "http://$host/" ); if ( $resp -> is_success ) { $html = $resp->status_line ."\n\n"; $html .= $resp->headers_as_string ."\n\n"; $html .= $resp -> content; } $end = time; print "LWP end " . $end . "\n"; print "LWP time = " . ($end - $start) . "\n\n"; $html = ""; my $geturl=qq~GET / HTTP/1.1 Accept: */* User-Agent: Mozilla/4.76 [en] (Win98; U) Host: $host Connection: Keep-Alive ~; $start = time; print "IO::Socket::INET strat " . $start . "\n"; my $socket = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"$host", Pe +erPort=>80, Timeout => 30); $socket->autoflush(1); my @html; if($socket){ print $socket "$geturl"; print "IO::Socket::INET time1 " . time . "\n"; @html=<$socket>; # Attention here, why spent so much time? print "IO::Socket::INET time2 " . time . "\n"; close($socket); }else{ } $html = join("",@html); $end = time; print "IO::Socket::INET end " . $end . "\n"; print "IO::Socket::INET time = " . ($end - $start) . "\n\n";
Results:
C:\>perl test.pl LWP strat 1192833431 LWP end 1192833432 LWP time = 1 IO::Socket::INET strat 1192833432 IO::Socket::INET time1 1192833432 IO::Socket::INET time2 1192833448 IO::Socket::INET end 1192833448 IO::Socket::INET time = 16
Why so many differences?
@html=<$socket>; # Attention here, why spent so much time?
Solutions? Thank you.

Replies are listed 'Best First'.
Re: IO::Socket::INET slower than the LWP::UserAgent how so many?
by sfink (Deacon) on Oct 20, 2007 at 16:45 UTC
    You told it to keep the connection alive, and then tried to read until EOF (when the connection closes). That's not a good combination.

    You can either do Connection: close instead of keeping it alive, or you can do what LWP is doing and parse out the Content-Length header and only read that many bytes.

Re: IO::Socket::INET slower than the LWP::UserAgent how so many?
by Cop (Initiate) on Oct 20, 2007 at 17:20 UTC

    You are lucky that your scipt didn't hang or lost data. If the data received is a bit different, that's what will happen.At least parse the header and understand the size of the data.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://646189]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found