#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use Cwd qw(abs_path);

our $VERSION = '0.06'; # VERSION

die <<_ unless @ARGV;
Usage:
  $0 <program> [args ...]
  $0 -e <code> ...
_

my $code;
if ($ARGV[0] eq '-e' && @ARGV > 1) {
    shift @ARGV;
    $code++;
} elsif ($ARGV[0] =~ s/^-e//) {
    $code++;
} elsif (!(-f $ARGV[0])) {
    $code++;
}

if ($code) {
    my @subs;
    say "Benchmarking ", join(", ", map { "sub { $_ }" } @ARGV), " ...";
    for (@ARGV) {
        eval "push \@subs, sub { $_ };";
        die $@ if $@;
    }
    require Bench;
    Bench::bench(\@subs, {n=>-1});
} else {
    my $prog  = shift @ARGV;
    my $aprog = abs_path($prog); # or die "can't abs_path($prog): $!\n";
    say "Benchmarking $aprog ...";
    require Bench; Bench->import;
    do $aprog;
}

1;
# ABSTRACT: Benchmark running times of Perl code
# PODNAME: bench


__END__
=pod

=head1 NAME

bench - Benchmark running times of Perl code

=head1 VERSION

version 0.06

=head1 SYNOPSIS

 % bench -e'some_code()'                     ; # -e is optional
 Benchmarking sub { some_code() } ...
 26 calls (28.98/s), 0.897s (34.51ms/call)

 % bench 'some_code()' 'another_code()'      ; # multiple code
 Benchmarking sub { some_code() }, sub { another_code() } ...
 26 calls (28.98/s), 0.897s (34.51ms/call)

 % bench prog.pl;                            ; # file is automatically detected
 Benchmarking /abs/path/to/prog.pl ...
 0.0320s

=head1 DESCRIPTION

This script is a command-line interface for L<Bench>.

 % bench prog.pl

is roughly equivalent to:

 % perl -MBench -e'do "/abs/path/to/prog.pl"'; # time the whole program

while:

 % bench -e 'some_code()'
 % bench 'some_code()'

is roughly equivalent to:

 % perl -MBench -e'bench sub { some_code() }'

=head1 FAQ

=head2 Why use this instead of 'time prog.pl'?

The script is more portable (should run under Windows too).

On the other hand, B<bench> can only time Perl scripts.

=head2 Why use this instead of 'perl -MBench -e'bench sub { ... }'?

C<bench '...'> is shorter to type.

=head1 TODO

=over 4

=item * Accept -I?

=back

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut

