1 [+ AutoGen5 template c +]
3 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #if (HAVE_DECL_S_IRGRP == 0)
30 #include <sf_unistd.h>
42 #define M_PI 3.14159265358979323846264338
46 ** Neat solution to the Win32/OS2 binary file flage requirement.
47 ** If O_BINARY isn't already defined by the inclusion of the system
48 ** headers, set it to zero.
54 #define WRITE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC | O_BINARY)
55 #define READ_FLAGS (O_RDONLY | O_BINARY)
57 #if (defined (WIN32) || defined (_WIN32) || defined (__OS2__))
58 #define WRITE_PERMS 0777
60 #define WRITE_PERMS (S_IRUSR | S_IWUSR | S_IRGRP)
63 #define BUFFER_SIZE (1<<18)
64 #define BLOCK_COUNT (30)
65 #define TEST_DURATION (5) /* 5 Seconds. */
72 static void *data = NULL ;
74 static void calc_raw_performance (PERF_STATS *stats) ;
77 +]static void calc_[+ (get "type_name") +]_performance (int format, double read_rate, double write_rate) ;
81 static int cpu_is_big_endian (void) ;
83 static const char* get_subtype_str (int subtype) ;
86 main (int argc, char *argv [])
88 char buffer [256] = "Benchmarking " ;
91 if (! (data = malloc (BUFFER_SIZE * sizeof (double))))
92 { perror ("Error : malloc failed") ;
96 sf_command (NULL, SFC_GET_LIB_VERSION, buffer + strlen (buffer), sizeof (buffer) - strlen (buffer)) ;
99 memset (buffer, '-', strlen (buffer)) ;
101 printf ("Each test takes a little over %d seconds.\n\n", TEST_DURATION) ;
103 calc_raw_performance (&stats) ;
105 if (argc < 2 || strcmp ("--native-only", argv [1]) == 0)
106 { puts ("\nNative endian I/O :") ;
107 format_major = cpu_is_big_endian () ? SF_FORMAT_AIFF : SF_FORMAT_WAV ;
109 calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
110 calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
111 calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
112 calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
113 calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
114 calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
115 calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
118 if (argc < 2 || strcmp ("--swap-only", argv [1]) == 0)
119 { puts ("\nEndian swapped I/O :") ;
120 format_major = cpu_is_big_endian () ? SF_FORMAT_WAV : SF_FORMAT_AIFF ;
122 calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
123 calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
124 calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
125 calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
126 calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
127 calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
128 calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
138 /*==============================================================================
142 calc_raw_performance (PERF_STATS *stats)
143 { clock_t start_clock, clock_time ;
144 int fd, k, byte_count, retval, op_count ;
145 const char *filename ;
147 filename = "benchmark.dat" ;
149 byte_count = BUFFER_SIZE * sizeof (short) ;
151 /* Collect write stats */
152 printf (" Raw write PCM_16 : ") ;
157 start_clock = clock () ;
159 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
160 { if ((fd = open (filename, WRITE_FLAGS, WRITE_PERMS)) < 0)
161 { printf ("Error : not able to open file : %s\n", filename) ;
166 for (k = 0 ; k < BLOCK_COUNT ; k++)
167 { if ((retval = write (fd, data, byte_count)) != byte_count)
168 { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
175 clock_time = clock () - start_clock ;
179 stats->write_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
180 stats->write_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
181 printf ("%10.0f samples per sec\n", stats->write_rate) ;
183 /* Collect read stats */
184 printf (" Raw read PCM_16 : ") ;
189 start_clock = clock () ;
191 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
192 { if ((fd = open (filename, READ_FLAGS)) < 0)
193 { printf ("Error : not able to open file : %s\n", filename) ;
198 for (k = 0 ; k < BLOCK_COUNT ; k++)
199 { if ((retval = read (fd, data, byte_count)) != byte_count)
200 { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
207 clock_time = clock () - start_clock ;
211 stats->read_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
212 stats->read_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
213 printf ("%10.0f samples per sec\n", stats->read_rate) ;
216 } /* calc_raw_performance */
218 /*------------------------------------------------------------------------------
223 calc_[+ (get "type_name") +]_performance (int format, double read_rate, double write_rate)
226 clock_t start_clock, clock_time ;
228 int k, item_count, retval, op_count ;
229 const char* subtype ;
230 [+ (get "type_name") +] *[+ (get "type_name") +]_data ;
231 const char *filename ;
233 filename = "benchmark.dat" ;
234 subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;
236 [+ (get "type_name") +]_data = data ;
237 item_count = BUFFER_SIZE ;
238 for (k = 0 ; k < item_count ; k++)
239 [+ (get "type_name") +]_data [k] = [+ (get "multiplier") +] * sin (2 * M_PI * k / 32000.0) ;
241 /* Collect write stats */
242 printf (" Write %-5s to %s : ", "[+ (get "type_name") +]", subtype) ;
245 sfinfo.channels = 1 ;
246 sfinfo.format = format ;
248 sfinfo.samplerate = 32000 ;
252 start_clock = clock () ;
254 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
255 { if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
256 { printf ("Error : not able to open file : %s\n", filename) ;
261 /* Turn off the addition of a PEAK chunk. */
262 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
264 for (k = 0 ; k < BLOCK_COUNT ; k++)
265 { if ((retval = sf_write_[+ (get "type_name") +] (file, [+ (get "type_name") +]_data, item_count)) != item_count)
266 { printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ;
273 clock_time = clock () - start_clock ;
277 performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
278 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
279 printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ;
281 /* Collect read stats */
282 printf (" Read %-5s from %s : ", "[+ (get "type_name") +]", subtype) ;
287 start_clock = clock () ;
289 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
290 { if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
291 { printf ("Error : not able to open file : %s\n", filename) ;
296 for (k = 0 ; k < BLOCK_COUNT ; k++)
297 { if ((retval = sf_read_[+ (get "type_name") +] (file, [+ (get "type_name") +]_data, item_count)) != item_count)
298 { printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ;
305 clock_time = clock () - start_clock ;
309 performance = (1.0 * item_count) * BLOCK_COUNT * op_count ;
310 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
311 printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ;
315 } /* calc_[+ (get "type_name") +]_performance */
319 /*==============================================================================
323 cpu_is_big_endian (void)
324 { unsigned char *cptr ;
327 endtest = 0x12345678 ;
329 cptr = (unsigned char*) (&endtest) ;
331 if (cptr [0] == 0x12 && cptr [1] == 0x34 && cptr [3] == 0x78)
335 } /* cpu_is_big_endian */
338 get_subtype_str (int subtype)
340 { case SF_FORMAT_PCM_16 :
343 case SF_FORMAT_PCM_24 :
346 case SF_FORMAT_PCM_32 :
349 case SF_FORMAT_FLOAT :
352 case SF_FORMAT_DOUBLE :
359 } /* get_subtype_str */