#!/usr/bin/perl -w

        my $base_url = "http://si.infonet.ee/xslt-benchmark/";

        my @file_list = ( qw{netinfo1 netinfo_big dj_r1 dj_r2 dj_r3 ix_small ix_big} );

        my $ab_bin = "/usr/local/httpd/bin/ab";
        my $restarter="wget -q \"$base_url/cgi-bin/restart.pl?%s\"";

        my %xslt_test_list = (
        'cgi_perl-sablotron' => 'cgi-bin/sablot.pl',
        'cgi_perl-libxlst'   => 'cgi-bin/xslt.pl',

                'mod_perl-sablotron' => 'mod_perl/sablot.pl',
        'mod_perl-libxlst'   => 'mod_perl/xslt.pl',

                'mod_php-libxlst'    => 'xslt.php',
                'mod_php-sablotron'  => 'sablot.php',
        );

        my %xslt_test_description = (
        'cgi_perl-sablotron' => 'Perl Sabloblotron (cgi)',
        'cgi_perl-libxlst'   => 'Perl LibXSLT (cgi)',

                'mod_perl-sablotron' => 'Perl Sabloblotron (mod_perl)',
        'mod_perl-libxlst'   => 'Perl LibXSLT (mod_perl)',

                'mod_php-sablotron'  => 'PHP Sabloblotron',
                'mod_php-libxlst'    => 'PHP DOMXML/LIBXSLT',
        );

        my %xslt_test_source = (
        'cgi_perl-sablotron' => 'src/sablot.pl.txt',
        'cgi_perl-libxlst'   => 'src/xslt.pl.txt',

                'mod_perl-sablotron' => 'src/sablot.pl.txt',
        'mod_perl-libxlst'   => 'src/xslt.pl.txt',

                'mod_php-sablotron'  => 'src/sablot.phps',
                'mod_php-libxlst'    => 'src/xslt.phps',
        );

        my %ab2xml = (
                'Document Path'        => 'document-path',
                'Document Length'      => 'document-length',
                'Concurrency Level'    => 'concurrency-level',
                'Time taken for tests' => 'time-for-test',
                'Complete requests'    => 'complete-requests',
                'Failed requests'      => 'failed-requests',
                'Total transferred'    => 'total-transferred',
                'HTML transferred'     => 'html-transferred',
                'Requests per second'  => 'requests-per-second',
        );

        my %ab2regexp = (
                'Document Path'        => '^Document Path:\s+(,+)$',
                'Document Length'      => '^Document Length:\s+(\d+)',
                'Concurrency Level'    => '^Concurrency Level:\s+(\d+)',
                'Time taken for tests' => '^Time taken for tests:\s+(\d+(\.\d+)?)',
                'Complete requests'    => '^Complete requests:\s+(\d+)',
                'Failed requests'      => '^Failed requests:\s+(\d+)',
                'Total transferred'    => '^Total transferred:\s+(\d+)',
                'HTML transferred'     => '^HTML transferred:\s+(\d+)',
                'Requests per second'  => '^Requests per second:\s+(\d+(\.\d+)?)',
        );

        print "<?xml version=\"1.0\" encoding=\"Windows-1251\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"benchmark.xsl\"?>\n";

        print "<benchmark>\n";
        print "<tests>\n";
        foreach $name (sort keys %xslt_test_list){
                print "\t<test id=\"$name\">\n";
          print "\t\t<source>".$xslt_test_source{$name}."</source>\n";
          print "\t\t<description>".$xslt_test_description{$name}."</description>\n";
                print "\t</test>\n";
        }
        print "</tests>\n";

        print "<results>\n";

        my @ab_conf = ( qw{ 1000:10 2000:25 3000:50 5000:100} );

        foreach my $ab (@ab_conf)
        {
                ($ab_req,$ab_concurr)  = split(/:/,$ab);
                print STDERR "ab: $ab_req,$ab_concurr\n";

          print "<ab>\n";
        print "\t<cmd>".sprintf("%s -n %d -c",$ab_bin,$ab_req,$ab_concurr)."</cmd>\n";
          print "\t<request>$ab_req</request>\n";
        print "\t<concurrency-level>$ab_concurr</concurrency-level>\n";

                my ($file,$xslt_processor);

                foreach $file (@file_list)
          {
                print STDERR "file: $file\n";

                print "\t<data>\n";
                print "\t\t<name>$file</name>\n";

                foreach $name (sort keys %xslt_test_list)
                {
                                 print STDERR "Restarting apache ...\n";
                                 system(sprintf($restarter.$name));
                                 sleep 10;

                                 print STDERR "Run test: $name\n";
                         print "\t\t<test id=\"$name\">\n";

                     my $cmd = sprintf("%s -n %d -c %d \"%s%s?%s\"",$ab_bin,$ab_req,$ab_concurr,$base_url,$xslt_test_list{$name},$file);

                         foreach my $s (`$cmd`)
                         {
                                chop($s);
                          foreach(keys %ab2regexp)
                          {
                                my $reg_exp = $ab2regexp{$_};
                            my $xml_name = $ab2xml{$_};

                            if ($s =~ /$reg_exp/){
                                                print "\t\t\t\t<$xml_name>".$1."</$xml_name>\n";
                                                last;
                            }
                          }
                   }

                                 print "\t\t</test>\n";
                }

                print "\t</data>\n";
    }
          print "</ab>\n";
        }
        print "</results>\n";
        print "</benchmark>\n";