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