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 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...
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
);
59 * 'main()' - Benchmark the raster read/write functions.
62 int /* O - Exit status */
65 int i
; /* Looping var */
66 int ras_fd
, /* File descriptor for read process */
67 status
; /* Exit status of read process */
68 double start_secs
, /* Start time */
69 write_secs
, /* Write time */
70 read_secs
, /* Read time */
71 pass_secs
[TEST_PASSES
]; /* Total test times */
78 signal(SIGPIPE
, SIG_IGN
);
81 * Run the tests several times to get a good average...
84 printf("Test read/write speed of %d pages, %dx%d pixels...\n\n",
85 TEST_PAGES
, TEST_WIDTH
, TEST_HEIGHT
);
86 for (i
= 0; i
< TEST_PASSES
; i
++)
88 printf("PASS %2d: ", i
+ 1);
91 ras_fd
= run_read_test();
92 start_secs
= get_time();
96 write_secs
= get_time();
97 printf(" %.3f write,", write_secs
- start_secs
);
103 read_secs
= get_time();
104 pass_secs
[i
] = read_secs
- start_secs
;
105 printf(" %.3f read, %.3f total\n", read_secs
- write_secs
, pass_secs
[i
]);
108 printf("\nMedian Total Time: %.3f seconds per document\n",
109 compute_median(pass_secs
));
116 * 'compute_median()' - Compute the median time for a test.
119 static double /* O - Median time in seconds */
120 compute_median(double *secs
) /* I - Array of time samples */
122 int i
, j
; /* Looping vars */
123 double temp
; /* Swap variable */
127 * Sort the array into ascending order using a quicky bubble sort...
130 for (i
= 0; i
< (TEST_PASSES
- 1); i
++)
131 for (j
= i
+ 1; j
< TEST_PASSES
; j
++)
132 if (secs
[i
] > secs
[j
])
140 * Return the average of the middle two samples...
143 return (0.5 * (secs
[TEST_PASSES
/ 2 - 1] + secs
[TEST_PASSES
/ 2]));
148 * 'get_time()' - Get the current time in seconds.
151 static double /* O - Time in seconds */
154 struct timeval curtime
; /* Current time */
157 gettimeofday(&curtime
, NULL
);
158 return (curtime
.tv_sec
+ 0.000001 * curtime
.tv_usec
);
163 * 'read_test()' - Benchmark the raster read functions.
167 read_test(int fd
) /* I - File descriptor to read from */
169 int y
; /* Looping var */
170 cups_raster_t
*r
; /* Raster stream */
171 cups_page_header_t header
; /* Page header */
172 unsigned char buffer
[8 * TEST_WIDTH
];
180 if ((r
= cupsRasterOpen(fd
, CUPS_RASTER_READ
)) == NULL
)
182 perror("Unable to create raster input stream");
186 while (cupsRasterReadHeader(r
, &header
))
188 for (y
= 0; y
< header
.cupsHeight
; y
++)
189 cupsRasterReadPixels(r
, buffer
, header
.cupsBytesPerLine
);
197 * 'run_read_test()' - Run the read test as a child process via pipes.
200 static int /* O - Standard input of child */
203 int ras_pipes
[2]; /* Raster data pipes */
204 int pid
; /* Child process ID */
210 if ((pid
= fork()) < 0)
213 * Fork error - return -1 on error...
224 * Child comes here - read data from the input pipe...
228 read_test(ras_pipes
[0]);
234 * Parent comes here - return the output pipe...
238 return (ras_pipes
[1]);
244 * 'write_test()' - Benchmark the raster write functions.
248 write_test(int fd
) /* I - File descriptor to write to */
250 int page
, x
, y
; /* Looping vars */
251 int count
; /* Number of bytes to set */
252 cups_raster_t
*r
; /* Raster stream */
253 cups_page_header_t header
; /* Page header */
254 unsigned char data
[32][8 * TEST_WIDTH
];
255 /* Raster data to write */
259 * Create a combination of random data and repeated data to simulate
260 * text with some whitespace.
265 memset(data
, 0, sizeof(data
));
267 for (y
= 0; y
< 28; y
++)
269 for (x
= rand() & 127, count
= (rand() & 15) + 1;
275 x
+= (rand() & 15) + 1;
276 count
= (rand() & 15) + 1;
278 if (x
>= sizeof(data
[0]))
287 * Test write speed...
290 if ((r
= cupsRasterOpen(fd
, CUPS_RASTER_WRITE
)) == NULL
)
292 perror("Unable to create raster output stream");
296 for (page
= 0; page
< TEST_PAGES
; page
++)
298 memset(&header
, 0, sizeof(header
));
299 header
.cupsWidth
= TEST_WIDTH
;
300 header
.cupsHeight
= TEST_HEIGHT
;
301 header
.cupsBytesPerLine
= TEST_WIDTH
;
305 header
.cupsBytesPerLine
*= 4;
306 header
.cupsColorSpace
= CUPS_CSPACE_CMYK
;
307 header
.cupsColorOrder
= CUPS_ORDER_CHUNKED
;
311 header
.cupsColorSpace
= CUPS_CSPACE_K
;
312 header
.cupsColorOrder
= CUPS_ORDER_BANDED
;
317 header
.cupsBytesPerLine
*= 2;
318 header
.cupsBitsPerColor
= 16;
319 header
.cupsBitsPerPixel
= (page
& 1) ? 64 : 16;
323 header
.cupsBitsPerColor
= 8;
324 header
.cupsBitsPerPixel
= (page
& 1) ? 32 : 8;
327 cupsRasterWriteHeader(r
, &header
);
329 for (y
= 0; y
< TEST_HEIGHT
; y
++)
330 cupsRasterWritePixels(r
, data
[y
& 31], header
.cupsBytesPerLine
);
338 * End of "$Id: rasterbench.c 6649 2007-07-11 21:46:42Z mike $".