#include "postgres.h"
#include "storage/checksum_impl.h"
#include <time.h>

#undef printf

int __attribute__ ((noinline)) checksum_block(char *page, uint32 blockno)
{
	return pg_checksum_page(page, blockno);
}

int main(int argc, char *argv[]) {
	char *page;
	uint64 i;
	uint64 sum = 0;

	struct timespec start;
	struct timespec end;
	double delta;

	if (argc<3) {
		printf("Usage: %s niterations nblocks\n", argv[0]);
		return 1;
	}

	uint64 n = strtoull(argv[1], 0, 10);
	uint64 b = strtoull(argv[2], 0, 10);

	page = malloc(BLCKSZ*b);

	for (i = 0; i < BLCKSZ*b; i++)
		page[i] = (i*997) & 0xFF;


	clock_gettime(CLOCK_MONOTONIC_RAW, &start);
	for (i = 0; i < n; i++)
		sum += checksum_block(page + BLCKSZ*(i % b), (uint32) i);
	clock_gettime(CLOCK_MONOTONIC_RAW, &end);

	delta = (double)(end.tv_sec - start.tv_sec) + (1e-9*(double) (end.tv_nsec - start.tv_nsec));

        printf("%0.5fms @ %0.3f GB/s\n", delta*1000, (((double) BLCKSZ) * n)/delta/1e9);
	printf("  %0.1fns per iteration\n", delta*1e9/n);
	return 0;
}
