Created
March 5, 2010 00:15
-
-
Save konobi/322303 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
use strict; | |
use warnings; | |
use Encode; | |
use HTTP::Request; | |
use LWPx::ParanoidAgent; | |
use JavaScript; | |
use Try::Tiny; | |
our $VERSION = '1.00'; | |
## why does LWPx::ParanoidAgent need this? | |
{ | |
no warnings 'redefine'; | |
sub LWP::Debug::debug { } | |
sub LWP::Debug::trace { } | |
} | |
sub http_request { | |
my (@js_args) = @_; | |
my $ua = LWPx::ParanoidAgent->new; | |
$ua->agent("Joyent Smart Platform / HTTP / $VERSION"); | |
$ua->timeout( 10 ); | |
my $response = try { | |
my @args; | |
for my $part (@js_args){ | |
push(@args, ( | |
ref($part) ? $part : Encode::encode("utf8", $part) | |
)); | |
} | |
my $req = shift @args; | |
my $r = ref($req) ? HTTP::Request->new(@$req) : HTTP::Request->new($req, @args); | |
$ua->request( $r ); | |
} catch { | |
die "Could not complete HTTP Request: $_"; | |
}; | |
my $ro = response_to_object($response); | |
return $ro; | |
} | |
sub response_to_object { | |
my $response = shift; | |
my %headers = %{ $response->{_headers} }; | |
my $ro = { | |
'headers' => \%headers, | |
'content' => $response->decoded_content, | |
'code' => $response->code, | |
}; | |
return $ro; | |
} | |
my $rt = JavaScript::Runtime->new(); | |
my $cx = $rt->create_context(); | |
$cx->bind_function(http_request => \&http_request); | |
$cx->bind_function(println => sub { print @_, "\n" }); | |
print $@ if $@; | |
my $last_size = 0; | |
for my $i (0..500_000){ | |
$cx->eval(q{http_request("GET", "http://127.0.0.1/");}); | |
if(!($i % 100)){ | |
my $foo = `ps -o pid,rss | grep $$`; | |
chomp($foo); | |
($foo) = $foo =~ /(\d+)$/; | |
my $diff = $foo - $last_size; | |
print "Completed: $i requests (res mem: $foo ---> [diff: $diff])\n"; | |
$last_size = $foo; | |
} | |
} | |
print $@ if $@; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment