test-perf.pl 3.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#!/usr/bin/perl

use strict;
use LWP::UserAgent;
use Time::HiRes qw(time);

use constant NB => 5;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->requests_redirectable( [] );

my ( $request, @get, @post, @menu, @cookies, @handler );

# Fake request to be sure that all is compiled
$request = new HTTP::Request( 'GET', 'http://lemonldap/objectweb.org/' );
$ua->request( $request, \&cb_content );

$request = new HTTP::Request( 'GET', 'http://127.0.0.1/index_simple.pl' );
$request->header( Host => 'auth.example.com' );
for ( my $i = 0 ; $i < NB ; $i++ ) {
    my $time = time();
    $ua->request( $request, \&cb_content );
    my $time2 = time() - $time;
    push @get, $time2;
}

$request = new HTTP::Request( 'POST', 'http://127.0.0.1/index_simple.pl' );
$request->header( Host             => 'auth.example.com' );
$request->header( 'Content-Lenght' => '42' );
$request->header( 'Content-Type'   => 'application/x-www-form-urlencoded' );

$request->content('url=&user=user&password=password');
for ( my $i = 0 ; $i < NB ; $i++ ) {
    my $time     = time();
    my $response = $ua->request( $request, \&cb_content );
    my $time2    = time() - $time;
    if ( my $r = $response->code != 302 ) {
        print STDERR "$r\n" . $response->content . "\n";
        print STDERR "Headers :\n";
        $response->scan( sub { print $_[0] . ": " . $_[1] . "\n"; } );
        next;
    }
    $response->scan(
        sub {
            if ( $_[0] eq 'Set-Cookie' ) {
                my $c = $_[1];
                $c =~ s/;.*$//;
                push @cookies, $c;
            }
        }
    );
    push @post, $time2;
}

for ( my $i = 0 ; $i < NB ; $i++ ) {
    $request = new HTTP::Request( 'GET', 'http://127.0.0.1/index_simple.pl' );
    $request->header( Host => 'auth.example.com' );
    my $cookie = shift @cookies;
    $request->header( "Cookie", $cookie );
    my $time     = time();
    my $response = $ua->request( $request, \&cb_content );
    my $time2    = time() - $time;
    push @menu, $time2;
    $request = new HTTP::Request( 'GET', 'http://127.0.0.1/index.pl' );
    $request->header( Host => 'test.example.com' );
    $request->header( "Cookie", $cookie );

    for ( my $j = 0 ; $j < 5 ; $j++ ) {
        $time     = time();
        $response = $ua->request( $request, \&cb_content );
        $time2    = time() - $time;
        push @{ $handler[$i] }, $time2;
    }
}

#close LOG;

print "Result
+-----+-----------+----------+---------+-----------------------------------------+
| Req | Auth form | Post req |  Menu   |    5 access to test.example.com      
   |
+-----+-----------+----------+---------+-----------------------------------------+
";

#  1  |  0.17408  |  0.03393 | 0.04451 | 0.02144 0.00719 0.00717 0.00717
0.00709 for ( my $i = 0 ; $i < NB ; $i++ ) {
    printf
      "| %-3d |  %3.5f  |  %3.5f | %3.5f | %3.5f %3.5f %3.5f %3.5f %3.5f |\n",
      $i + 1, $get[$i], $post[$i], $menu[$i], @{ $handler[$i] };
}

print
"+-----+-----------+----------+---------+-----------------------------------------+\n";

sub cb_content {

    #print LOG shift;
}