Created
June 29, 2009 02:40
-
-
Save tokuhirom/137442 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use Coro; | |
use LWP::Simple; | |
use Coro::LWP; | |
use Time::HiRes 'time'; | |
use GTop; | |
my @sites = qw( | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=windows&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=G%83X%83%7C%83b%83g&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=%83%89%83u&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=hyoshiok&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=%93%EC%8F%EA&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=%92e&x=0&y=0 | |
); | |
&get1; | |
&get2; | |
exit; | |
sub get1() { | |
my $btime = time(); | |
print "TIME1=" .(time-$btime). "\n"; | |
my @coros; | |
my @results; | |
my $idx=0; | |
foreach my $site ( @sites ) { | |
push @coros, async { | |
push @results, ($idx++) . ':'.length(LWP::Simple::get($site)); | |
}; | |
} | |
print "MEMORY=" . commify(GTop->new->proc_mem($$)->size) . "\n"; | |
$_->join for @coros; | |
foreach my $result ( @results ) { | |
print $result, "\n"; | |
} | |
print "TIME1=" .(time-$btime). "\n"; | |
} | |
sub get2() { | |
my $btime = time(); | |
print "TIME2=" .(time-$btime). "\n"; | |
my $idx=0; | |
foreach my $site ( @sites ) { | |
print $idx.":".length(LWP::Simple::get($site)), "\n"; | |
$idx++; | |
} | |
print "TIME2=" .(time-$btime). "\n"; | |
} | |
sub commify { | |
local $_ = shift; | |
1 while s/((?:\A|[^.0-9])[-+]?\d+)(\d{3})/$1,$2/s; | |
return $_; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use LWP::Simple; | |
use threads; | |
use Thread::Queue; | |
use Time::HiRes 'time'; | |
use GTop; | |
my @sites = qw( | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=windows&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=G%83X%83%7C%83b%83g&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=%83%89%83u&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=hyoshiok&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=%93%EC%8F%EA&x=0&y=0 | |
http://www.amazon.co.jp/s/ref=nb_ss_gw?__mk_ja_JP=%83J%83%5E%83J%83i&url=search-alias%3Daps&field-keywords=%92e&x=0&y=0 | |
); | |
my @threads; | |
my $queue = new Thread::Queue; | |
&get1; | |
&get2; | |
exit; | |
sub get1() { | |
my $btime = time(); | |
print "TIME1=" .(time-$btime). "\n"; | |
my $idx=0; | |
foreach my $site ( @sites ) { | |
push @threads, threads->new(\&_search, $site, $idx++); | |
} | |
print "MEMORY=" . commify(GTop->new->proc_mem($$)->size) . "\n"; | |
foreach (@threads) { $_->join; } | |
foreach my $site ( @sites ) { | |
my $xml = $queue->dequeue(); | |
print $xml, "\n"; | |
} | |
print "TIME1=" .(time-$btime). "\n"; | |
} | |
sub get2() { | |
my $btime = time(); | |
print "TIME2=" .(time-$btime). "\n"; | |
my $idx=0; | |
foreach my $site ( @sites ) { | |
print $idx.":".length(LWP::Simple::get($site)), "\n"; | |
$idx++; | |
} | |
print "TIME2=" .(time-$btime). "\n"; | |
} | |
sub _search { | |
my $url = shift; | |
my $idx = shift; | |
$queue->enqueue( $idx.":".length( LWP::Simple::get($url) ) ); | |
} | |
sub commify { | |
local $_ = shift; | |
1 while s/((?:\A|[^.0-9])[-+]?\d+)(\d{3})/$1,$2/s; | |
return $_; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment