Added patch of gbjk, released 1.16
[catagits/HTTP-Body.git] / scripts / benchmark.pl
CommitLineData
d10b9732 1#!/usr/bin/perl
2
3BEGIN {
4 require FindBin;
5}
6
7use strict;
8use warnings;
9use lib "$FindBin::Bin/../lib";
712c90b5 10use lib "$FindBin::Bin/../t/lib";
d10b9732 11
12use Benchmark qw[cmpthese timethese];
13use CGI qw[];
14use CGI::Simple qw[];
15use HTTP::Body qw[];
16use IO::Handle qw[];
17use IO::File qw[O_RDONLY SEEK_SET];
712c90b5 18use PAML qw[LoadFile];
d10b9732 19
20my ( $headers, $content, $message );
21
22my $benchmarks = {
23 'CGI' => sub {
24
25 $content->seek( 0, SEEK_SET )
26 or die $!;
27
28 STDIN->fdopen( $content->fileno, 'r' );
29
30 CGI::_reset_globals();
31
32 my $cgi = CGI->new;
33 },
34 'HTTP::Body' => sub {
35
36 $content->seek( 0, SEEK_SET )
37 or die $!;
38
39 my $body = HTTP::Body->new( $headers->{'Content-Type'},
40 $headers->{'Content-Length'} );
41
42 while ( $content->read( my $buffer, 4096 ) ) {
43 $body->add($buffer);
44 }
45
46 unless ( $body->state eq 'done' ) {
47 die 'baaaaaaaaad';
48 }
7428d118 49 }
d10b9732 50};
51
52if ( eval 'require CGI::Simple' ) {
53 $benchmarks->{'CGI::Simple'} = sub {
54
55 $content->seek( 0, SEEK_SET )
56 or die $!;
57
58 STDIN->fdopen( $content->fileno, 'r' );
59
60 CGI::Simple::_reset_globals();
61
62 my $cgi = CGI::Simple->new;
63 };
64}
65
7428d118 66if ( eval 'require APR::Request' ) {
67
68 require APR;
69 require APR::Pool;
70 require APR::Request;
71 require APR::Request::CGI;
72 require APR::Request::Param;
73
74 $benchmarks->{'APR::Request'} = sub {
75
76 $content->seek( 0, SEEK_SET )
77 or die $!;
78
79 STDIN->fdopen( $content->fileno, 'r' );
80
81 my $pool = APR::Pool->new;
82 my $apr = APR::Request::CGI->handle($pool);
83
84 if ( my $table = $apr->param ) {
85 $table->do( sub { 1 } );
86 }
87
88 if ( my $body = $apr->body ) {
89 $body->param_class('APR::Request::Param');
90 $body->uploads($pool)->do( sub { 1 } );
91 }
92 };
93}
94
d10b9732 95my @benchmarks = @ARGV ? @ARGV : qw[ t/data/benchmark/001
96 t/data/benchmark/002
97 t/data/benchmark/003 ];
98
99foreach my $benchmark ( @benchmarks ) {
100
712c90b5 101 $headers = LoadFile("$FindBin::Bin/../$benchmark-headers.pml");
d10b9732 102 $content = IO::File->new( "$FindBin::Bin/../$benchmark-content.dat", O_RDONLY )
103 or die $!;
104
105 binmode($content);
106
d10b9732 107 local %ENV = (
108 CONTENT_LENGTH => $headers->{'Content-Length'},
109 CONTENT_TYPE => $headers->{'Content-Type'},
110 QUERY_STRING => '',
111 REQUEST_METHOD => 'POST'
112 );
113
114 printf( "Content-Type : %s\n", $headers->{'Content-Type'} =~ m/^([^;]+)/ );
115 printf( "Content-Length : %s\n", $headers->{'Content-Length'} );
116 printf( "Benchmark : %s\n", $headers->{'Benchmark'} ) if $headers->{'Benchmark'};
117 print "\n";
118
71806b5a 119 timethese( -1, $benchmarks );
d10b9732 120
121 printf( "%s\n", "-" x 80 ) if @benchmarks > 1;
122}