1 /*- 2 * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 #include <sys/types.h> 27 #include <sys/time.h> 28 #include <sys/resource.h> 29 #include <err.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <unistd.h> 34 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 40 const char value1[] = "Large ------------------ value"; 41 const char value2[] = "Small -- value"; 42 char nameValuePair[] = "less=more"; 43 const char name[] = "PATH"; 44 const char name2[] = "SHELL"; 45 const int MaxIterations = 1000000; 46 const char Tabs[] = "\t\t\t"; 47 48 49 static int 50 report_time(const char *action, struct timeval *startTime, 51 struct timeval *endTime) 52 { 53 int actionLen; 54 int numTabs; 55 56 actionLen = strlen(action); 57 numTabs = 3 - actionLen / 8; 58 59 return (printf("Time spent executing %s:%.*s%f\n", action, numTabs, Tabs, 60 (endTime->tv_sec - startTime->tv_sec) + 61 (double)(endTime->tv_usec - startTime->tv_usec) / 1000000)); 62 } 63 64 65 int 66 main(int argc, char **argv) 67 { 68 int iterations; 69 struct rusage endUsage; 70 struct rusage startUsage; 71 72 /* 73 * getenv() on the existing environment. 74 */ 75 getrusage(RUSAGE_SELF, &startUsage); 76 77 /* Iterate over setting variable. */ 78 for (iterations = 0; iterations < MaxIterations; iterations++) 79 if (getenv(name) == NULL) 80 err(EXIT_FAILURE, "getenv(name)"); 81 82 getrusage(RUSAGE_SELF, &endUsage); 83 84 report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime); 85 86 87 /* 88 * setenv() a variable with a large value. 89 */ 90 getrusage(RUSAGE_SELF, &startUsage); 91 92 /* Iterate over setting variable. */ 93 for (iterations = 0; iterations < MaxIterations; iterations++) 94 if (setenv(name, value1, 1) == -1) 95 err(EXIT_FAILURE, "setenv(name, value1, 1)"); 96 97 getrusage(RUSAGE_SELF, &endUsage); 98 99 report_time("setenv(name, value1, 1)", &startUsage.ru_utime, 100 &endUsage.ru_utime); 101 102 103 /* 104 * getenv() the new variable on the new environment. 105 */ 106 getrusage(RUSAGE_SELF, &startUsage); 107 108 /* Iterate over setting variable. */ 109 for (iterations = 0; iterations < MaxIterations; iterations++) 110 /* Set large value to variable. */ 111 if (getenv(name) == NULL) 112 err(EXIT_FAILURE, "getenv(name)"); 113 114 getrusage(RUSAGE_SELF, &endUsage); 115 116 report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime); 117 118 119 /* 120 * getenv() a different variable on the new environment. 121 */ 122 getrusage(RUSAGE_SELF, &startUsage); 123 124 /* Iterate over setting variable. */ 125 for (iterations = 0; iterations < MaxIterations; iterations++) 126 /* Set large value to variable. */ 127 if (getenv(name2) == NULL) 128 err(EXIT_FAILURE, "getenv(name2)"); 129 130 getrusage(RUSAGE_SELF, &endUsage); 131 132 report_time("getenv(name2)", &startUsage.ru_utime, &endUsage.ru_utime); 133 134 135 /* 136 * setenv() a variable with a small value. 137 */ 138 getrusage(RUSAGE_SELF, &startUsage); 139 140 /* Iterate over setting variable. */ 141 for (iterations = 0; iterations < MaxIterations; iterations++) 142 if (setenv(name, value2, 1) == -1) 143 err(EXIT_FAILURE, "setenv(name, value2, 1)"); 144 145 getrusage(RUSAGE_SELF, &endUsage); 146 147 report_time("setenv(name, value2, 1)", &startUsage.ru_utime, 148 &endUsage.ru_utime); 149 150 151 /* 152 * getenv() a different variable on the new environment. 153 */ 154 getrusage(RUSAGE_SELF, &startUsage); 155 156 /* Iterate over setting variable. */ 157 for (iterations = 0; iterations < MaxIterations; iterations++) 158 /* Set large value to variable. */ 159 if (getenv(name2) == NULL) 160 err(EXIT_FAILURE, "getenv(name)"); 161 162 getrusage(RUSAGE_SELF, &endUsage); 163 164 report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime); 165 166 167 /* 168 * getenv() a different variable on the new environment. 169 */ 170 getrusage(RUSAGE_SELF, &startUsage); 171 172 /* Iterate over setting variable. */ 173 for (iterations = 0; iterations < MaxIterations; iterations++) 174 /* Set large value to variable. */ 175 if (getenv(name2) == NULL) 176 err(EXIT_FAILURE, "getenv(name2)"); 177 178 getrusage(RUSAGE_SELF, &endUsage); 179 180 report_time("getenv(name2)", &startUsage.ru_utime, &endUsage.ru_utime); 181 182 183 /* 184 * putenv() a variable with a small value. 185 */ 186 getrusage(RUSAGE_SELF, &startUsage); 187 188 /* Iterate over setting variable. */ 189 for (iterations = 0; iterations < MaxIterations; iterations++) 190 if (putenv(nameValuePair) == -1) 191 err(EXIT_FAILURE, "putenv(nameValuePair)"); 192 193 getrusage(RUSAGE_SELF, &endUsage); 194 195 report_time("putenv(nameValuePair)", &startUsage.ru_utime, 196 &endUsage.ru_utime); 197 198 199 exit(EXIT_SUCCESS); 200 } 201