1171195Sscf/*- 2171195Sscf * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org> 3171195Sscf * All rights reserved. 4171195Sscf * 5171195Sscf * Redistribution and use in source and binary forms, with or without 6171195Sscf * modification, are permitted provided that the following conditions 7171195Sscf * are met: 8171195Sscf * 1. Redistributions of source code must retain the above copyright 9171195Sscf * notice, this list of conditions and the following disclaimer. 10171195Sscf * 2. Redistributions in binary form must reproduce the above copyright 11171195Sscf * notice, this list of conditions and the following disclaimer in the 12171195Sscf * documentation and/or other materials provided with the distribution. 13171195Sscf * 14171195Sscf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15171195Sscf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16171195Sscf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17171195Sscf * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18171195Sscf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19171195Sscf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20171195Sscf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21171195Sscf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22171195Sscf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23171195Sscf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24171195Sscf * SUCH DAMAGE. 25171195Sscf */ 26171525Sscf#include <sys/types.h> 27171195Sscf#include <sys/time.h> 28171525Sscf#include <sys/resource.h> 29171195Sscf#include <err.h> 30171195Sscf#include <stdio.h> 31171195Sscf#include <stdlib.h> 32171195Sscf#include <string.h> 33171195Sscf#include <unistd.h> 34171195Sscf 35171195Sscf 36171195Sscf#include <sys/cdefs.h> 37171195Sscf__FBSDID("$FreeBSD$"); 38171195Sscf 39171195Sscf 40171195Sscfconst char value1[] = "Large ------------------ value"; 41171195Sscfconst char value2[] = "Small -- value"; 42171195Sscfchar nameValuePair[] = "less=more"; 43171195Sscfconst char name[] = "PATH"; 44171195Sscfconst char name2[] = "SHELL"; 45171195Sscfconst int MaxIterations = 1000000; 46171195Sscfconst char Tabs[] = "\t\t\t"; 47171195Sscf 48171195Sscf 49171195Sscfstatic int 50171195Sscfreport_time(const char *action, struct timeval *startTime, 51171195Sscf struct timeval *endTime) 52171195Sscf{ 53171195Sscf int actionLen; 54171195Sscf int numTabs; 55171195Sscf 56171195Sscf actionLen = strlen(action); 57171195Sscf numTabs = 3 - actionLen / 8; 58171195Sscf 59171195Sscf return (printf("Time spent executing %s:%.*s%f\n", action, numTabs, Tabs, 60171195Sscf (endTime->tv_sec - startTime->tv_sec) + 61171195Sscf (double)(endTime->tv_usec - startTime->tv_usec) / 1000000)); 62171195Sscf} 63171195Sscf 64171195Sscf 65171195Sscfint 66171195Sscfmain(int argc, char **argv) 67171195Sscf{ 68171195Sscf int iterations; 69171525Sscf struct rusage endUsage; 70171525Sscf struct rusage startUsage; 71171195Sscf 72171195Sscf /* 73171195Sscf * getenv() on the existing environment. 74171195Sscf */ 75171525Sscf getrusage(RUSAGE_SELF, &startUsage); 76171195Sscf 77171195Sscf /* Iterate over setting variable. */ 78171195Sscf for (iterations = 0; iterations < MaxIterations; iterations++) 79171195Sscf if (getenv(name) == NULL) 80171195Sscf err(EXIT_FAILURE, "getenv(name)"); 81171195Sscf 82171525Sscf getrusage(RUSAGE_SELF, &endUsage); 83171195Sscf 84171525Sscf report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime); 85171195Sscf 86171195Sscf 87171195Sscf /* 88171195Sscf * setenv() a variable with a large value. 89171195Sscf */ 90171525Sscf getrusage(RUSAGE_SELF, &startUsage); 91171195Sscf 92171195Sscf /* Iterate over setting variable. */ 93171195Sscf for (iterations = 0; iterations < MaxIterations; iterations++) 94171195Sscf if (setenv(name, value1, 1) == -1) 95171195Sscf err(EXIT_FAILURE, "setenv(name, value1, 1)"); 96171195Sscf 97171525Sscf getrusage(RUSAGE_SELF, &endUsage); 98171195Sscf 99171525Sscf report_time("setenv(name, value1, 1)", &startUsage.ru_utime, 100171525Sscf &endUsage.ru_utime); 101171195Sscf 102171195Sscf 103171195Sscf /* 104171195Sscf * getenv() the new variable on the new environment. 105171195Sscf */ 106171525Sscf getrusage(RUSAGE_SELF, &startUsage); 107171195Sscf 108171195Sscf /* Iterate over setting variable. */ 109171195Sscf for (iterations = 0; iterations < MaxIterations; iterations++) 110171195Sscf /* Set large value to variable. */ 111171195Sscf if (getenv(name) == NULL) 112171195Sscf err(EXIT_FAILURE, "getenv(name)"); 113171195Sscf 114171525Sscf getrusage(RUSAGE_SELF, &endUsage); 115171195Sscf 116171525Sscf report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime); 117171195Sscf 118171195Sscf 119171195Sscf /* 120171195Sscf * getenv() a different variable on the new environment. 121171195Sscf */ 122171525Sscf getrusage(RUSAGE_SELF, &startUsage); 123171195Sscf 124171195Sscf /* Iterate over setting variable. */ 125171195Sscf for (iterations = 0; iterations < MaxIterations; iterations++) 126171195Sscf /* Set large value to variable. */ 127171195Sscf if (getenv(name2) == NULL) 128171195Sscf err(EXIT_FAILURE, "getenv(name2)"); 129171195Sscf 130171525Sscf getrusage(RUSAGE_SELF, &endUsage); 131171195Sscf 132171525Sscf report_time("getenv(name2)", &startUsage.ru_utime, &endUsage.ru_utime); 133171195Sscf 134171195Sscf 135171195Sscf /* 136171195Sscf * setenv() a variable with a small value. 137171195Sscf */ 138171525Sscf getrusage(RUSAGE_SELF, &startUsage); 139171195Sscf 140171195Sscf /* Iterate over setting variable. */ 141171195Sscf for (iterations = 0; iterations < MaxIterations; iterations++) 142171195Sscf if (setenv(name, value2, 1) == -1) 143171195Sscf err(EXIT_FAILURE, "setenv(name, value2, 1)"); 144171195Sscf 145171525Sscf getrusage(RUSAGE_SELF, &endUsage); 146171195Sscf 147171525Sscf report_time("setenv(name, value2, 1)", &startUsage.ru_utime, 148171525Sscf &endUsage.ru_utime); 149171195Sscf 150171195Sscf 151171195Sscf /* 152171195Sscf * getenv() a different variable on the new environment. 153171195Sscf */ 154171525Sscf getrusage(RUSAGE_SELF, &startUsage); 155171195Sscf 156171195Sscf /* Iterate over setting variable. */ 157171195Sscf for (iterations = 0; iterations < MaxIterations; iterations++) 158171195Sscf /* Set large value to variable. */ 159171195Sscf if (getenv(name2) == NULL) 160171195Sscf err(EXIT_FAILURE, "getenv(name)"); 161171195Sscf 162171525Sscf getrusage(RUSAGE_SELF, &endUsage); 163171195Sscf 164171525Sscf report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime); 165171195Sscf 166171195Sscf 167171195Sscf /* 168171195Sscf * getenv() a different variable on the new environment. 169171195Sscf */ 170171525Sscf getrusage(RUSAGE_SELF, &startUsage); 171171195Sscf 172171195Sscf /* Iterate over setting variable. */ 173171195Sscf for (iterations = 0; iterations < MaxIterations; iterations++) 174171195Sscf /* Set large value to variable. */ 175171195Sscf if (getenv(name2) == NULL) 176171195Sscf err(EXIT_FAILURE, "getenv(name2)"); 177171195Sscf 178171525Sscf getrusage(RUSAGE_SELF, &endUsage); 179171195Sscf 180171525Sscf report_time("getenv(name2)", &startUsage.ru_utime, &endUsage.ru_utime); 181171195Sscf 182171195Sscf 183171195Sscf /* 184171195Sscf * putenv() a variable with a small value. 185171195Sscf */ 186171525Sscf getrusage(RUSAGE_SELF, &startUsage); 187171195Sscf 188171195Sscf /* Iterate over setting variable. */ 189171195Sscf for (iterations = 0; iterations < MaxIterations; iterations++) 190171195Sscf if (putenv(nameValuePair) == -1) 191171195Sscf err(EXIT_FAILURE, "putenv(nameValuePair)"); 192171195Sscf 193171525Sscf getrusage(RUSAGE_SELF, &endUsage); 194171195Sscf 195171525Sscf report_time("putenv(nameValuePair)", &startUsage.ru_utime, 196171525Sscf &endUsage.ru_utime); 197171195Sscf 198171195Sscf 199171195Sscf exit(EXIT_SUCCESS); 200171195Sscf} 201