/*-
 * Copyright (c) 2007 Sean Farley <sean-freebsd@farley.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    without modification, immediately at the beginning of the file.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/limits.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <err.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


const int MaxIterations = 10000000;


#if defined(TESTLEN)
size_t xstrlen(const char *str);
#define STRLEN xstrlen
#else
#define STRLEN strlen
#endif


static int report_time(const char *program, const char *action,
    size_t len, struct timeval *startTime, struct timeval *endTime)
{
	const char Tabs[] = "\t\t\t\t\t";
	int actionTabs;
	int programTabs;

	program = basename(program);
	actionTabs = 2 - strlen(action) / 8;
	programTabs = 2 - (strlen(program) + 1) / 8;

	return (printf("%s:%.*stime spent executing %s = %d:%.*s%f\n", program,
	    programTabs, Tabs, action, len, actionTabs, Tabs, (endTime->tv_sec -
	    startTime->tv_sec) + (double)(endTime->tv_usec -
	    startTime->tv_usec) / 1000000));
}


int main(int argc,
    char **argv)
{
	const char *string = "abcasdlkjgalskgjlaskdgjlaskgjlaskgjalsjg";
	int iterations;
	int len;
	struct rusage endUsage;
	struct rusage startUsage;

	/* Use an alternate string. */
	if (argc == 2)
		string = argv[1];

	/* Start time. */
	getrusage(RUSAGE_SELF, &startUsage);

	/* Iterate over setting variable. */
	len = strlen(string);
	for (iterations = 0; iterations < MaxIterations; iterations++)
		if (len != STRLEN(string))
			errx(EXIT_FAILURE, "Oops");

	/* End time. */
	getrusage(RUSAGE_SELF, &endUsage);

	report_time(argv[0], "strlen(string)", strlen(string), &startUsage.ru_utime,
	    &endUsage.ru_utime);

	exit(EXIT_SUCCESS);
}

