2 * "$Id: rasterbench.c 9771 2011-05-12 05:21:56Z mike $"
4 * Raster benchmark program for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Benchmark the raster read/write functions.
20 * compute_median() - Compute the median time for a test.
21 * read_test() - Benchmark the raster read functions.
22 * write_test() - Benchmark the raster write functions.
26 * Include necessary headers...
30 #include <cups/raster.h>
42 #define TEST_WIDTH 1024
43 #define TEST_HEIGHT 1024
45 #define TEST_PASSES 20
52 static double compute_median(double *secs);
53 static double get_time(void);
54 static void read_test(int fd);
55 static int run_read_test(void);
56 static void write_test(int fd, cups_mode_t mode);
60 * 'main()' - Benchmark the raster read/write functions.
63 int /* O - Exit status */
64 main(int argc, /* I - Number of command-line args */
65 char *argv[]) /* I - Command-line arguments */
67 int i; /* Looping var */
68 int ras_fd, /* File descriptor for read process */
69 status; /* Exit status of read process */
70 double start_secs, /* Start time */
71 write_secs, /* Write time */
72 read_secs, /* Read time */
73 pass_secs[TEST_PASSES]; /* Total test times */
74 cups_mode_t mode; /* Write mode */
78 * See if we have anything on the command-line...
81 if (argc > 2 || (argc == 2 && strcmp(argv[1], "-z")))
83 puts("Usage: rasterbench [-z]");
87 mode = argc > 1 ? CUPS_RASTER_WRITE_COMPRESSED : CUPS_RASTER_WRITE;
93 signal(SIGPIPE, SIG_IGN);
96 * Run the tests several times to get a good average...
99 printf("Test read/write speed of %d pages, %dx%d pixels...\n\n",
100 TEST_PAGES, TEST_WIDTH, TEST_HEIGHT);
101 for (i = 0; i < TEST_PASSES; i ++)
103 printf("PASS %2d: ", i + 1);
106 ras_fd = run_read_test();
107 start_secs = get_time();
109 write_test(ras_fd, mode);
111 write_secs = get_time();
112 printf(" %.3f write,", write_secs - start_secs);
118 read_secs = get_time();
119 pass_secs[i] = read_secs - start_secs;
120 printf(" %.3f read, %.3f total\n", read_secs - write_secs, pass_secs[i]);
123 printf("\nMedian Total Time: %.3f seconds per document\n",
124 compute_median(pass_secs));
131 * 'compute_median()' - Compute the median time for a test.
134 static double /* O - Median time in seconds */
135 compute_median(double *secs) /* I - Array of time samples */
137 int i, j; /* Looping vars */
138 double temp; /* Swap variable */
142 * Sort the array into ascending order using a quicky bubble sort...
145 for (i = 0; i < (TEST_PASSES - 1); i ++)
146 for (j = i + 1; j < TEST_PASSES; j ++)
147 if (secs[i] > secs[j])
155 * Return the average of the middle two samples...
158 return (0.5 * (secs[TEST_PASSES / 2 - 1] + secs[TEST_PASSES / 2]));
163 * 'get_time()' - Get the current time in seconds.
166 static double /* O - Time in seconds */
169 struct timeval curtime; /* Current time */
172 gettimeofday(&curtime, NULL);
173 return (curtime.tv_sec + 0.000001 * curtime.tv_usec);
178 * 'read_test()' - Benchmark the raster read functions.
182 read_test(int fd) /* I - File descriptor to read from */
184 int y; /* Looping var */
185 cups_raster_t *r; /* Raster stream */
186 cups_page_header2_t header; /* Page header */
187 unsigned char buffer[8 * TEST_WIDTH];
195 if ((r = cupsRasterOpen(fd, CUPS_RASTER_READ)) == NULL)
197 perror("Unable to create raster input stream");
201 while (cupsRasterReadHeader2(r, &header))
203 for (y = 0; y < header.cupsHeight; y ++)
204 cupsRasterReadPixels(r, buffer, header.cupsBytesPerLine);
212 * 'run_read_test()' - Run the read test as a child process via pipes.
215 static int /* O - Standard input of child */
218 int ras_pipes[2]; /* Raster data pipes */
219 int pid; /* Child process ID */
225 if ((pid = fork()) < 0)
228 * Fork error - return -1 on error...
239 * Child comes here - read data from the input pipe...
243 read_test(ras_pipes[0]);
249 * Parent comes here - return the output pipe...
253 return (ras_pipes[1]);
259 * 'write_test()' - Benchmark the raster write functions.
263 write_test(int fd, /* I - File descriptor to write to */
264 cups_mode_t mode) /* I - Write mode */
266 int page, x, y; /* Looping vars */
267 int count; /* Number of bytes to set */
268 cups_raster_t *r; /* Raster stream */
269 cups_page_header2_t header; /* Page header */
270 unsigned char data[32][8 * TEST_WIDTH];
271 /* Raster data to write */
275 * Create a combination of random data and repeated data to simulate
276 * text with some whitespace.
279 CUPS_SRAND(time(NULL));
281 memset(data, 0, sizeof(data));
283 for (y = 0; y < 28; y ++)
285 for (x = CUPS_RAND() & 127, count = (CUPS_RAND() & 15) + 1;
291 x += (CUPS_RAND() & 15) + 1;
292 count = (CUPS_RAND() & 15) + 1;
294 if (x >= sizeof(data[0]))
298 data[y][x] = CUPS_RAND();
303 * Test write speed...
306 if ((r = cupsRasterOpen(fd, mode)) == NULL)
308 perror("Unable to create raster output stream");
312 for (page = 0; page < TEST_PAGES; page ++)
314 memset(&header, 0, sizeof(header));
315 header.cupsWidth = TEST_WIDTH;
316 header.cupsHeight = TEST_HEIGHT;
317 header.cupsBytesPerLine = TEST_WIDTH;
321 header.cupsBytesPerLine *= 4;
322 header.cupsColorSpace = CUPS_CSPACE_CMYK;
323 header.cupsColorOrder = CUPS_ORDER_CHUNKED;
327 header.cupsColorSpace = CUPS_CSPACE_K;
328 header.cupsColorOrder = CUPS_ORDER_BANDED;
333 header.cupsBytesPerLine *= 2;
334 header.cupsBitsPerColor = 16;
335 header.cupsBitsPerPixel = (page & 1) ? 64 : 16;
339 header.cupsBitsPerColor = 8;
340 header.cupsBitsPerPixel = (page & 1) ? 32 : 8;
343 cupsRasterWriteHeader2(r, &header);
345 for (y = 0; y < TEST_HEIGHT; y ++)
346 cupsRasterWritePixels(r, data[y & 31], header.cupsBytesPerLine);
354 * End of "$Id: rasterbench.c 9771 2011-05-12 05:21:56Z mike $".