use strict;
use warnings;

open my $f, '../../../binaries/system/profile.txt' or die $!;
my $turn = 0;
my %s;
my %f;
while (<$f>) {
    if (/^PS profiler snapshot/) {
        if ($turn) {
            for (keys %f) {
                $s{$_}[$turn-1] = $f{$_};
            }
        }
        ++$turn;
        %f = ();
    } elsif (/Time in node: ([\d.]+)/) {
        $f{"total"} += $1;
    } elsif (/^[|'][- |']*([^|]+?)\s+\|[^|]+\| (\S+)/) {
        $f{$1} += $2;
    } elsif (/^[|']-([^|]+?)\s+\| (\d+)/) {
        $f{$1} += $2;
    }
}

print "var graphData = [\n";
my $n = 0;
for my $k (sort keys %s) {
    print ",\n" if $n++;
    print "{label: '$k', data:[";
    for my $t (0..$#{$s{$k}}) {
        print "," if $t;
        print "[$t,".($s{$k}[$t] || 0)."]";
    }
    print "]}";
}
print "\n];\n";