4 * Raster benchmark program for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
23 #include <cups/raster.h>
35 #define TEST_WIDTH 1024
36 #define TEST_HEIGHT 1024
38 #define TEST_PASSES 20
45 static double compute_median(double *secs
);
46 static double get_time(void);
47 static void read_test(int fd
);
48 static int run_read_test(void);
49 static void write_test(int fd
, cups_mode_t mode
);
53 * 'main()' - Benchmark the raster read/write functions.
56 int /* O - Exit status */
57 main(int argc
, /* I - Number of command-line args */
58 char *argv
[]) /* I - Command-line arguments */
60 int i
; /* Looping var */
61 int ras_fd
, /* File descriptor for read process */
62 status
; /* Exit status of read process */
63 double start_secs
, /* Start time */
64 write_secs
, /* Write time */
65 read_secs
, /* Read time */
66 pass_secs
[TEST_PASSES
]; /* Total test times */
67 cups_mode_t mode
; /* Write mode */
71 * See if we have anything on the command-line...
74 if (argc
> 2 || (argc
== 2 && strcmp(argv
[1], "-z")))
76 puts("Usage: rasterbench [-z]");
80 mode
= argc
> 1 ? CUPS_RASTER_WRITE_COMPRESSED
: CUPS_RASTER_WRITE
;
86 signal(SIGPIPE
, SIG_IGN
);
89 * Run the tests several times to get a good average...
92 printf("Test read/write speed of %d pages, %dx%d pixels...\n\n",
93 TEST_PAGES
, TEST_WIDTH
, TEST_HEIGHT
);
94 for (i
= 0; i
< TEST_PASSES
; i
++)
96 printf("PASS %2d: ", i
+ 1);
99 ras_fd
= run_read_test();
100 start_secs
= get_time();
102 write_test(ras_fd
, mode
);
104 write_secs
= get_time();
105 printf(" %.3f write,", write_secs
- start_secs
);
111 read_secs
= get_time();
112 pass_secs
[i
] = read_secs
- start_secs
;
113 printf(" %.3f read, %.3f total\n", read_secs
- write_secs
, pass_secs
[i
]);
116 printf("\nMedian Total Time: %.3f seconds per document\n",
117 compute_median(pass_secs
));
124 * 'compute_median()' - Compute the median time for a test.
127 static double /* O - Median time in seconds */
128 compute_median(double *secs
) /* I - Array of time samples */
130 int i
, j
; /* Looping vars */
131 double temp
; /* Swap variable */
135 * Sort the array into ascending order using a quicky bubble sort...
138 for (i
= 0; i
< (TEST_PASSES
- 1); i
++)
139 for (j
= i
+ 1; j
< TEST_PASSES
; j
++)
140 if (secs
[i
] > secs
[j
])
148 * Return the average of the middle two samples...
151 return (0.5 * (secs
[TEST_PASSES
/ 2 - 1] + secs
[TEST_PASSES
/ 2]));
156 * 'get_time()' - Get the current time in seconds.
159 static double /* O - Time in seconds */
162 struct timeval curtime
; /* Current time */
165 gettimeofday(&curtime
, NULL
);
166 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
171 * 'read_test()' - Benchmark the raster read functions.
175 read_test(int fd
) /* I - File descriptor to read from */
177 unsigned y
; /* Looping var */
178 cups_raster_t
*r
; /* Raster stream */
179 cups_page_header2_t header
; /* Page header */
180 unsigned char buffer
[8 * TEST_WIDTH
];
188 if ((r
= cupsRasterOpen(fd
, CUPS_RASTER_READ
)) == NULL
)
190 perror("Unable to create raster input stream");
194 while (cupsRasterReadHeader2(r
, &header
))
196 for (y
= 0; y
< header
.cupsHeight
; y
++)
197 cupsRasterReadPixels(r
, buffer
, header
.cupsBytesPerLine
);
205 * 'run_read_test()' - Run the read test as a child process via pipes.
208 static int /* O - Standard input of child */
211 int ras_pipes
[2]; /* Raster data pipes */
212 int pid
; /* Child process ID */
218 if ((pid
= fork()) < 0)
221 * Fork error - return -1 on error...
232 * Child comes here - read data from the input pipe...
236 read_test(ras_pipes
[0]);
242 * Parent comes here - return the output pipe...
246 return (ras_pipes
[1]);
252 * 'write_test()' - Benchmark the raster write functions.
256 write_test(int fd
, /* I - File descriptor to write to */
257 cups_mode_t mode
) /* I - Write mode */
259 unsigned page
, x
, y
; /* Looping vars */
260 unsigned count
; /* Number of bytes to set */
261 cups_raster_t
*r
; /* Raster stream */
262 cups_page_header2_t header
; /* Page header */
263 unsigned char data
[32][8 * TEST_WIDTH
];
264 /* Raster data to write */
268 * Create a combination of random data and repeated data to simulate
269 * text with some whitespace.
272 CUPS_SRAND(time(NULL
));
274 memset(data
, 0, sizeof(data
));
276 for (y
= 0; y
< 28; y
++)
278 for (x
= CUPS_RAND() & 127, count
= (CUPS_RAND() & 15) + 1;
284 x
+= (CUPS_RAND() & 15) + 1;
285 count
= (CUPS_RAND() & 15) + 1;
287 if (x
>= sizeof(data
[0]))
291 data
[y
][x
] = (unsigned char)CUPS_RAND();
296 * Test write speed...
299 if ((r
= cupsRasterOpen(fd
, mode
)) == NULL
)
301 perror("Unable to create raster output stream");
305 for (page
= 0; page
< TEST_PAGES
; page
++)
307 memset(&header
, 0, sizeof(header
));
308 header
.cupsWidth
= TEST_WIDTH
;
309 header
.cupsHeight
= TEST_HEIGHT
;
310 header
.cupsBytesPerLine
= TEST_WIDTH
;
314 header
.cupsBytesPerLine
*= 4;
315 header
.cupsColorSpace
= CUPS_CSPACE_CMYK
;
316 header
.cupsColorOrder
= CUPS_ORDER_CHUNKED
;
320 header
.cupsColorSpace
= CUPS_CSPACE_K
;
321 header
.cupsColorOrder
= CUPS_ORDER_BANDED
;
326 header
.cupsBytesPerLine
*= 2;
327 header
.cupsBitsPerColor
= 16;
328 header
.cupsBitsPerPixel
= (page
& 1) ? 64 : 16;
332 header
.cupsBitsPerColor
= 8;
333 header
.cupsBitsPerPixel
= (page
& 1) ? 32 : 8;
336 cupsRasterWriteHeader2(r
, &header
);
338 for (y
= 0; y
< TEST_HEIGHT
; y
++)
339 cupsRasterWritePixels(r
, data
[y
& 31], header
.cupsBytesPerLine
);