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 const char value1[] = "Large ------------------ value"; 38 const char value2[] = "Small -- value"; 39 char nameValuePair[] = "less=more"; 40 const char name[] = "PATH"; 41 const char name2[] = "SHELL"; 42 const int MaxIterations = 1000000; 43 const char Tabs[] = "\t\t\t"; 44 45 46 static int 47 report_time(const char *action, struct timeval *startTime, 48 struct timeval *endTime) 49 { 50 int actionLen; 51 int numTabs; 52 53 actionLen = strlen(action); 54 numTabs = 3 - actionLen / 8; 55 56 return (printf("Time spent executing %s:%.*s%f\n", action, numTabs, Tabs, 57 (endTime->tv_sec - startTime->tv_sec) + 58 (double)(endTime->tv_usec - startTime->tv_usec) / 1000000)); 59 } 60 61 62 int 63 main(int argc, char **argv) 64 { 65 int iterations; 66 struct rusage endUsage; 67 struct rusage startUsage; 68 69 /* 70 * getenv() on the existing environment. 71 */ 72 getrusage(RUSAGE_SELF, &startUsage); 73 74 /* Iterate over setting variable. */ 75 for (iterations = 0; iterations < MaxIterations; iterations++) 76 if (getenv(name) == NULL) 77 err(EXIT_FAILURE, "getenv(name)"); 78 79 getrusage(RUSAGE_SELF, &endUsage); 80 81 report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime); 82 83 84 /* 85 * setenv() a variable with a large value. 86 */ 87 getrusage(RUSAGE_SELF, &startUsage); 88 89 /* Iterate over setting variable. */ 90 for (iterations = 0; iterations < MaxIterations; iterations++) 91 if (setenv(name, value1, 1) == -1) 92 err(EXIT_FAILURE, "setenv(name, value1, 1)"); 93 94 getrusage(RUSAGE_SELF, &endUsage); 95 96 report_time("setenv(name, value1, 1)", &startUsage.ru_utime, 97 &endUsage.ru_utime); 98 99 100 /* 101 * getenv() the new variable on the new environment. 102 */ 103 getrusage(RUSAGE_SELF, &startUsage); 104 105 /* Iterate over setting variable. */ 106 for (iterations = 0; iterations < MaxIterations; iterations++) 107 /* Set large value to variable. */ 108 if (getenv(name) == NULL) 109 err(EXIT_FAILURE, "getenv(name)"); 110 111 getrusage(RUSAGE_SELF, &endUsage); 112 113 report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime); 114 115 116 /* 117 * getenv() a different variable on the new environment. 118 */ 119 getrusage(RUSAGE_SELF, &startUsage); 120 121 /* Iterate over setting variable. */ 122 for (iterations = 0; iterations < MaxIterations; iterations++) 123 /* Set large value to variable. */ 124 if (getenv(name2) == NULL) 125 err(EXIT_FAILURE, "getenv(name2)"); 126 127 getrusage(RUSAGE_SELF, &endUsage); 128 129 report_time("getenv(name2)", &startUsage.ru_utime, &endUsage.ru_utime); 130 131 132 /* 133 * setenv() a variable with a small value. 134 */ 135 getrusage(RUSAGE_SELF, &startUsage); 136 137 /* Iterate over setting variable. */ 138 for (iterations = 0; iterations < MaxIterations; iterations++) 139 if (setenv(name, value2, 1) == -1) 140 err(EXIT_FAILURE, "setenv(name, value2, 1)"); 141 142 getrusage(RUSAGE_SELF, &endUsage); 143 144 report_time("setenv(name, value2, 1)", &startUsage.ru_utime, 145 &endUsage.ru_utime); 146 147 148 /* 149 * getenv() a different variable on the new environment. 150 */ 151 getrusage(RUSAGE_SELF, &startUsage); 152 153 /* Iterate over setting variable. */ 154 for (iterations = 0; iterations < MaxIterations; iterations++) 155 /* Set large value to variable. */ 156 if (getenv(name2) == NULL) 157 err(EXIT_FAILURE, "getenv(name)"); 158 159 getrusage(RUSAGE_SELF, &endUsage); 160 161 report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime); 162 163 164 /* 165 * getenv() a different variable on the new environment. 166 */ 167 getrusage(RUSAGE_SELF, &startUsage); 168 169 /* Iterate over setting variable. */ 170 for (iterations = 0; iterations < MaxIterations; iterations++) 171 /* Set large value to variable. */ 172 if (getenv(name2) == NULL) 173 err(EXIT_FAILURE, "getenv(name2)"); 174 175 getrusage(RUSAGE_SELF, &endUsage); 176 177 report_time("getenv(name2)", &startUsage.ru_utime, &endUsage.ru_utime); 178 179 180 /* 181 * putenv() a variable with a small value. 182 */ 183 getrusage(RUSAGE_SELF, &startUsage); 184 185 /* Iterate over setting variable. */ 186 for (iterations = 0; iterations < MaxIterations; iterations++) 187 if (putenv(nameValuePair) == -1) 188 err(EXIT_FAILURE, "putenv(nameValuePair)"); 189 190 getrusage(RUSAGE_SELF, &endUsage); 191 192 report_time("putenv(nameValuePair)", &startUsage.ru_utime, 193 &endUsage.ru_utime); 194 195 196 exit(EXIT_SUCCESS); 197 } 198