File: benchmark.pl

package info (click to toggle)
libhttp-body-perl 1.11-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,468 kB
  • sloc: perl: 840; makefile: 4
file content (122 lines) | stat: -rw-r--r-- 2,910 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/perl

BEGIN {
    require FindBin;
}

use strict;
use warnings;
use lib "$FindBin::Bin/../lib";
use lib "$FindBin::Bin/../t/lib";

use Benchmark   qw[cmpthese timethese];
use CGI         qw[];
use CGI::Simple qw[];
use HTTP::Body  qw[];
use IO::Handle  qw[];
use IO::File    qw[O_RDONLY SEEK_SET];
use PAML        qw[LoadFile];

my ( $headers, $content, $message );

my $benchmarks = {
    'CGI' => sub {

        $content->seek( 0, SEEK_SET )
          or die $!;

        STDIN->fdopen( $content->fileno, 'r' );

        CGI::_reset_globals();

        my $cgi = CGI->new;
    },
    'HTTP::Body' => sub {

        $content->seek( 0, SEEK_SET )
          or die $!;

        my $body = HTTP::Body->new( $headers->{'Content-Type'},
                                    $headers->{'Content-Length'} );

        while ( $content->read( my $buffer, 4096 ) ) {
            $body->add($buffer);
        }

        unless ( $body->state eq 'done' ) {
            die 'baaaaaaaaad';
        }
    }
};

if ( eval 'require CGI::Simple' ) {
    $benchmarks->{'CGI::Simple'} = sub {

        $content->seek( 0, SEEK_SET )
          or die $!;

        STDIN->fdopen( $content->fileno, 'r' );

        CGI::Simple::_reset_globals();

        my $cgi = CGI::Simple->new;
    };
}

if ( eval 'require APR::Request' ) {

    require APR;
    require APR::Pool;
    require APR::Request;
    require APR::Request::CGI;
    require APR::Request::Param;

    $benchmarks->{'APR::Request'} = sub {

        $content->seek( 0, SEEK_SET )
          or die $!;

        STDIN->fdopen( $content->fileno, 'r' );

        my $pool = APR::Pool->new;
        my $apr  = APR::Request::CGI->handle($pool);

        if ( my $table = $apr->param ) {
            $table->do( sub { 1 } );
        }

        if ( my $body = $apr->body ) {
            $body->param_class('APR::Request::Param');
            $body->uploads($pool)->do( sub { 1 } );
        }
    };
}

my @benchmarks =  @ARGV ? @ARGV : qw[ t/data/benchmark/001
                                      t/data/benchmark/002
                                      t/data/benchmark/003 ];

foreach my $benchmark ( @benchmarks ) {

    $headers  = LoadFile("$FindBin::Bin/../$benchmark-headers.pml");
    $content  = IO::File->new( "$FindBin::Bin/../$benchmark-content.dat", O_RDONLY )
      or die $!;

    binmode($content);

    local %ENV = (
        CONTENT_LENGTH => $headers->{'Content-Length'},
        CONTENT_TYPE   => $headers->{'Content-Type'},
        QUERY_STRING   => '',
        REQUEST_METHOD => 'POST'
    );

    printf( "Content-Type   : %s\n", $headers->{'Content-Type'} =~ m/^([^;]+)/ );
    printf( "Content-Length : %s\n", $headers->{'Content-Length'} );
    printf( "Benchmark      : %s\n", $headers->{'Benchmark'} ) if $headers->{'Benchmark'};
    print "\n";

    timethese( -1, $benchmarks );

    printf( "%s\n", "-" x 80 ) if @benchmarks > 1;
}