#include "postgres_fe.h"

#include "portability/instr_time.h"

#include "snprintf.c"


#define TIMETEST(fmtstr,...) \
	printf("Test case: %s\n", fmtstr); \
	INSTR_TIME_SET_CURRENT(start); \
	for (i = 0; i < count; i++) \
		snprintf(buffer, sizeof(buffer), fmtstr, __VA_ARGS__); \
	INSTR_TIME_SET_CURRENT(stop); \
	INSTR_TIME_SUBTRACT(stop, start); \
	elapsed = INSTR_TIME_GET_MILLISEC(stop); \
	printf("snprintf time = %g ms total, %g ms per iteration\n", \
		   elapsed, elapsed / count); \
	INSTR_TIME_SET_CURRENT(start); \
	for (i = 0; i < count; i++) \
		pg_snprintf(buffer, sizeof(buffer), fmtstr, __VA_ARGS__); \
	INSTR_TIME_SET_CURRENT(stop); \
	INSTR_TIME_SUBTRACT(stop, start); \
	elapsed2 = INSTR_TIME_GET_MILLISEC(stop); \
	printf("pg_snprintf time = %g ms total, %g ms per iteration\n", \
		   elapsed2, elapsed2 / count); \
	printf("ratio = %.3f\n\n", elapsed2 / elapsed)

int
main(int argc, char **argv)
{
	int			count = 0;
	char		buffer[1000];
	instr_time	start;
	instr_time	stop;
	double		elapsed;
	double		elapsed2;
	int			i;

	if (argc > 1)
		count = atoi(argv[1]);
	if (count <= 0)
		count = 1000000;

	TIMETEST("%2$.*3$f %1$d", 42, 123.456, 2);

	TIMETEST("%.*g", 15, 123.456);

	TIMETEST("%d %d", 15, 16);

	TIMETEST("%10d", 15);

	TIMETEST("%s",
			 "0123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890");

	TIMETEST("%sx",
			 "0123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890");

	TIMETEST("%d 0123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890",
			 42);

	TIMETEST("%1$d 0123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890",
			 42);

	return 0;
}