AnyEvent-HTTPBenchmark

 view release on metacpan or  search on metacpan

benchmark.pl  view on Meta::CPAN

#!/usr/bin/env perl
use common::sense;     #new features in perl
use Getopt::Long;      # to command line parsing
use AnyEvent::HTTP; # use for http requests and etc. methods
use POSIX; # POSIX related stuff
use Data::Dumper;      # to debug data
use AnyEvent::CacheDNS ':register'; # dns requests caching
my $DEBUG      = 0;        #Debug mode. Default is false (0)
my $verbose    = 0;        #to view the each connection result
my $timeout    = 60;
my $count      = 30000;    #default number of requests
my $concurency = 20;       # default number of parralle requests
my $done       = 0;             #number of done requests
my $url; # the url to test
my $method = 'GET';        #http method
my $proxy;                 # proxy server
my $max_recurse = 10;      # the default recurse number;
my $useragent = 'Mozilla/5.0 (compatible; U; AnyEvent::HTTPBenchmark/0.09; +http://github.com/shafiev/AnyEvent-HTTPBenchmark)';

#arrays
my @reqs_time;             # the time of requests

parse_command_line();      #parsing the command line arguments

$AnyEvent::VERBOSE            = 10 if $DEBUG;
$AnyEvent::HTTP::MAX_PER_HOST = $concurency;
$AnyEvent::HTTP::set_proxy    = $proxy;
$AnyEvent::HTTP::USERAGENT    = $useragent;

#on ctrl-c break run the end_bench sub.
$SIG{'INT'} = 'end_bench';

my $cv = AnyEvent->condvar;

#start measuring time
my $start_time = AnyEvent->time;

print 'Started at ' . format_time($start_time) . "\n";

#starting requests
for ( 1 .. $concurency ) {
    add_request( $_, $url );
}

$cv->recv;      # begin receiving message and make callbacks magic ;)
end_bench();    # call the end

#subs
sub parse_command_line {
    if (not defined @ARGV)
    {
        print <<HEREDOC;
    AnyEvent::HTTPBenchmark     http://github.com/shafiev/AnyEvent-HTTPBenchmark


        -url                 url to test,
        -number        number of requests,
        -c                    number of parrallel clients
        -verbose        verbose mode
        -debug           debug mode
        -proxy            proxy
        -useragent     useragent string

Example :
    ./benchmark.pl -url http://myfavouritesite.com  -n number_of_requests -c number_of_parrallel clients 
    



( run in 0.902 second using v1.01-cache-2.11-cpan-df04353d9ac )