timings.c revision 171195
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/time.h>
27#include <err.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/tools/regression/environ/timings.c 171195 2007-07-04 00:00:41Z scf $");
36
37
38const char value1[] = "Large ------------------ value";
39const char value2[] = "Small -- value";
40char nameValuePair[] = "less=more";
41const char name[] = "PATH";
42const char name2[] = "SHELL";
43const int MaxIterations = 1000000;
44const char Tabs[] = "\t\t\t";
45
46
47static int
48report_time(const char *action, struct timeval *startTime,
49    struct timeval *endTime)
50{
51	int actionLen;
52	int numTabs;
53
54	actionLen = strlen(action);
55	numTabs = 3 - actionLen / 8;
56
57	return (printf("Time spent executing %s:%.*s%f\n", action, numTabs, Tabs,
58	    (endTime->tv_sec - startTime->tv_sec) +
59	    (double)(endTime->tv_usec - startTime->tv_usec) / 1000000));
60}
61
62
63int
64main(int argc, char **argv)
65{
66	int iterations;
67	struct timeval endTime;
68	struct timeval startTime;
69
70	/*
71	 * getenv() on the existing environment.
72	 */
73	gettimeofday(&startTime, NULL);
74
75	/* Iterate over setting variable. */
76	for (iterations = 0; iterations < MaxIterations; iterations++)
77		if (getenv(name) == NULL)
78			err(EXIT_FAILURE, "getenv(name)");
79
80	gettimeofday(&endTime, NULL);
81
82	report_time("getenv(name)", &startTime, &endTime);
83
84
85	/*
86	 * setenv() a variable with a large value.
87	 */
88	gettimeofday(&startTime, NULL);
89
90	/* Iterate over setting variable. */
91	for (iterations = 0; iterations < MaxIterations; iterations++)
92		if (setenv(name, value1, 1) == -1)
93			err(EXIT_FAILURE, "setenv(name, value1, 1)");
94
95	gettimeofday(&endTime, NULL);
96
97	report_time("setenv(name, value1, 1)", &startTime, &endTime);
98
99
100	/*
101	 * getenv() the new variable on the new environment.
102	 */
103	gettimeofday(&startTime, NULL);
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	gettimeofday(&endTime, NULL);
112
113	report_time("getenv(name)", &startTime, &endTime);
114
115
116	/*
117	 * getenv() a different variable on the new environment.
118	 */
119	gettimeofday(&startTime, NULL);
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	gettimeofday(&endTime, NULL);
128
129	report_time("getenv(name2)", &startTime, &endTime);
130
131
132	/*
133	 * setenv() a variable with a small value.
134	 */
135	gettimeofday(&startTime, NULL);
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	gettimeofday(&endTime, NULL);
143
144	report_time("setenv(name, value2, 1)", &startTime, &endTime);
145
146
147	/*
148	 * getenv() a different variable on the new environment.
149	 */
150	gettimeofday(&startTime, NULL);
151
152	/* Iterate over setting variable. */
153	for (iterations = 0; iterations < MaxIterations; iterations++)
154		/* Set large value to variable. */
155		if (getenv(name2) == NULL)
156			err(EXIT_FAILURE, "getenv(name)");
157
158	gettimeofday(&endTime, NULL);
159
160	report_time("getenv(name)", &startTime, &endTime);
161
162
163	/*
164	 * getenv() a different variable on the new environment.
165	 */
166	gettimeofday(&startTime, NULL);
167
168	/* Iterate over setting variable. */
169	for (iterations = 0; iterations < MaxIterations; iterations++)
170		/* Set large value to variable. */
171		if (getenv(name2) == NULL)
172			err(EXIT_FAILURE, "getenv(name2)");
173
174	gettimeofday(&endTime, NULL);
175
176	report_time("getenv(name2)", &startTime, &endTime);
177
178
179	/*
180	 * putenv() a variable with a small value.
181	 */
182	gettimeofday(&startTime, NULL);
183
184	/* Iterate over setting variable. */
185	for (iterations = 0; iterations < MaxIterations; iterations++)
186		if (putenv(nameValuePair) == -1)
187			err(EXIT_FAILURE, "putenv(nameValuePair)");
188
189	gettimeofday(&endTime, NULL);
190
191	report_time("putenv(nameValuePair)", &startTime, &endTime);
192
193
194	exit(EXIT_SUCCESS);
195}
196