The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/env perl

use strict;
use warnings;

use Pinto;
use Pinto::Initializer;
use Pinto::DistributionSpec;

use File::Temp;
use List::Util qw(sum);
use Getopt::Long::Descriptive;

#-----------------------------------------------------------------------------

# Copyright 2013 Jeffrey Ryan Thalhammer <jeff@stratopan.com>

#-----------------------------------------------------------------------------

my @ops = qw(pin unpin unregister register);

my ($opt, $usage) = describe_options(
	"$0 %o TARGETS",
    [ 'root|r=s',         "Root of repository",                        ],
    [ 'ops|o=s@',         "Operations to perform",                     ],
    [ 'iterations|i=i',   "Number of iterations",  { default => 100  } ],
);

my @targets = @ARGV ? @ARGV : qw(THALJEF/Pinto-0.065.tar.gz);
my %ops     = map { lc $_ => 1 } $opt->ops ? @{$opt->ops} : @ops; 
my $root    = $opt->root || File::Temp->newdir;
my $iters   = $opt->iterations;

#-----------------------------------------------------------------------------

{
	Pinto::Initializer->new->init(root => $root) unless -e $root;
	my $pinto = Pinto->new(root => "$root");

	for my $target (@targets) {
		my $spec = Pinto::DistributionSpec->new($target);
		next if $pinto->repo->get_distribution(spec => $spec);
		$pinto->run(Pull => (targets => $target, message => "pulled $target"));
	}
}

#-----------------------------------------------------------------------------

my @runs;
for my $i (1..$iters) {
	print "Iteration $i: ";
	DB::enable_profile() if $i == $iters && defined $Devel::NYTProf::VERSION;

	my $start = time;
	my $pinto = Pinto->new(root => "$root");
	$pinto->run(Pin         => (targets => \@targets, message => 'pin'))   if $ops{pin};
	$pinto->run(Unpin       => (targets => \@targets, message => 'unpin')) if $ops{unpin};
	$pinto->run(Unregister  => (targets => \@targets, message => 'unreg')) if $ops{unregister};
	$pinto->run(Register    => (targets => \@targets, message => 'reg'))   if $ops{register};
	my $elapsed = time - $start;

	print "$elapsed seconds\n";
	push @runs, $elapsed;
}

my $average = sum( @runs ) / scalar @runs;
print "Average: $average seconds\n";