2 * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/types.h>
28 #include <sys/resource.h>
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";
47 report_time(const char *action, struct timeval *startTime,
48 struct timeval *endTime)
53 actionLen = strlen(action);
54 numTabs = 3 - actionLen / 8;
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));
63 main(int argc, char **argv)
66 struct rusage endUsage;
67 struct rusage startUsage;
70 * getenv() on the existing environment.
72 getrusage(RUSAGE_SELF, &startUsage);
74 /* Iterate over setting variable. */
75 for (iterations = 0; iterations < MaxIterations; iterations++)
76 if (getenv(name) == NULL)
77 err(EXIT_FAILURE, "getenv(name)");
79 getrusage(RUSAGE_SELF, &endUsage);
81 report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime);
85 * setenv() a variable with a large value.
87 getrusage(RUSAGE_SELF, &startUsage);
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)");
94 getrusage(RUSAGE_SELF, &endUsage);
96 report_time("setenv(name, value1, 1)", &startUsage.ru_utime,
101 * getenv() the new variable on the new environment.
103 getrusage(RUSAGE_SELF, &startUsage);
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)");
111 getrusage(RUSAGE_SELF, &endUsage);
113 report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime);
117 * getenv() a different variable on the new environment.
119 getrusage(RUSAGE_SELF, &startUsage);
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)");
127 getrusage(RUSAGE_SELF, &endUsage);
129 report_time("getenv(name2)", &startUsage.ru_utime, &endUsage.ru_utime);
133 * setenv() a variable with a small value.
135 getrusage(RUSAGE_SELF, &startUsage);
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)");
142 getrusage(RUSAGE_SELF, &endUsage);
144 report_time("setenv(name, value2, 1)", &startUsage.ru_utime,
149 * getenv() a different variable on the new environment.
151 getrusage(RUSAGE_SELF, &startUsage);
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)");
159 getrusage(RUSAGE_SELF, &endUsage);
161 report_time("getenv(name)", &startUsage.ru_utime, &endUsage.ru_utime);
165 * getenv() a different variable on the new environment.
167 getrusage(RUSAGE_SELF, &startUsage);
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)");
175 getrusage(RUSAGE_SELF, &endUsage);
177 report_time("getenv(name2)", &startUsage.ru_utime, &endUsage.ru_utime);
181 * putenv() a variable with a small value.
183 getrusage(RUSAGE_SELF, &startUsage);
185 /* Iterate over setting variable. */
186 for (iterations = 0; iterations < MaxIterations; iterations++)
187 if (putenv(nameValuePair) == -1)
188 err(EXIT_FAILURE, "putenv(nameValuePair)");
190 getrusage(RUSAGE_SELF, &endUsage);
192 report_time("putenv(nameValuePair)", &startUsage.ru_utime,