#!/usr/bin/perl
use strict;
use warnings;

# Author: Stefan Trcek
# Copyright(c) 2004 ABAS Software AG

use CGI qw(:standard -no_xhtml);
use Time::HiRes;
use WWW::Webrobot::WebrobotLoad;
use WWW::Webrobot::StatisticSummary;
use WWW::Webrobot::CGIHelper;


my $USAGE = <<EOF;
USAGE: webrobot-load [options]
Options are written 'option_name=value_of_option'

option           description
------------------------------------------------------------------------
cfg              test configuration [mandatory], must end in '.prop'
testplan         overwrite 'testplan' in cfg
<property>       set property

BUG: This script will work on unixoid systems only.
EOF

do {print $USAGE; exit} if ($ARGV[0] || "") =~ /^-(-)?h(elp)?$/i;

my $cgi = CGI -> new();
my $cfg_name = $cgi->param('cfg') or die "No configuration file defined!";
my $testplan_name = $cgi->param('testplan');


MAIN: {
    my $cmd_param = WWW::Webrobot::CGIHelper::param2list($cgi, [qw(cfg testplan)]);
    push @$cmd_param, ["output", "WWW::Webrobot::Print::ChildSend"];
    my $cfg = WWW::Webrobot -> read_configuration($cfg_name, $cmd_param);
    my $wrl = WWW::Webrobot::WebrobotLoad->new();

    # start processes
    my ($sec, $usec) = Time::HiRes::gettimeofday();
    my ($statistic, $histogram, $url_statistic, $http_errcode, $assert_ok) = $wrl -> run($cfg, $testplan_name);
    my $elaps = Time::HiRes::tv_interval([$sec, $usec], [ Time::HiRes::gettimeofday() ]);
    $statistic->{total_time} = $elaps;


    my $out_file = $cfg->{load}->{output_file};
    my $scale = $cfg->{load}->{scale} || 40;

    my $old_filehandle = select;
    my @write_to = (*STDOUT);
    my @written_to = ();
    push(@write_to, ">".$out_file) if defined $out_file;
    foreach (@write_to) {
        my $FH;
        if (/^>/) {
            open(FILE, $_) or warn("Can't open $_"), next;
            (my $filename = $_) =~ s/^>//;
            push @written_to, $filename;
            $FH = *FILE;
        }
        select($FH || $_);
        WWW::Webrobot::StatisticSummary::url_statistic($url_statistic, "\n=== URL Statistic ===");
        WWW::Webrobot::StatisticSummary::statistic($statistic, "\n=== Statistic ===\nNumber of clients: $cfg->{load}->{number_of_clients}");
        WWW::Webrobot::StatisticSummary::histogram($histogram, 0, $scale, "\n=== Histogram (x-axis logarithmic) ===");
        WWW::Webrobot::StatisticSummary::histogram($histogram, 1, $scale, "\n=== Histogram (both axis logarithmic) ===");
        WWW::Webrobot::StatisticSummary::http_errcodes($http_errcode, "\n=== HTTP Error Codes ===\ncode  count");
        WWW::Webrobot::StatisticSummary::assert_codes($assert_ok, "\n=== Assert codes ===");
        close $FH if defined $FH;
    }
    select $old_filehandle;
    print "Written to ", join(" ", @written_to), "\n" if @written_to;

    wait;
    #exit $exit;
    exit 0;
}


1;

=head1 NAME

webrobot-load - run a testplan in multiple client mode

=head1 SYNOPSIS

 webrobot-load cfg=example/cfg.prop testplan=example/testplan.xml

=head1 DESCRIPTION

This command runs multiple clients.
Each client runs a testplan.
The results will be printed on STDOUT and the file WEBROBOT_OUT.

This command takes two parameters,
both are mandatory:

=over

=item cfg

This is the configuration file,
see L<WWW::Webrobot::pod::Config>.
Note that there are some special parameter
for C<webrobot-load> only.

=item testplan

This is the testplan to run,
see L<WWW::Webrobot::pod::Testplan>.

=back

B<NOTE:>
Although it may seem that C<webrobot-load> is
a superset of C<webrobot> this is not true.
Use C<webrobot> for single client tests.

B<LIMITATION:>
This script will work on unixoid systems only.
It uses the magic C<open($HANDLE, "-|")> in L<WWW::Webrobot/Forker> which uses fork.


=head1 SEE ALSO

L<webrobot>

=cut
