]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/testspeed.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / testspeed.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: testspeed.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * Scheduler speed test for the Common UNIX Printing System (CUPS).
5 *
bc44d920 6 * Copyright 2007 by Apple Inc.
ef416fc2 7 * Copyright 1997-2005 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * Contents:
16 *
e1d6a774 17 * main() - Send multiple IPP requests and report on the average response
18 * time.
19 * do_test() - Run a test on a specific host...
20 * usage() - Show program usage...
ef416fc2 21 */
22
23/*
24 * Include necessary headers...
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <ctype.h>
30#include <sys/types.h>
31#include <sys/time.h>
32#include <sys/wait.h>
33#include <cups/cups.h>
34#include <cups/language.h>
35#include <cups/debug.h>
36#include <errno.h>
37
38
39/*
40 * Local functions...
41 */
42
e1d6a774 43static int do_test(const char *server, http_encryption_t encryption,
44 int requests, int verbose);
45static void usage(void);
ef416fc2 46
47
48/*
49 * 'main()' - Send multiple IPP requests and report on the average response
50 * time.
51 */
52
53int
54main(int argc, /* I - Number of command-line arguments */
55 char *argv[]) /* I - Command-line arguments */
56{
57 int i; /* Looping var */
58 const char *server; /* Server to use */
59 http_encryption_t encryption; /* Encryption to use */
60 int requests; /* Number of requests to send */
61 int children; /* Number of children to fork */
62 int pid; /* Child PID */
63 int status; /* Child status */
64 time_t start, /* Start time */
65 end; /* End time */
66 double elapsed; /* Elapsed time */
67 int verbose; /* Verbosity */
68
69
70 /*
71 * Parse command-line options...
72 */
73
74 requests = 100;
75 children = 5;
76 server = cupsServer();
77 encryption = HTTP_ENCRYPT_IF_REQUESTED;
78 verbose = 0;
79
80 for (i = 1; i < argc; i ++)
81 if (!strcmp(argv[i], "-c"))
82 {
83 i ++;
84 if (i >= argc)
85 usage();
86
87 children = atoi(argv[i]);
88 }
89 else if (!strcmp(argv[i], "-r"))
90 {
91 i ++;
92 if (i >= argc)
93 usage();
94
95 requests = atoi(argv[i]);
96 }
97 else if (!strcmp(argv[i], "-E"))
98 encryption = HTTP_ENCRYPT_REQUIRED;
99 else if (!strcmp(argv[i], "-v"))
100 verbose ++;
101 else if (argv[i][0] == '-')
102 usage();
103 else
104 server = argv[i];
105
106 /*
107 * Then create child processes to act as clients...
108 */
109
110 printf("testspeed: Simulating %d clients with %d requests to %s with %s encryption...\n",
111 children, requests, server,
112 encryption == HTTP_ENCRYPT_IF_REQUESTED ? "no" : "");
113
114 start = time(NULL);
115
116 if (children == 1)
117 {
118 do_test(server, encryption, requests, verbose);
119 }
120 else
121 {
122 for (i = 0; i < children; i ++)
123 if ((pid = fork()) == 0)
124 {
125 /*
126 * Child goes here...
127 */
128
129 exit(do_test(server, encryption, requests, verbose));
130 }
131 else if (pid < 0)
132 {
133 perror("fork failed");
134 break;
135 }
136 else
137 printf("testspeed(%d): Started...\n", pid);
138
139 /*
140 * Wait for children to finish...
141 */
142
143 for (;;)
144 {
145 pid = wait(&status);
146
147 if (pid < 0 && errno != EINTR)
148 break;
149
150 printf("testspeed(%d): Ended (%d)...\n", pid, status);
151 }
152 }
153
154 /*
155 * Compute the total run time...
156 */
157
158 end = time(NULL);
159 elapsed = end - start;
160 i = children * requests;
161
162 printf("testspeed: %dx%d=%d requests in %.1fs (%.3fs/r, %.1fr/s)\n",
163 children, requests, i, elapsed, elapsed / i, i / elapsed);
164
165 /*
166 * Exit with no errors...
167 */
168
169 return (status);
170}
171
172
173/*
174 * 'do_test()' - Run a test on a specific host...
175 */
176
e1d6a774 177static int /* O - Exit status */
ef416fc2 178do_test(const char *server, /* I - Server to use */
179 http_encryption_t encryption, /* I - Encryption to use */
180 int requests, /* I - Number of requests to send */
181 int verbose) /* I - Verbose output? */
182{
183 int i; /* Looping var */
184 http_t *http; /* Connection to server */
185 ipp_t *request, /* IPP Request */
186 *response; /* IPP Response */
187 cups_lang_t *language; /* Default language */
188 struct timeval start, /* Start time */
189 end; /* End time */
190 double elapsed; /* Elapsed time */
191 static ipp_op_t ops[4] = /* Operations to test... */
192 {
193 IPP_PRINT_JOB,
194 CUPS_GET_PRINTERS,
195 CUPS_GET_CLASSES,
196 IPP_GET_JOBS
197 };
198
199
200 /*
201 * Connect to the server...
202 */
203
204 http = httpConnectEncrypt(server, ippPort(), encryption);
205
206 if (http == NULL)
207 {
208 perror("testspeed: unable to connect to server");
209 return (1);
210 }
211
212 language = cupsLangDefault();
213
214 /*
215 * Do multiple requests...
216 */
217
218 for (elapsed = 0.0, i = 0; i < requests; i ++)
219 {
220 if (verbose && (i % 10) == 0)
221 printf("testspeed(%d): %d%% complete...\n", (int)getpid(),
222 i * 100 / requests);
223
224 /*
225 * Build a request which requires the following attributes:
226 *
227 * attributes-charset
228 * attributes-natural-language
229 *
230 * In addition, IPP_GET_JOBS needs a printer-uri attribute.
231 */
232
233 request = ippNew();
234
235 request->request.op.operation_id = ops[i & 3];
236 request->request.op.request_id = 1;
237
238 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
239 "attributes-charset", NULL, cupsLangEncoding(language));
240
241 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
242 "attributes-natural-language", NULL, language->language);
243
244 gettimeofday(&start, NULL);
245
246 switch (request->request.op.operation_id)
247 {
248 case IPP_GET_JOBS :
249 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
250 NULL, "ipp://localhost/printers/");
251
252 default :
253 response = cupsDoRequest(http, request, "/");
254 break;
255
256 case IPP_PRINT_JOB :
257 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
258 NULL, "ipp://localhost/printers/test");
259 response = cupsDoFileRequest(http, request, "/printers/test",
260 "../data/testprint.ps");
261 break;
262 }
263
264 gettimeofday(&end, NULL);
265
266 if (response != NULL)
267 ippDelete(response);
268
269 elapsed += (end.tv_sec - start.tv_sec) +
270 0.000001 * (end.tv_usec - start.tv_usec);
271 }
272
273 cupsLangFree(language);
274 httpClose(http);
275
276 printf("testspeed(%d): %d requests in %.1fs (%.3fs/r, %.1fr/s)\n",
277 (int)getpid(), i, elapsed, elapsed / i, i / elapsed);
278
279 return (0);
280}
281
282
283/*
284 * 'usage()' - Show program usage...
285 */
286
e1d6a774 287static void
ef416fc2 288usage(void)
289{
290 puts("Usage: testspeed [-c children] [-h] [-r requests] [-v] [-E] hostname");
291 exit(0);
292}
293
294
295
296/*
bc44d920 297 * End of "$Id: testspeed.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 298 */