#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my $results;
my $counts;

my $fs;
my $size;
my $test;
my %sizes;
my %tests;
my %filesystems;

while (my $line = <>) {
	
	if ($line =~ /^RUN/) {
		my $r;
		($r, $fs, $size, $test) = split(' ', $line);
		$tests{$test} = 1;
		$sizes{$size} = 1;
		$filesystems{$fs} = 1;
	}
	if ($line =~ /^Throughput/) {
		my ($r, $res) = split(' ', $line);
		push(@{$results->{$test}->{$fs}->{$size}->{results}}, $res);
	}
}

# create the data files
for $test (keys %tests) {
	for $fs (keys %filesystems) {
		open FILE, ">$test-$fs.dat" || die;
		for $size (sort {$a <=> $b} keys %sizes) {
			if (defined $results->{$test}->{$fs}->{$size}) {
				my @res = @{$results->{$test}->{$fs}->{$size}->{results}};
				print FILE "$size $res[0]\n";
			}
		}
		close FILE;
	}
}

sub cmpfs($$$)
{
	my $test = shift;
	my $fs1 = shift;
	my $fs2 = shift;
	my @res;
	@res = @{$results->{$test}->{$fs1}->{10}->{results}};
	my $r1 = $res[0];
	@res = @{$results->{$test}->{$fs2}->{10}->{results}};
	my $r2 = $res[0];
	return $r2 <=> $r1;
}

# create the gnuplot output

for $test (keys %tests) {
	open FILE, "|gnuplot" || die;
	if ($test =~ /noxattr/) {
		print FILE "set title \"dbench3 comparison without xattr\"\n";
	} else {
		print FILE "set title \"dbench3 comparison using xattr\"\n";
	}
	print FILE "set xlabel \"number of clients\"\n";
	print FILE "set ylabel \"MBytes/sec\"\n";
	print FILE "set yrange [0:600]\n";
	print FILE "set terminal png\n";
	print FILE "set size 1.5\n";
	print FILE "set output \"$test.png\"\n";
	print FILE "plot ";
	my @keys = sort {cmpfs("xattr", $a,$b)} keys %filesystems;
	for (my $i=0;$i<=$#keys;$i++) {
		my $fs = $keys[$i];
		print FILE "\"$test-$fs.dat\" title \"$fs\" with linesp";
		if ($i == $#keys) {
			print FILE "\n";
		} else {
			print FILE ", ";
		}
	}
	close FILE;
	print "Created $test.png\n";
}


# delete the data file
for $test (keys %tests) {
	for $fs (keys %filesystems) {
		unlink "$test-$fs.dat";
	}
}
