#!/usr/bin/perl

#Simluate the catastrohpic performance failure of the wordpress
#supercache under high load

for ( my $hitrate = 0 ; $hitrate <= 100 ; $hitrate += 4 ) {

    my $av    = 0;
    my $worst = 0;

    for ( my $i = 0 ; $i < 64 ; $i++ ) {

        my $seconds    = 0;
        my $rendertime = 0.5;
        my $rendered   = 0;
        my $cores      = 4;

        #initially we've one cached page being created
        my $concurrency = 1;

        #emulate at 1/8th of a second
        my $interval = 1 / 512;

        while ( $rendered < $rendertime ) {

            #do we get another hit in this time interval?
            if ( rand() < ( $hitrate * $interval ) ) {
                $concurrency++;
            }
            $rendered += ( $cores * $interval / $concurrency );
            $seconds  += $interval;

            # print "$seconds : $concurrency : $rendered\n";
        }

        #print "$hitrate : $seconds\n";

        $av += $seconds;
        if ( $seconds > $worst ) { $worst = $seconds; }
    }

    $av = $av / 64;
    print "$hitrate $worst $av\n";

}

#plot graph with
#./supercache-emuplator.pl >sc-emulator.csv
# echo "set terminal png size 600,400 enhanced font 'Helvetica,10'; set output 'sc.png'; set xtic auto; set ytic auto; set ylabel 'seconds'; set xlabel 'hits/s'; set xr[0:100]; set yr[0:3600]; plot 'sc-emulator.csv' using 1:2 title 'worst(s)' with lines, 'sc-emulator.csv' using 1:3 title 'average(s)' with lines;" | gnuplot


