2 * "$Id: rasterbench.c 6649 2007-07-11 21:46:42Z mike $"
4 * Raster benchmark program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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...
29 #include <cups/raster.h>
41 #define TEST_WIDTH 1024
42 #define TEST_HEIGHT 1024
44 #define TEST_PASSES 20
51 static double compute_median(double *secs
);
52 static double get_time(void);
53 static void read_test(int fd
);
54 static int run_read_test(void);
55 static void write_test(int fd
, cups_mode_t mode
);
59 * 'main()' - Benchmark the raster read/write functions.
62 int /* O - Exit status */
63 main(int argc
, /* I - Number of command-line args */
64 char *argv
[]) /* I - Command-line arguments */
66 int i
; /* Looping var */
67 int ras_fd
, /* File descriptor for read process */
68 status
; /* Exit status of read process */
69 double start_secs
, /* Start time */
70 write_secs
, /* Write time */
71 read_secs
, /* Read time */
72 pass_secs
[TEST_PASSES
]; /* Total test times */
73 cups_mode_t mode
; /* Write mode */
77 * See if we have anything on the command-line...
80 if (argc
> 2 || (argc
== 2 && strcmp(argv
[1], "-z")))
82 puts("Usage: rasterbench [-z]");
86 mode
= argc
> 1 ? CUPS_RASTER_WRITE_COMPRESSED
: CUPS_RASTER_WRITE
;
92 signal(SIGPIPE
, SIG_IGN
);
95 * Run the tests several times to get a good average...
98 printf("Test read/write speed of %d pages, %dx%d pixels...\n\n",
99 TEST_PAGES
, TEST_WIDTH
, TEST_HEIGHT
);
100 for (i
= 0; i
< TEST_PASSES
; i
++)
102 printf("PASS %2d: ", i
+ 1);
105 ras_fd
= run_read_test();
106 start_secs
= get_time();
108 write_test(ras_fd
, mode
);
110 write_secs
= get_time();
111 printf(" %.3f write,", write_secs
- start_secs
);
117 read_secs
= get_time();
118 pass_secs
[i
] = read_secs
- start_secs
;
119 printf(" %.3f read, %.3f total\n", read_secs
- write_secs
, pass_secs
[i
]);
122 printf("\nMedian Total Time: %.3f seconds per document\n",
123 compute_median(pass_secs
));
130 * 'compute_median()' - Compute the median time for a test.
133 static double /* O - Median time in seconds */
134 compute_median(double *secs
) /* I - Array of time samples */
136 int i
, j
; /* Looping vars */
137 double temp
; /* Swap variable */
141 * Sort the array into ascending order using a quicky bubble sort...
144 for (i
= 0; i
< (TEST_PASSES
- 1); i
++)
145 for (j
= i
+ 1; j
< TEST_PASSES
; j
++)
146 if (secs
[i
] > secs
[j
])
154 * Return the average of the middle two samples...
157 return (0.5 * (secs
[TEST_PASSES
/ 2 - 1] + secs
[TEST_PASSES
/ 2]));
162 * 'get_time()' - Get the current time in seconds.
165 static double /* O - Time in seconds */
168 struct timeval curtime
; /* Current time */
171 gettimeofday(&curtime
, NULL
);
172 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
177 * 'read_test()' - Benchmark the raster read functions.
181 read_test(int fd
) /* I - File descriptor to read from */
183 int y
; /* Looping var */
184 cups_raster_t
*r
; /* Raster stream */
185 cups_page_header2_t header
; /* Page header */
186 unsigned char buffer
[8 * TEST_WIDTH
];
194 if ((r
= cupsRasterOpen(fd
, CUPS_RASTER_READ
)) == NULL
)
196 perror("Unable to create raster input stream");
200 while (cupsRasterReadHeader2(r
, &header
))
202 for (y
= 0; y
< header
.cupsHeight
; y
++)
203 cupsRasterReadPixels(r
, buffer
, header
.cupsBytesPerLine
);
211 * 'run_read_test()' - Run the read test as a child process via pipes.
214 static int /* O - Standard input of child */
217 int ras_pipes
[2]; /* Raster data pipes */
218 int pid
; /* Child process ID */
224 if ((pid
= fork()) < 0)
227 * Fork error - return -1 on error...
238 * Child comes here - read data from the input pipe...
242 read_test(ras_pipes
[0]);
248 * Parent comes here - return the output pipe...
252 return (ras_pipes
[1]);
258 * 'write_test()' - Benchmark the raster write functions.
262 write_test(int fd
, /* I - File descriptor to write to */
263 cups_mode_t mode
) /* I - Write mode */
265 int page
, x
, y
; /* Looping vars */
266 int count
; /* Number of bytes to set */
267 cups_raster_t
*r
; /* Raster stream */
268 cups_page_header2_t header
; /* Page header */
269 unsigned char data
[32][8 * TEST_WIDTH
];
270 /* Raster data to write */
274 * Create a combination of random data and repeated data to simulate
275 * text with some whitespace.
280 memset(data
, 0, sizeof(data
));
282 for (y
= 0; y
< 28; y
++)
284 for (x
= rand() & 127, count
= (rand() & 15) + 1;
290 x
+= (rand() & 15) + 1;
291 count
= (rand() & 15) + 1;
293 if (x
>= sizeof(data
[0]))
302 * Test write speed...
305 if ((r
= cupsRasterOpen(fd
, mode
)) == NULL
)
307 perror("Unable to create raster output stream");
311 for (page
= 0; page
< TEST_PAGES
; page
++)
313 memset(&header
, 0, sizeof(header
));
314 header
.cupsWidth
= TEST_WIDTH
;
315 header
.cupsHeight
= TEST_HEIGHT
;
316 header
.cupsBytesPerLine
= TEST_WIDTH
;
320 header
.cupsBytesPerLine
*= 4;
321 header
.cupsColorSpace
= CUPS_CSPACE_CMYK
;
322 header
.cupsColorOrder
= CUPS_ORDER_CHUNKED
;
326 header
.cupsColorSpace
= CUPS_CSPACE_K
;
327 header
.cupsColorOrder
= CUPS_ORDER_BANDED
;
332 header
.cupsBytesPerLine
*= 2;
333 header
.cupsBitsPerColor
= 16;
334 header
.cupsBitsPerPixel
= (page
& 1) ? 64 : 16;
338 header
.cupsBitsPerColor
= 8;
339 header
.cupsBitsPerPixel
= (page
& 1) ? 32 : 8;
342 cupsRasterWriteHeader2(r
, &header
);
344 for (y
= 0; y
< TEST_HEIGHT
; y
++)
345 cupsRasterWritePixels(r
, data
[y
& 31], header
.cupsBytesPerLine
);
353 * End of "$Id: rasterbench.c 6649 2007-07-11 21:46:42Z mike $".