#!/usr/bin/env perl

#
# Copyright (c) 2015, Jyri J. Virkki
# All rights reserved.
#
# This file is under BSD license. See LICENSE file.
#

#
# ./colgraph SIZE ERROR
#
# SIZE = size of bloom library to initialize
# ERROR = expected error
#
# This script runs a random collision test (test-libbloom -c) $rounds
# number of times for 10 element counts from SIZE/10 to SIZE. The
# average of each run is saved in the 'data' file in the current
# directory.
#
# If ploticus is available it'll also display a graph. Or you can use
# any other graphing app or tool to process the 'data' file.
#

$rounds = 25;

$size = shift(@ARGV);
if (!$size) {
    die "provide a size\n";
}

$error = shift(@ARGV);
if (!$error) {
    die "provide expected error\n";
}

open(OUT, ">data");
for ($tenth = 1; $tenth < 11; $tenth++) {
    $count = ($size / 10) * $tenth;

    $avg = 0;
    for ($n = 0; $n < $rounds; $n++) {
        open(RES, "../build/test-libbloom -c $size $error $count |");
        while(<RES>) {$got = $_;}
        close(RES);
        ($added, $coll) = $got =~ /count: (\d+), coll: (\d+)/;
        $avg += $coll;
    }
    $avg /= $rounds;
    print "ADDED $added, AVG.COLL $avg\n";
    print OUT "$added, $avg\n";
}
close(OUT);

$cmd = "ploticus -prefab lines data=data x=1 y=2 \"xrange=0 $size\" " .
    "\"title=size = $size\" \"ylbl=collisions\" \"xlbl=count\" ";
print "$cmd\n";
system($cmd);
