#! /usr/bin/perl

use strict;
use warnings FATAL => 'all';
use diagnostics;


sub analyze($$$) {
	my ($op, $bit, $exe) = @_;
	my @vals;
	my ($avg, $med);
	my ($min, $max);

	open(my $in, "< $op-$bit-$exe.log") or die "open(): $!";
	while (<$in>) {
		chop;
		/\duser / || next;
		my ($time) = /([\d\.]*)user/;
		$min = $time if (! defined($min) || $time < $min);
		$max = $time if (! defined($max) || $time > $max);
		$avg += $time;
		push(@vals, $time);
	}
	my $n = $#vals + 1;
	$avg /= ($#vals + 1);
	$med = $vals[$n / 2];
	close($in);

	my $sdev = 0;
	$sdev += ($_ - $avg) ** 2 foreach (@vals);
	$sdev = sqrt($sdev / $n);

	return ($avg, $med, $sdev, $min, $max);
}


sub write($) {
	my $op = shift;
	my ($med, $avg, $sdev, $min, $max);

	open(my $out, "> data.$op") or die "open(): $!";

	printf($out "# n\tavg\tmed\tsdev\tmin\tmax\n");

	($avg, $med, $sdev, $min, $max) = &analyze($op, "32", "stock");
	printf($out "1\t%f\t%f\t%f\t%f\t%f\n",
			$avg, $med, $sdev, $min, $max);

	($avg, $med, $sdev, $min, $max) = &analyze($op, "32", "opt");
	printf($out "2\t%f\t%f\t%f\t%f\t%f\n",
			$avg, $med, $sdev, $min, $max);

	($avg, $med, $sdev, $min, $max) = &analyze($op, "64", "stock");
	printf($out "3\t%f\t%f\t%f\t%f\t%f\n",
			$avg, $med, $sdev, $min, $max);

	($avg, $med, $sdev, $min, $max) = &analyze($op, "64", "opt");
	printf($out "4\t%f\t%f\t%f\t%f\t%f\n",
			$avg, $med, $sdev, $min, $max);

	close($out);
}


foreach my $op ("enc", "dec", "pipe") {
	&write($op);
}
