2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 ** Compile this program using a command line similar to:
13 ** cc -O -L../OBJ/libsm -o b-strl b-strl.c -lsm
14 ** where "OBJ" is the name of the object directory for the platform
15 ** you are compiling on.
16 ** Then run the program:
18 ** and read the output for results and how to interpret the results.
22 SM_RCSID("@(#)$Id: b-strl.c,v 1.25 2005/06/14 23:07:20 ca Exp $")
26 #include <sys/types.h>
28 #include <sm/string.h>
31 #define toseconds(x, y) (x.tv_sec - y.tv_sec)
32 #define LOOPS 4000000L /* initial number of loops */
33 #define MAXTIME 30L /* "maximum" time to run single test */
46 printf("This program benchmarks the performance differences between\n");
47 printf("strlcpy() and sm_strlcpy(), and strlcat() and sm_strlcat().\n");
48 printf("These tests may take several minutes to complete.\n");
57 printf("The configuration indicates the system needs the libsm\n");
58 printf("versions of strlcpy(3) and strlcat(3). Thus, performing\n");
59 printf("these tests will not be of much use.\n");
60 printf("If your OS has strlcpy(3) and strlcat(3) then set the macro\n");
61 printf("SM_CONF_STRL to 1 in your site.config.m4 file\n");
62 printf("(located in ../devtools/Site) and recompile this program.\n");
63 #else /* !SM_CONF_STRL */
69 struct timeval t1
, t2
;
70 char dest
[SRC_SIZE
], source
[SRC_SIZE
];
73 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != -1)
89 printf("If you want to run it, specify -d as option.\n");
94 ** Let's place a small string at the head of dest for
95 ** catenation to happen (it'll be ignored for the copy).
97 (void) sm_strlcpy(dest
, "a small string at the start! ", SRC_SIZE
- 1);
100 ** Let's place a larger string into source for the catenation and
103 (void) strlcpy(source
,
104 " This is the longer string that will be used for catenation and copying for the the performace testing. The longer the string being catenated or copied the greater the difference in measureable performance\n",
107 /* Run-time comments to the user */
110 printf("Test 1: strlcat() versus sm_strlcat()\n");
113 if (gettimeofday(&t1
, NULL
) < 0)
114 fatal("gettimeofday");
116 for (a
= 0; a
< loops
; a
++)
117 strlcat(dest
, source
, SRC_SIZE
- 1);
119 if (gettimeofday(&t2
, NULL
) < 0)
120 fatal("gettimeofday");
122 printf("\tstrlcat() result: %ld seconds\n", one
= toseconds(t2
, t1
));
124 if (gettimeofday(&t1
, NULL
) < 0)
125 fatal("gettimeofday");
127 for (a
= 0; a
< loops
; a
++)
128 sm_strlcat(dest
, source
, SRC_SIZE
- 1);
130 if (gettimeofday(&t2
, NULL
) < 0)
131 fatal("gettimeofday");
133 printf("\tsm_strlcat() result: %ld seconds\n", two
= toseconds(t2
, t1
));
135 if (one
- two
>= -2 && one
- two
<= 2)
138 if (loops
< 0L || one
> MAXTIME
)
140 printf("\t\t** results too close: no decision\n");
144 printf("\t\t** results too close redoing test %ld times **\n",
151 printf("Test 2: strlcpy() versus sm_strlpy()\n");
154 if (gettimeofday(&t1
, NULL
) < 0)
155 fatal("gettimeofday");
157 for (a
= 0; a
< loops
; a
++)
158 strlcpy(dest
, source
, SRC_SIZE
- 1);
160 if (gettimeofday(&t2
, NULL
) < 0)
161 fatal("gettimeofday");
163 printf("\tstrlcpy() result: %ld seconds\n", one
= toseconds(t2
, t1
));
165 if (gettimeofday(&t1
, NULL
) < 0)
166 fatal("gettimeofday");
168 for (a
= 0; a
< loops
; a
++)
169 sm_strlcpy(dest
, source
, SRC_SIZE
- 1);
171 if (gettimeofday(&t2
, NULL
) < 0)
172 fatal("gettimeofday");
174 printf("\tsm_strlcpy() result: %ld seconds\n", two
= toseconds(t2
, t1
));
176 if (one
- two
>= -2 && one
- two
<= 2)
179 if (loops
< 0L || one
> MAXTIME
)
181 printf("\t\t** results too close: no decision\n");
185 printf("\t\t** results too close redoing test %ld times **\n",
192 printf("Interpreting the results:\n");
193 printf("\tFor differences larger than 2 seconds, the lower value is\n");
194 printf("\tbetter and that function should be used for performance\n");
195 printf("\treasons.\n\n");
196 printf("This program will re-run the tests when the difference is\n");
197 printf("less than 2 seconds.\n");
198 printf("The result will vary depending on the compiler optimization\n"); printf("level used. Compiling the sendmail libsm library with a\n");
199 printf("better optimization level can change the results.\n");
200 #endif /* !SM_CONF_STRL */