#!/usr/bin/perl

my $nblocks = 2**23;
my @dir = qw(
	/mnt/data-mag/robert.haas
	/mnt/data-mag2/robert.haas
	/mnt/data-ssd/robert.haas
	/mnt/data-ssd2/robert.haas
);
my @parallel_degree = qw(1 2 4 8 16);
my @result;

for my $dir (@dir)
{
	for my $degree (@parallel_degree)
	{
		my @cmd = ('./write_and_fsync', $nblocks / $degree);

		clean_dir($dir);
		for (my $i = 1; $i <= $degree; ++$i)
		{
			push @cmd, $dir . '/test' . $i;
		}

		push @result, sprintf("%s %s %s\n", $dir, $degree, try(@cmd));
	}
}

for my $degree (@parallel_degree)
{
	next if $degree % @dir != 0;
	print $degree, "\n";

	my @cmd = ('./write_and_fsync', $nblocks / $degree);

	clean_dir($_) for @dir;
	for (my $i = 1; $i <= $degree; ++$i)
	{
		push @cmd, $dir[$i % @dir] . '/test' . $i;
	}

	push @result, sprintf("ALLDIRS %s %s\n", $degree, try(@cmd));
}

print @result;

sub clean_dir
{
	my ($dir) = @_;

	opendir(DIR, $dir) || die "opendir: $!";
	my @f = grep { /^test\d+$/ } readdir(DIR);
	closedir(DIR);

	for my $f (@f)
	{
		unlink("$dir/$f") || die "unlink: $!";
	}
}

sub try
{
	my (@cmd) = @_;

	print "executing: @cmd\n";
	my $t0 = time();
	system @cmd;
	my $t1 = time();
	
	return $t1 - $t0;
}
