]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/rasterbench.c
Fix some compile issues, use our implementation of rresvport_af since it is
[thirdparty/cups.git] / filter / rasterbench.c
CommitLineData
ed486911 1/*
f2d18633 2 * "$Id$"
ed486911 3 *
7e86f2f6 4 * Raster benchmark program for CUPS.
ed486911 5 *
a6a4a2f5 6 * Copyright 2007-2016 by Apple Inc.
7e86f2f6 7 * Copyright 1997-2006 by Easy Software Products.
ed486911 8 *
7e86f2f6
MS
9 * These coded instructions, statements, and computer programs are the
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/".
ed486911 14 *
7e86f2f6 15 * This file is subject to the Apple OS-Developed Software exception.
ed486911 16 */
17
18/*
19 * Include necessary headers...
20 */
21
41681883 22#include <config.h>
ac884b6a 23#include <cups/raster.h>
a6a4a2f5 24#include <stdio.h>
ed486911 25#include <stdlib.h>
26#include <sys/time.h>
27#include <signal.h>
28#include <unistd.h>
29#include <sys/wait.h>
30
31
32/*
33 * Constants...
34 */
35
36#define TEST_WIDTH 1024
37#define TEST_HEIGHT 1024
38#define TEST_PAGES 16
39#define TEST_PASSES 20
40
41
42/*
43 * Local functions...
44 */
45
46static double compute_median(double *secs);
47static double get_time(void);
48static void read_test(int fd);
49static int run_read_test(void);
db0bd74a 50static void write_test(int fd, cups_mode_t mode);
ed486911 51
52
53/*
54 * 'main()' - Benchmark the raster read/write functions.
55 */
56
57int /* O - Exit status */
db0bd74a
MS
58main(int argc, /* I - Number of command-line args */
59 char *argv[]) /* I - Command-line arguments */
ed486911 60{
61 int i; /* Looping var */
62 int ras_fd, /* File descriptor for read process */
63 status; /* Exit status of read process */
64 double start_secs, /* Start time */
65 write_secs, /* Write time */
66 read_secs, /* Read time */
67 pass_secs[TEST_PASSES]; /* Total test times */
db0bd74a 68 cups_mode_t mode; /* Write mode */
ed486911 69
70
db0bd74a
MS
71 /*
72 * See if we have anything on the command-line...
73 */
74
75 if (argc > 2 || (argc == 2 && strcmp(argv[1], "-z")))
76 {
77 puts("Usage: rasterbench [-z]");
78 return (1);
79 }
80
81 mode = argc > 1 ? CUPS_RASTER_WRITE_COMPRESSED : CUPS_RASTER_WRITE;
82
ed486911 83 /*
84 * Ignore SIGPIPE...
85 */
86
87 signal(SIGPIPE, SIG_IGN);
88
89 /*
90 * Run the tests several times to get a good average...
91 */
92
93 printf("Test read/write speed of %d pages, %dx%d pixels...\n\n",
94 TEST_PAGES, TEST_WIDTH, TEST_HEIGHT);
95 for (i = 0; i < TEST_PASSES; i ++)
96 {
97 printf("PASS %2d: ", i + 1);
98 fflush(stdout);
99
100 ras_fd = run_read_test();
101 start_secs = get_time();
102
db0bd74a 103 write_test(ras_fd, mode);
ed486911 104
105 write_secs = get_time();
106 printf(" %.3f write,", write_secs - start_secs);
107 fflush(stdout);
108
109 close(ras_fd);
110 wait(&status);
111
112 read_secs = get_time();
113 pass_secs[i] = read_secs - start_secs;
114 printf(" %.3f read, %.3f total\n", read_secs - write_secs, pass_secs[i]);
115 }
116
117 printf("\nMedian Total Time: %.3f seconds per document\n",
118 compute_median(pass_secs));
119
120 return (0);
121}
122
123
124/*
125 * 'compute_median()' - Compute the median time for a test.
126 */
127
128static double /* O - Median time in seconds */
129compute_median(double *secs) /* I - Array of time samples */
130{
131 int i, j; /* Looping vars */
132 double temp; /* Swap variable */
133
134
135 /*
136 * Sort the array into ascending order using a quicky bubble sort...
137 */
138
139 for (i = 0; i < (TEST_PASSES - 1); i ++)
140 for (j = i + 1; j < TEST_PASSES; j ++)
141 if (secs[i] > secs[j])
142 {
143 temp = secs[i];
144 secs[i] = secs[j];
145 secs[j] = temp;
146 }
147
148 /*
149 * Return the average of the middle two samples...
150 */
151
152 return (0.5 * (secs[TEST_PASSES / 2 - 1] + secs[TEST_PASSES / 2]));
153}
154
155
156/*
157 * 'get_time()' - Get the current time in seconds.
158 */
159
160static double /* O - Time in seconds */
161get_time(void)
162{
163 struct timeval curtime; /* Current time */
164
165
166 gettimeofday(&curtime, NULL);
167 return (curtime.tv_sec + 0.000001 * curtime.tv_usec);
168}
169
170
171/*
172 * 'read_test()' - Benchmark the raster read functions.
173 */
174
175static void
176read_test(int fd) /* I - File descriptor to read from */
177{
7e86f2f6 178 unsigned y; /* Looping var */
ed486911 179 cups_raster_t *r; /* Raster stream */
db0bd74a 180 cups_page_header2_t header; /* Page header */
ed486911 181 unsigned char buffer[8 * TEST_WIDTH];
182 /* Read buffer */
183
184
185 /*
186 * Test read speed...
187 */
188
189 if ((r = cupsRasterOpen(fd, CUPS_RASTER_READ)) == NULL)
190 {
191 perror("Unable to create raster input stream");
192 return;
193 }
194
db0bd74a 195 while (cupsRasterReadHeader2(r, &header))
ed486911 196 {
197 for (y = 0; y < header.cupsHeight; y ++)
198 cupsRasterReadPixels(r, buffer, header.cupsBytesPerLine);
199 }
200
201 cupsRasterClose(r);
202}
203
204
205/*
206 * 'run_read_test()' - Run the read test as a child process via pipes.
207 */
208
209static int /* O - Standard input of child */
210run_read_test(void)
211{
212 int ras_pipes[2]; /* Raster data pipes */
213 int pid; /* Child process ID */
214
215
216 if (pipe(ras_pipes))
217 return (-1);
218
219 if ((pid = fork()) < 0)
220 {
221 /*
222 * Fork error - return -1 on error...
223 */
224
225 close(ras_pipes[0]);
226 close(ras_pipes[1]);
227
228 return (-1);
229 }
230 else if (pid == 0)
231 {
232 /*
233 * Child comes here - read data from the input pipe...
234 */
235
236 close(ras_pipes[1]);
237 read_test(ras_pipes[0]);
238 exit(0);
239 }
240 else
241 {
242 /*
243 * Parent comes here - return the output pipe...
244 */
245
246 close(ras_pipes[0]);
247 return (ras_pipes[1]);
248 }
249}
250
251
252/*
253 * 'write_test()' - Benchmark the raster write functions.
254 */
255
256static void
db0bd74a
MS
257write_test(int fd, /* I - File descriptor to write to */
258 cups_mode_t mode) /* I - Write mode */
ed486911 259{
7e86f2f6
MS
260 unsigned page, x, y; /* Looping vars */
261 unsigned count; /* Number of bytes to set */
ed486911 262 cups_raster_t *r; /* Raster stream */
db0bd74a 263 cups_page_header2_t header; /* Page header */
ed486911 264 unsigned char data[32][8 * TEST_WIDTH];
265 /* Raster data to write */
266
267
268 /*
269 * Create a combination of random data and repeated data to simulate
270 * text with some whitespace.
271 */
272
41681883 273 CUPS_SRAND(time(NULL));
ed486911 274
275 memset(data, 0, sizeof(data));
276
277 for (y = 0; y < 28; y ++)
278 {
41681883 279 for (x = CUPS_RAND() & 127, count = (CUPS_RAND() & 15) + 1;
ed486911 280 x < sizeof(data[0]);
281 x ++, count --)
282 {
283 if (count <= 0)
284 {
41681883
MS
285 x += (CUPS_RAND() & 15) + 1;
286 count = (CUPS_RAND() & 15) + 1;
ed486911 287
288 if (x >= sizeof(data[0]))
289 break;
290 }
291
7e86f2f6 292 data[y][x] = (unsigned char)CUPS_RAND();
ed486911 293 }
294 }
295
296 /*
297 * Test write speed...
298 */
299
db0bd74a 300 if ((r = cupsRasterOpen(fd, mode)) == NULL)
ed486911 301 {
302 perror("Unable to create raster output stream");
303 return;
304 }
305
306 for (page = 0; page < TEST_PAGES; page ++)
307 {
308 memset(&header, 0, sizeof(header));
309 header.cupsWidth = TEST_WIDTH;
310 header.cupsHeight = TEST_HEIGHT;
311 header.cupsBytesPerLine = TEST_WIDTH;
312
313 if (page & 1)
314 {
315 header.cupsBytesPerLine *= 4;
316 header.cupsColorSpace = CUPS_CSPACE_CMYK;
317 header.cupsColorOrder = CUPS_ORDER_CHUNKED;
318 }
319 else
320 {
321 header.cupsColorSpace = CUPS_CSPACE_K;
322 header.cupsColorOrder = CUPS_ORDER_BANDED;
323 }
324
325 if (page & 2)
326 {
327 header.cupsBytesPerLine *= 2;
328 header.cupsBitsPerColor = 16;
329 header.cupsBitsPerPixel = (page & 1) ? 64 : 16;
330 }
331 else
332 {
333 header.cupsBitsPerColor = 8;
334 header.cupsBitsPerPixel = (page & 1) ? 32 : 8;
335 }
336
db0bd74a 337 cupsRasterWriteHeader2(r, &header);
ed486911 338
339 for (y = 0; y < TEST_HEIGHT; y ++)
340 cupsRasterWritePixels(r, data[y & 31], header.cupsBytesPerLine);
341 }
342
343 cupsRasterClose(r);
344}
345
346
347/*
f2d18633 348 * End of "$Id$".
ed486911 349 */