/*-
 * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.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.
 * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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/time.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/tools/regression/environ/timings.c,v 1.1 2007/07/04 00:00:39 scf Exp $");

extern char **environ;
const char Tabs[] = "\t\t\t";
const int MaxIterations = 10000;


static int
report_time(const char *action, struct timeval *startTime,
    struct timeval *endTime)
{
	int actionLen;
	int numTabs;

	actionLen = strlen(action);
	numTabs = 3 - actionLen / 8;

	return (printf("Time spent executing %s:%.*s%f\n", action, numTabs, Tabs,
	    (endTime->tv_sec - startTime->tv_sec) +
	    (double)(endTime->tv_usec - startTime->tv_usec) / 1000000));
}


char **gen_envcopy(void)
{
	char **cEnv;
	char **env;
	char **envCopy;
	int envCount;

	for (env = environ, envCount = 0; *env != NULL; env++)
		envCount++;

	envCopy = calloc(1, (envCount + 1) * sizeof (*envCopy));

	for (env = environ, cEnv = envCopy; *env != NULL; env++, cEnv++)
		*cEnv = strdup(*env);

	return (envCopy);
}


int
main(int argc, char **argv)
{
	char **mergeEnv1;
	char **mergeEnv2;
	int iterations;
	struct timeval endTime;
	struct timeval startTime;

	/* Setup merge environ. */
	mergeEnv1 = gen_envcopy();
	mergeEnv2 = gen_envcopy();

	gettimeofday(&startTime, NULL);

	/* Iterate over merge environment. */
	for (iterations = 0; iterations < MaxIterations; iterations++) {
		setenv("TEST", "TEST", 1);
		environ = mergeEnv1;
		setenv("TEST", "TEST", 1);
		environ = mergeEnv2;
		setenv("TEST", "TEST", 1);
	}

	gettimeofday(&endTime, NULL);

	report_time("merge environ", &startTime, &endTime);

	exit(EXIT_SUCCESS);
}

