###
### Add this chunk to the end of your interchange.cfg file to enable
### benchtimes
###

UserTag benchtimes Order start display
UserTag benchtimes Routine <<EOR
sub {
    my ($start, $display) = @_;
    my @times = times();
    if($start or ! defined $::Instance->{benchtimes_start}) {
        $::Instance->{benchtimes_start} = 0;
        $::Instance->{benchtimes_times} = [ @times ];
        for(@times) {
            $::Instance->{benchtimes_start} += $_;
        }
    }
    my $current_total;
    if($display or ! $start) {
		my $benchtimes_times = $::Instance->{benchtimes_times};
        for(@times) {
            $current_total += $_;
        }
        unless ($start) {
            $current_total = sprintf '%.3f', $current_total - $::Instance->{benchtimes_start};
            for(my $i = 0; $i < 4; $i++) {
                $times[$i] = sprintf '%.3f', $times[$i] - $benchtimes_times->[$i];
            }
        }
        return $current_total;
    }
    return;
}
EOR

## You can change the location
Variable BENCHTIMES_LOG_FILE  logs/bench.log
Variable BENCHTIMES_COUNTER   logs/bench.counter

## Subroutine to start the logging process
GlobalSub benchtimes_start <<EOS
sub {
	my @bench_things = qw( ip path session pid time count bench );
	my %hash = (
		ip		=> $CGI::remote_addr,
		path	=> $CGI::request_uri,
		session	=> $Vend::SessionID,
		pid		=> $$,
		time	=> time(),
	);
	$hash{count}
		= $Vend::Session->{benchtimes_count}
			= Vend::Interpolate::tag_counter($Global::Variable->{BENCHTIMES_COUNTER});

	# Start the benchtimes
	$Vend::Interpolate::Tag->benchtimes(1);
	Vend::Util::logData(
			$Global::Variable->{BENCHTIMES_LOG_FILE},
			@hash{@bench_things}
		);
	return;
}
EOS

GlobalSub benchtimes_end <<EOS
sub {
	my @bench_things = qw( ip path session pid time count bench );
	my %hash = (
		ip		=> $CGI::remote_addr,
		path	=> $CGI::request_uri,
		session	=> $Vend::SessionID,
		pid		=> $$,
		time	=> time(),
		count	=> $Vend::Session->{benchtimes_count},
		bench	=> $Vend::Interpolate::Tag->benchtimes(undef,1),
	);
	Vend::Util::logData(
			$Global::Variable->{BENCHTIMES_LOG_FILE},
			@hash{@bench_things}
		);
	return;
}
EOS
