2 * Raster benchmark program for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
21 #include <cups/raster.h>
34 #define TEST_WIDTH 1024
35 #define TEST_HEIGHT 1024
37 #define TEST_PASSES 20
44 static double compute_median(double *secs
);
45 static double get_time(void);
46 static void read_test(int fd
);
47 static int run_read_test(void);
48 static void write_test(int fd
, cups_mode_t mode
);
52 * 'main()' - Benchmark the raster read/write functions.
55 int /* O - Exit status */
56 main(int argc
, /* I - Number of command-line args */
57 char *argv
[]) /* I - Command-line arguments */
59 int i
; /* Looping var */
60 int ras_fd
, /* File descriptor for read process */
61 status
; /* Exit status of read process */
62 double start_secs
, /* Start time */
63 write_secs
, /* Write time */
64 read_secs
, /* Read time */
65 pass_secs
[TEST_PASSES
]; /* Total test times */
66 cups_mode_t mode
; /* Write mode */
70 * See if we have anything on the command-line...
73 if (argc
> 2 || (argc
== 2 && strcmp(argv
[1], "-z")))
75 puts("Usage: rasterbench [-z]");
79 mode
= argc
> 1 ? CUPS_RASTER_WRITE_COMPRESSED
: CUPS_RASTER_WRITE
;
85 signal(SIGPIPE
, SIG_IGN
);
88 * Run the tests several times to get a good average...
91 printf("Test read/write speed of %d pages, %dx%d pixels...\n\n",
92 TEST_PAGES
, TEST_WIDTH
, TEST_HEIGHT
);
93 for (i
= 0; i
< TEST_PASSES
; i
++)
95 printf("PASS %2d: ", i
+ 1);
98 ras_fd
= run_read_test();
99 start_secs
= get_time();
101 write_test(ras_fd
, mode
);
103 write_secs
= get_time();
104 printf(" %.3f write,", write_secs
- start_secs
);
110 read_secs
= get_time();
111 pass_secs
[i
] = read_secs
- start_secs
;
112 printf(" %.3f read, %.3f total\n", read_secs
- write_secs
, pass_secs
[i
]);
115 printf("\nMedian Total Time: %.3f seconds per document\n",
116 compute_median(pass_secs
));
123 * 'compute_median()' - Compute the median time for a test.
126 static double /* O - Median time in seconds */
127 compute_median(double *secs
) /* I - Array of time samples */
129 int i
, j
; /* Looping vars */
130 double temp
; /* Swap variable */
134 * Sort the array into ascending order using a quicky bubble sort...
137 for (i
= 0; i
< (TEST_PASSES
- 1); i
++)
138 for (j
= i
+ 1; j
< TEST_PASSES
; j
++)
139 if (secs
[i
] > secs
[j
])
147 * Return the average of the middle two samples...
150 return (0.5 * (secs
[TEST_PASSES
/ 2 - 1] + secs
[TEST_PASSES
/ 2]));
155 * 'get_time()' - Get the current time in seconds.
158 static double /* O - Time in seconds */
161 struct timeval curtime
; /* Current time */
164 gettimeofday(&curtime
, NULL
);
165 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
170 * 'read_test()' - Benchmark the raster read functions.
174 read_test(int fd
) /* I - File descriptor to read from */
176 unsigned y
; /* Looping var */
177 cups_raster_t
*r
; /* Raster stream */
178 cups_page_header2_t header
; /* Page header */
179 unsigned char buffer
[8 * TEST_WIDTH
];
187 if ((r
= cupsRasterOpen(fd
, CUPS_RASTER_READ
)) == NULL
)
189 perror("Unable to create raster input stream");
193 while (cupsRasterReadHeader2(r
, &header
))
195 for (y
= 0; y
< header
.cupsHeight
; y
++)
196 cupsRasterReadPixels(r
, buffer
, header
.cupsBytesPerLine
);
204 * 'run_read_test()' - Run the read test as a child process via pipes.
207 static int /* O - Standard input of child */
210 int ras_pipes
[2]; /* Raster data pipes */
211 int pid
; /* Child process ID */
217 if ((pid
= fork()) < 0)
220 * Fork error - return -1 on error...
231 * Child comes here - read data from the input pipe...
235 read_test(ras_pipes
[0]);
241 * Parent comes here - return the output pipe...
245 return (ras_pipes
[1]);
251 * 'write_test()' - Benchmark the raster write functions.
255 write_test(int fd
, /* I - File descriptor to write to */
256 cups_mode_t mode
) /* I - Write mode */
258 unsigned page
, x
, y
; /* Looping vars */
259 unsigned count
; /* Number of bytes to set */
260 cups_raster_t
*r
; /* Raster stream */
261 cups_page_header2_t header
; /* Page header */
262 unsigned char data
[32][8 * TEST_WIDTH
];
263 /* Raster data to write */
267 * Create a combination of random data and repeated data to simulate
268 * text with some whitespace.
271 CUPS_SRAND(time(NULL
));
273 memset(data
, 0, sizeof(data
));
275 for (y
= 0; y
< 28; y
++)
277 for (x
= CUPS_RAND() & 127, count
= (CUPS_RAND() & 15) + 1;
283 x
+= (CUPS_RAND() & 15) + 1;
284 count
= (CUPS_RAND() & 15) + 1;
286 if (x
>= sizeof(data
[0]))
290 data
[y
][x
] = (unsigned char)CUPS_RAND();
295 * Test write speed...
298 if ((r
= cupsRasterOpen(fd
, mode
)) == NULL
)
300 perror("Unable to create raster output stream");
304 for (page
= 0; page
< TEST_PAGES
; page
++)
306 memset(&header
, 0, sizeof(header
));
307 header
.cupsWidth
= TEST_WIDTH
;
308 header
.cupsHeight
= TEST_HEIGHT
;
309 header
.cupsBytesPerLine
= TEST_WIDTH
;
313 header
.cupsBytesPerLine
*= 4;
314 header
.cupsColorSpace
= CUPS_CSPACE_CMYK
;
315 header
.cupsColorOrder
= CUPS_ORDER_CHUNKED
;
319 header
.cupsColorSpace
= CUPS_CSPACE_K
;
320 header
.cupsColorOrder
= CUPS_ORDER_BANDED
;
325 header
.cupsBytesPerLine
*= 2;
326 header
.cupsBitsPerColor
= 16;
327 header
.cupsBitsPerPixel
= (page
& 1) ? 64 : 16;
331 header
.cupsBitsPerColor
= 8;
332 header
.cupsBitsPerPixel
= (page
& 1) ? 32 : 8;
335 cupsRasterWriteHeader2(r
, &header
);
337 for (y
= 0; y
< TEST_HEIGHT
; y
++)
338 cupsRasterWritePixels(r
, data
[y
& 31], header
.cupsBytesPerLine
);