Permalink
Browse files

some stats

  • Loading branch information...
1 parent 3a6c8e8 commit 19a4ca8ec32c549a73fa1b2f8f0b74dc2b41ab33 @shafiev committed Dec 7, 2009
Showing with 101 additions and 64 deletions.
  1. +1 −0 README
  2. +100 −64 benchmark.pl
View
1 README
@@ -0,0 +1 @@
+AnyEvent-HTTPBenchmark is tool to make benchmark. It's similar as ab but more improvement.
View
164 benchmark.pl 100644 → 100755
@@ -1,101 +1,137 @@
-#!/usr/bin/perl
-use common::sense;
+#!/usr/bin/env perl
+#Yet another http benchmark ;)
+use common::sense;
use AnyEvent::HTTP;
use Time::HiRes;
-use Getopt::Long;
+use Getopt::Long;
use DateTime;
use Data::Dumper;
-#$AnyEvent::VERBOSE = 10;
-
+my $DEBUG = 0; #Debug mode. Default is false (0)
my $timeout = 60;
-my %timeofreq;
-
-my $count = 100;
-my $concurency = 10;
-
-my $method = 'GET';
-
-$AnyEvent::HTTP::MAX_PER_HOST = $concurency*$concurency;
-
-my $done = 0;
-my $url = 'http://ya.ru';
-
-
+my $count = 30000; #number of requests
+my $concurency = 20; # number of parralle requests
+my $done = 0;
+my $url = 'http://elementa.su/';# url to test
+my $method = 'GET'; #http method
-sub file_open
-{
-my $file = $ARGV[0];
-
-die "invalid file "
- unless -e $file
- and -s _ and -f _;
-
-my @scenario;
-
-open FH, '<', $file;
-while ( <FH> ) {
- my ( $method, $url ) = split( / /, $_, 2 );
-
- if ( $method !~ /get|post/io ) {
- warn "bad method [$method]\n";
- next;
- }
-
- push( @scenario, [ $method, $url ] );
-}
-close FH;
-
-}
-
-#file_open();
+my $cv = AnyEvent->condvar;
+#arrays
+my @reqs_time; # the times of requests
+my @scenario; #used for script scenario file . Not yet implemented (:
+$AnyEvent::VERBOSE = 10 if $DEBUG;
+$AnyEvent::HTTP::MAX_PER_HOST = $concurency;
-my $cv = AnyEvent->condvar;
+#on ctrl-c break run the end_bench sub.
+$SIG{'INT'} = 'end_bench';
my $start_time = Time::HiRes::time;
my $dt = DateTime->from_epoch( epoch => $start_time );
-say 'Started at ' .
-($dt->hms). '.' .($dt->millisecond);
-
+say 'Started at ' .($dt->hms). '.' .($dt->millisecond);
-$SIG{'INT'} = 'end_bench';
+#read_script_file(); #read the url script file
+#starting requests
for ( 1 .. $concurency )
{
add_request($_, $url);
}
-$cv->recv;
+$cv->recv; # begin receiving message and make callbacks magic ;)
+end_bench(); # call the end
+#subs
sub add_request
{
my ($id, $url) = @_;
+
my $req_time = Time::HiRes::time;
http_request $method => $url, timeout => $timeout, sub
{
my $completed = Time::HiRes::time;
my $dtin = DateTime->from_epoch( epoch => ($completed-$req_time) );
- say 'Got answer in ' . ($dtin->second) . '.' . ($dtin->millisecond) . ' seconds' ;
+ say 'Got answer in '. $dtin->second . '.' . $dtin->millisecond .' seconds';
+ push( @reqs_time , ( ($dtin->second) .'.'. ($dtin->millisecond) ) );
$done++;
-
- my ($body, $hdr) = @_;
- print "finished request $id, done $done\n";
+ my $hdr = @_[1];
+
+ if ( $hdr->{Status} =~ /^2/ )
+ {
+ say "done $done\n";
+ }
+ else
+ {
+ say "Oops we get problem in request . $done . ($hdr->{Status}) . ($hdr->{Reason}) ";
+ }
+
return add_request($done, $url) if $done < $count;
-
+
$cv->send;
- };
-
+ }
+}
+
+
+#sub read_script_file under test
+sub read_script_file
+{
+ my $file = $ARGV[0];
+
+ warn "invalid file "
+ unless -e $file and -s _ and -f _;
+
+
+ open FH, '<', $file;
+ while ( <FH> )
+ {
+ chomp;
+ my ($method, $url) = split( / /, $_, 2 );
+
+ if ( $method !~ /get|post/io )
+ {
+ warn "bad method [$method]\n";
+ next;
+ }
+
+
+ push( @scenario, [ $method, $url ] );
+ }
+
+ close FH;
+
}
+
sub end_bench
{
-my $end_time = Time::HiRes::time;
-my $end_dt = DateTime->from_epoch( epoch => ($end_time - $start_time) );
-say 'It\'s takes the ' . ($end_dt->second) .':' .($end_dt->millisecond);
-say 'Requests per second is ' . ( $count /($end_dt->second) );
-exit;
+ my $end_time = Time::HiRes::time;
+ my $end_dt = DateTime->from_epoch( epoch => ($end_time - $start_time) );
+ say;
+ say 'It\'s takes the ' . ($end_dt->second) .'.' .($end_dt->millisecond) .' seconds';
+ my $sum;
+
+ #dirty hack to avoid division by zero ;)
+ if ( ($end_dt->second) ==0 )
+ {
+ say 'Requests per second is ' . ( $count /($end_dt->millisecond) );
+ }
+ else
+ {
+ say 'Requests per second is ' . ( $count /( ($end_dt->minute)*60 + ($end_dt->second) ));
+ }
+ #sort by time
+ @reqs_time = sort (@reqs_time);
+
+ for my $i(0..scalar(@reqs_time) )
+ {
+ #calculate average time
+ $sum +=$reqs_time[$i];
+ }
+ say ;#new line
+ say 'Shortest is : '. $reqs_time[0] . ' sec.';
+ say 'Average time is : '. ($sum/$count) . ' sec.';
+ say 'Longest is : '. $reqs_time[scalar(@reqs_time)-1] . ' sec.';
+ exit;
}
-end_bench();
-1;
+1;

0 comments on commit 19a4ca8

Please sign in to comment.