@define RANDCNT %( 4000 %)
@define RANDMAX %( 1000 %)
@define ITERS %( 1500 %)

@define time
%(
    %( runtime == "dyninst" %?
	gettimeofday_us()
    %:
	cpu_clock_us(cpu())
    %)
%)

@define feed(agg, tagg)
%(
    t = @time
    foreach(k in randvals)
	@agg <<< k
    @tagg += @time - t
%)

global x, tx = 0, y, ty = 0
global a, ta = 0, b, tb = 0
global randvals[@RANDCNT]

probe begin
{
    /* TEST 1: test optimizations for @count, @sum, @min, and @max. */

    for (i=0; i<@ITERS; i++)
    {

	for (j=0; j<@RANDCNT; j++)
	    randvals[j] = randint(@RANDMAX)

	/* The "ordering dance" described above happens here */
	if(i%2)
	{
	    @feed(x, tx)
	    @feed(y, ty)
	}
	else
	{
	    @feed(y, ty)
	    @feed(x, tx)
	}
    }

    /*
     * We need to print the stats out to avoid compiler elision.
     * The list of stats mentioned below makes the actual difference
     * between stats under test and is the gist of this test.  The test
     * should show no measurable shrinkage, if the below list doesn't
     * differ for measured stats.
     */
    printdln(" ", "IGNORE", @count(x))
    printdln(" ", "IGNORE", @count(y), @sum(y), @min(y), @max(y))

    /*
     * Measured shrinkage [%], prevent division by zero caused by
     * rounding errors
     */
    printf("TEST1 %d\n", ((ty-tx)*10000)/(ty*100))


    /* TEST 2: test optimizations for @variance. */

    for (i=0; i<(@ITERS / 4); i++)
    {

	for (j=0; j<@RANDCNT; j++)
	    randvals[j] = randint(@RANDMAX)

	if(i%2)
	{
	    @feed(a, ta)
	    @feed(b, tb)
	}
	else
	{
	    @feed(b, tb)
	    @feed(a, ta)
	}
    }

    printdln(" ", "IGNORE", @count(a))
    printdln(" ", "IGNORE", @variance(b))

    /*
     * Measured shrinkage [%], prevent division by zero caused by
     * rounding errors
     */
    printf("TEST2 %d\n", ((tb-ta)*10000)/(tb*100))

    exit()
}
