cannam@125
|
1 [+ AutoGen5 template c +]
|
cannam@125
|
2 /*
|
cannam@125
|
3 ** Copyright (C) 2002-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
|
cannam@125
|
4 **
|
cannam@125
|
5 ** This program is free software; you can redistribute it and/or modify
|
cannam@125
|
6 ** it under the terms of the GNU General Public License as published by
|
cannam@125
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
cannam@125
|
8 ** (at your option) any later version.
|
cannam@125
|
9 **
|
cannam@125
|
10 ** This program is distributed in the hope that it will be useful,
|
cannam@125
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@125
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@125
|
13 ** GNU General Public License for more details.
|
cannam@125
|
14 **
|
cannam@125
|
15 ** You should have received a copy of the GNU General Public License
|
cannam@125
|
16 ** along with this program; if not, write to the Free Software
|
cannam@125
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
cannam@125
|
18 */
|
cannam@125
|
19
|
cannam@125
|
20 #include "sfconfig.h"
|
cannam@125
|
21
|
cannam@125
|
22 #include <stdio.h>
|
cannam@125
|
23 #include <stdlib.h>
|
cannam@125
|
24
|
cannam@125
|
25 #if HAVE_UNISTD_H
|
cannam@125
|
26 #include <unistd.h>
|
cannam@125
|
27 #endif
|
cannam@125
|
28
|
cannam@125
|
29 #if (HAVE_DECL_S_IRGRP == 0)
|
cannam@125
|
30 #include <sf_unistd.h>
|
cannam@125
|
31 #endif
|
cannam@125
|
32
|
cannam@125
|
33 #include <string.h>
|
cannam@125
|
34 #include <math.h>
|
cannam@125
|
35 #include <time.h>
|
cannam@125
|
36 #include <fcntl.h>
|
cannam@125
|
37 #include <sys/stat.h>
|
cannam@125
|
38
|
cannam@125
|
39 #include <sndfile.h>
|
cannam@125
|
40
|
cannam@125
|
41 #ifndef M_PI
|
cannam@125
|
42 #define M_PI 3.14159265358979323846264338
|
cannam@125
|
43 #endif
|
cannam@125
|
44
|
cannam@125
|
45 /*
|
cannam@125
|
46 ** Neat solution to the Win32/OS2 binary file flage requirement.
|
cannam@125
|
47 ** If O_BINARY isn't already defined by the inclusion of the system
|
cannam@125
|
48 ** headers, set it to zero.
|
cannam@125
|
49 */
|
cannam@125
|
50 #ifndef O_BINARY
|
cannam@125
|
51 #define O_BINARY 0
|
cannam@125
|
52 #endif
|
cannam@125
|
53
|
cannam@125
|
54 #define WRITE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC | O_BINARY)
|
cannam@125
|
55 #define READ_FLAGS (O_RDONLY | O_BINARY)
|
cannam@125
|
56
|
cannam@125
|
57 #if (defined (WIN32) || defined (_WIN32) || defined (__OS2__))
|
cannam@125
|
58 #define WRITE_PERMS 0777
|
cannam@125
|
59 #else
|
cannam@125
|
60 #define WRITE_PERMS (S_IRUSR | S_IWUSR | S_IRGRP)
|
cannam@125
|
61 #endif
|
cannam@125
|
62
|
cannam@125
|
63 #define BUFFER_SIZE (1 << 18)
|
cannam@125
|
64 #define BLOCK_COUNT (30)
|
cannam@125
|
65 #define TEST_DURATION (5) /* 5 Seconds. */
|
cannam@125
|
66
|
cannam@125
|
67 typedef struct
|
cannam@125
|
68 { double write_rate ;
|
cannam@125
|
69 double read_rate ;
|
cannam@125
|
70 } PERF_STATS ;
|
cannam@125
|
71
|
cannam@125
|
72 static void *data = NULL ;
|
cannam@125
|
73
|
cannam@125
|
74 static void calc_raw_performance (PERF_STATS *stats) ;
|
cannam@125
|
75
|
cannam@125
|
76 [+ FOR data_type
|
cannam@125
|
77 +]static void calc_[+ (get "type_name") +]_performance (int format, double read_rate, double write_rate) ;
|
cannam@125
|
78 [+ ENDFOR data_type
|
cannam@125
|
79 +]
|
cannam@125
|
80
|
cannam@125
|
81 static int cpu_is_big_endian (void) ;
|
cannam@125
|
82
|
cannam@125
|
83 static const char* get_subtype_str (int subtype) ;
|
cannam@125
|
84
|
cannam@125
|
85 int
|
cannam@125
|
86 main (int argc, char *argv [])
|
cannam@125
|
87 { PERF_STATS stats ;
|
cannam@125
|
88 char buffer [256] = "Benchmarking " ;
|
cannam@125
|
89 int format_major ;
|
cannam@125
|
90
|
cannam@125
|
91 if (! (data = malloc (BUFFER_SIZE * sizeof (double))))
|
cannam@125
|
92 { perror ("Error : malloc failed") ;
|
cannam@125
|
93 exit (1) ;
|
cannam@125
|
94 } ;
|
cannam@125
|
95
|
cannam@125
|
96 sf_command (NULL, SFC_GET_LIB_VERSION, buffer + strlen (buffer), sizeof (buffer) - strlen (buffer)) ;
|
cannam@125
|
97
|
cannam@125
|
98 puts (buffer) ;
|
cannam@125
|
99 memset (buffer, '-', strlen (buffer)) ;
|
cannam@125
|
100 puts (buffer) ;
|
cannam@125
|
101 printf ("Each test takes a little over %d seconds.\n\n", TEST_DURATION) ;
|
cannam@125
|
102
|
cannam@125
|
103 calc_raw_performance (&stats) ;
|
cannam@125
|
104
|
cannam@125
|
105 if (argc < 2 || strcmp ("--native-only", argv [1]) == 0)
|
cannam@125
|
106 { puts ("\nNative endian I/O :") ;
|
cannam@125
|
107 format_major = cpu_is_big_endian () ? SF_FORMAT_AIFF : SF_FORMAT_WAV ;
|
cannam@125
|
108
|
cannam@125
|
109 calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
110 calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
111 calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
112 calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
113 calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
114 calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
115 calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
|
cannam@125
|
116 } ;
|
cannam@125
|
117
|
cannam@125
|
118 if (argc < 2 || strcmp ("--swap-only", argv [1]) == 0)
|
cannam@125
|
119 { puts ("\nEndian swapped I/O :") ;
|
cannam@125
|
120 format_major = cpu_is_big_endian () ? SF_FORMAT_WAV : SF_FORMAT_AIFF ;
|
cannam@125
|
121
|
cannam@125
|
122 calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
123 calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
124 calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
125 calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
126 calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
127 calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
|
cannam@125
|
128 calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
|
cannam@125
|
129 } ;
|
cannam@125
|
130
|
cannam@125
|
131 puts ("") ;
|
cannam@125
|
132
|
cannam@125
|
133 free (data) ;
|
cannam@125
|
134
|
cannam@125
|
135 return 0 ;
|
cannam@125
|
136 } /* main */
|
cannam@125
|
137
|
cannam@125
|
138 /*==============================================================================
|
cannam@125
|
139 */
|
cannam@125
|
140
|
cannam@125
|
141 static void
|
cannam@125
|
142 calc_raw_performance (PERF_STATS *stats)
|
cannam@125
|
143 { clock_t start_clock, clock_time ;
|
cannam@125
|
144 int fd, k, byte_count, retval, op_count ;
|
cannam@125
|
145 const char *filename ;
|
cannam@125
|
146
|
cannam@125
|
147 filename = "benchmark.dat" ;
|
cannam@125
|
148
|
cannam@125
|
149 byte_count = BUFFER_SIZE * sizeof (short) ;
|
cannam@125
|
150
|
cannam@125
|
151 /* Collect write stats */
|
cannam@125
|
152 printf (" Raw write PCM_16 : ") ;
|
cannam@125
|
153 fflush (stdout) ;
|
cannam@125
|
154
|
cannam@125
|
155 clock_time = 0 ;
|
cannam@125
|
156 op_count = 0 ;
|
cannam@125
|
157 start_clock = clock () ;
|
cannam@125
|
158
|
cannam@125
|
159 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
cannam@125
|
160 { if ((fd = open (filename, WRITE_FLAGS, WRITE_PERMS)) < 0)
|
cannam@125
|
161 { printf ("Error : not able to open file : %s\n", filename) ;
|
cannam@125
|
162 perror ("") ;
|
cannam@125
|
163 exit (1) ;
|
cannam@125
|
164 } ;
|
cannam@125
|
165
|
cannam@125
|
166 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
cannam@125
|
167 { if ((retval = write (fd, data, byte_count)) != byte_count)
|
cannam@125
|
168 { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
|
cannam@125
|
169 exit (1) ;
|
cannam@125
|
170 } ;
|
cannam@125
|
171 } ;
|
cannam@125
|
172
|
cannam@125
|
173 close (fd) ;
|
cannam@125
|
174
|
cannam@125
|
175 clock_time = clock () - start_clock ;
|
cannam@125
|
176 op_count ++ ;
|
cannam@125
|
177 } ;
|
cannam@125
|
178
|
cannam@125
|
179 stats->write_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
|
cannam@125
|
180 stats->write_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
cannam@125
|
181 printf ("%10.0f samples per sec\n", stats->write_rate) ;
|
cannam@125
|
182
|
cannam@125
|
183 /* Collect read stats */
|
cannam@125
|
184 printf (" Raw read PCM_16 : ") ;
|
cannam@125
|
185 fflush (stdout) ;
|
cannam@125
|
186
|
cannam@125
|
187 clock_time = 0 ;
|
cannam@125
|
188 op_count = 0 ;
|
cannam@125
|
189 start_clock = clock () ;
|
cannam@125
|
190
|
cannam@125
|
191 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
cannam@125
|
192 { if ((fd = open (filename, READ_FLAGS)) < 0)
|
cannam@125
|
193 { printf ("Error : not able to open file : %s\n", filename) ;
|
cannam@125
|
194 perror ("") ;
|
cannam@125
|
195 exit (1) ;
|
cannam@125
|
196 } ;
|
cannam@125
|
197
|
cannam@125
|
198 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
cannam@125
|
199 { if ((retval = read (fd, data, byte_count)) != byte_count)
|
cannam@125
|
200 { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
|
cannam@125
|
201 exit (1) ;
|
cannam@125
|
202 } ;
|
cannam@125
|
203 } ;
|
cannam@125
|
204
|
cannam@125
|
205 close (fd) ;
|
cannam@125
|
206
|
cannam@125
|
207 clock_time = clock () - start_clock ;
|
cannam@125
|
208 op_count ++ ;
|
cannam@125
|
209 } ;
|
cannam@125
|
210
|
cannam@125
|
211 stats->read_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
|
cannam@125
|
212 stats->read_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
cannam@125
|
213 printf ("%10.0f samples per sec\n", stats->read_rate) ;
|
cannam@125
|
214
|
cannam@125
|
215 unlink (filename) ;
|
cannam@125
|
216 } /* calc_raw_performance */
|
cannam@125
|
217
|
cannam@125
|
218 /*------------------------------------------------------------------------------
|
cannam@125
|
219 */
|
cannam@125
|
220
|
cannam@125
|
221 [+ FOR data_type
|
cannam@125
|
222 +]static void
|
cannam@125
|
223 calc_[+ (get "type_name") +]_performance (int format, double read_rate, double write_rate)
|
cannam@125
|
224 { SNDFILE *file ;
|
cannam@125
|
225 SF_INFO sfinfo ;
|
cannam@125
|
226 clock_t start_clock, clock_time ;
|
cannam@125
|
227 double performance ;
|
cannam@125
|
228 int k, item_count, retval, op_count ;
|
cannam@125
|
229 const char* subtype ;
|
cannam@125
|
230 [+ (get "type_name") +] *[+ (get "type_name") +]_data ;
|
cannam@125
|
231 const char *filename ;
|
cannam@125
|
232
|
cannam@125
|
233 filename = "benchmark.dat" ;
|
cannam@125
|
234 subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;
|
cannam@125
|
235
|
cannam@125
|
236 [+ (get "type_name") +]_data = data ;
|
cannam@125
|
237 item_count = BUFFER_SIZE ;
|
cannam@125
|
238 for (k = 0 ; k < item_count ; k++)
|
cannam@125
|
239 [+ (get "type_name") +]_data [k] = [+ (get "multiplier") +] * sin (2 * M_PI * k / 32000.0) ;
|
cannam@125
|
240
|
cannam@125
|
241 /* Collect write stats */
|
cannam@125
|
242 printf (" Write %-5s to %s : ", "[+ (get "type_name") +]", subtype) ;
|
cannam@125
|
243 fflush (stdout) ;
|
cannam@125
|
244
|
cannam@125
|
245 sfinfo.channels = 1 ;
|
cannam@125
|
246 sfinfo.format = format ;
|
cannam@125
|
247 sfinfo.frames = 1 ;
|
cannam@125
|
248 sfinfo.samplerate = 32000 ;
|
cannam@125
|
249
|
cannam@125
|
250 clock_time = 0 ;
|
cannam@125
|
251 op_count = 0 ;
|
cannam@125
|
252 start_clock = clock () ;
|
cannam@125
|
253
|
cannam@125
|
254 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
cannam@125
|
255 { if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
|
cannam@125
|
256 { printf ("Error : not able to open file : %s\n", filename) ;
|
cannam@125
|
257 perror ("") ;
|
cannam@125
|
258 exit (1) ;
|
cannam@125
|
259 } ;
|
cannam@125
|
260
|
cannam@125
|
261 /* Turn off the addition of a PEAK chunk. */
|
cannam@125
|
262 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
|
cannam@125
|
263
|
cannam@125
|
264 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
cannam@125
|
265 { if ((retval = sf_write_[+ (get "type_name") +] (file, [+ (get "type_name") +]_data, item_count)) != item_count)
|
cannam@125
|
266 { printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ;
|
cannam@125
|
267 exit (1) ;
|
cannam@125
|
268 } ;
|
cannam@125
|
269 } ;
|
cannam@125
|
270
|
cannam@125
|
271 sf_close (file) ;
|
cannam@125
|
272
|
cannam@125
|
273 clock_time = clock () - start_clock ;
|
cannam@125
|
274 op_count ++ ;
|
cannam@125
|
275 } ;
|
cannam@125
|
276
|
cannam@125
|
277 performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
|
cannam@125
|
278 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
cannam@125
|
279 printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ;
|
cannam@125
|
280
|
cannam@125
|
281 /* Collect read stats */
|
cannam@125
|
282 printf (" Read %-5s from %s : ", "[+ (get "type_name") +]", subtype) ;
|
cannam@125
|
283 fflush (stdout) ;
|
cannam@125
|
284
|
cannam@125
|
285 clock_time = 0 ;
|
cannam@125
|
286 op_count = 0 ;
|
cannam@125
|
287 start_clock = clock () ;
|
cannam@125
|
288
|
cannam@125
|
289 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
cannam@125
|
290 { if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
|
cannam@125
|
291 { printf ("Error : not able to open file : %s\n", filename) ;
|
cannam@125
|
292 perror ("") ;
|
cannam@125
|
293 exit (1) ;
|
cannam@125
|
294 } ;
|
cannam@125
|
295
|
cannam@125
|
296 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
cannam@125
|
297 { if ((retval = sf_read_[+ (get "type_name") +] (file, [+ (get "type_name") +]_data, item_count)) != item_count)
|
cannam@125
|
298 { printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ;
|
cannam@125
|
299 exit (1) ;
|
cannam@125
|
300 } ;
|
cannam@125
|
301 } ;
|
cannam@125
|
302
|
cannam@125
|
303 sf_close (file) ;
|
cannam@125
|
304
|
cannam@125
|
305 clock_time = clock () - start_clock ;
|
cannam@125
|
306 op_count ++ ;
|
cannam@125
|
307 } ;
|
cannam@125
|
308
|
cannam@125
|
309 performance = (1.0 * item_count) * BLOCK_COUNT * op_count ;
|
cannam@125
|
310 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
cannam@125
|
311 printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ;
|
cannam@125
|
312
|
cannam@125
|
313 unlink (filename) ;
|
cannam@125
|
314
|
cannam@125
|
315 } /* calc_[+ (get "type_name") +]_performance */
|
cannam@125
|
316 [+ ENDFOR data_type
|
cannam@125
|
317 +]
|
cannam@125
|
318
|
cannam@125
|
319 /*==============================================================================
|
cannam@125
|
320 */
|
cannam@125
|
321
|
cannam@125
|
322 static int
|
cannam@125
|
323 cpu_is_big_endian (void)
|
cannam@125
|
324 { unsigned char *cptr ;
|
cannam@125
|
325 int endtest ;
|
cannam@125
|
326
|
cannam@125
|
327 endtest = 0x12345678 ;
|
cannam@125
|
328
|
cannam@125
|
329 cptr = (unsigned char*) (&endtest) ;
|
cannam@125
|
330
|
cannam@125
|
331 if (cptr [0] == 0x12 && cptr [1] == 0x34 && cptr [3] == 0x78)
|
cannam@125
|
332 return SF_TRUE ;
|
cannam@125
|
333
|
cannam@125
|
334 return SF_FALSE ;
|
cannam@125
|
335 } /* cpu_is_big_endian */
|
cannam@125
|
336
|
cannam@125
|
337 static const char*
|
cannam@125
|
338 get_subtype_str (int subtype)
|
cannam@125
|
339 { switch (subtype)
|
cannam@125
|
340 { case SF_FORMAT_PCM_16 :
|
cannam@125
|
341 return "PCM_16" ;
|
cannam@125
|
342
|
cannam@125
|
343 case SF_FORMAT_PCM_24 :
|
cannam@125
|
344 return "PCM_24" ;
|
cannam@125
|
345
|
cannam@125
|
346 case SF_FORMAT_PCM_32 :
|
cannam@125
|
347 return "PCM_32" ;
|
cannam@125
|
348
|
cannam@125
|
349 case SF_FORMAT_FLOAT :
|
cannam@125
|
350 return "FLOAT " ;
|
cannam@125
|
351
|
cannam@125
|
352 case SF_FORMAT_DOUBLE :
|
cannam@125
|
353 return "DOUBLE" ;
|
cannam@125
|
354
|
cannam@125
|
355 default : break ;
|
cannam@125
|
356 } ;
|
cannam@125
|
357
|
cannam@125
|
358 return "UNKNOWN" ;
|
cannam@125
|
359 } /* get_subtype_str */
|
cannam@125
|
360
|