Update readme, install and license files.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / benchmark.tpl
1 [+ AutoGen5 template c +]
2 /*
3 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 **
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.
9 **
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.
14 **
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.
18 */
19
20 #include "sfconfig.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #if (HAVE_DECL_S_IRGRP == 0)
30 #include <sf_unistd.h>
31 #endif
32
33 #include <string.h>
34 #include <math.h>
35 #include <time.h>
36 #include <fcntl.h>
37 #include <sys/stat.h>
38
39 #include <sndfile.h>
40
41 #ifndef M_PI
42 #define M_PI 3.14159265358979323846264338
43 #endif
44
45 /*
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.
49 */
50 #ifndef O_BINARY
51 #define O_BINARY 0
52 #endif
53
54 #define WRITE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC | O_BINARY)
55 #define READ_FLAGS (O_RDONLY | O_BINARY)
56
57 #if (defined (WIN32) || defined (_WIN32) || defined (__OS2__))
58 #define WRITE_PERMS 0777
59 #else
60 #define WRITE_PERMS (S_IRUSR | S_IWUSR | S_IRGRP)
61 #endif
62
63 #define BUFFER_SIZE (1<<18)
64 #define BLOCK_COUNT (30)
65 #define TEST_DURATION (5) /* 5 Seconds. */
66
67 typedef struct
68 { double write_rate ;
69 double read_rate ;
70 } PERF_STATS ;
71
72 static void *data = NULL ;
73
74 static void calc_raw_performance (PERF_STATS *stats) ;
75
76 [+ FOR data_type
77 +]static void calc_[+ (get "type_name") +]_performance (int format, double read_rate, double write_rate) ;
78 [+ ENDFOR data_type
79 +]
80
81 static int cpu_is_big_endian (void) ;
82
83 static const char* get_subtype_str (int subtype) ;
84
85 int
86 main (int argc, char *argv [])
87 { PERF_STATS stats ;
88 char buffer [256] = "Benchmarking " ;
89 int format_major ;
90
91 if (! (data = malloc (BUFFER_SIZE * sizeof (double))))
92 { perror ("Error : malloc failed") ;
93 exit (1) ;
94 } ;
95
96 sf_command (NULL, SFC_GET_LIB_VERSION, buffer + strlen (buffer), sizeof (buffer) - strlen (buffer)) ;
97
98 puts (buffer) ;
99 memset (buffer, '-', strlen (buffer)) ;
100 puts (buffer) ;
101 printf ("Each test takes a little over %d seconds.\n\n", TEST_DURATION) ;
102
103 calc_raw_performance (&stats) ;
104
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 ;
108
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) ;
116 } ;
117
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 ;
121
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) ;
129 } ;
130
131 puts ("") ;
132
133 free (data) ;
134
135 return 0 ;
136 } /* main */
137
138 /*==============================================================================
139 */
140
141 static void
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 ;
146
147 filename = "benchmark.dat" ;
148
149 byte_count = BUFFER_SIZE * sizeof (short) ;
150
151 /* Collect write stats */
152 printf (" Raw write PCM_16 : ") ;
153 fflush (stdout) ;
154
155 clock_time = 0 ;
156 op_count = 0 ;
157 start_clock = clock () ;
158
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) ;
162 perror ("") ;
163 exit (1) ;
164 } ;
165
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) ;
169 exit (1) ;
170 } ;
171 } ;
172
173 close (fd) ;
174
175 clock_time = clock () - start_clock ;
176 op_count ++ ;
177 } ;
178
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) ;
182
183 /* Collect read stats */
184 printf (" Raw read PCM_16 : ") ;
185 fflush (stdout) ;
186
187 clock_time = 0 ;
188 op_count = 0 ;
189 start_clock = clock () ;
190
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) ;
194 perror ("") ;
195 exit (1) ;
196 } ;
197
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) ;
201 exit (1) ;
202 } ;
203 } ;
204
205 close (fd) ;
206
207 clock_time = clock () - start_clock ;
208 op_count ++ ;
209 } ;
210
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) ;
214
215 unlink (filename) ;
216 } /* calc_raw_performance */
217
218 /*------------------------------------------------------------------------------
219 */
220
221 [+ FOR data_type
222 +]static void
223 calc_[+ (get "type_name") +]_performance (int format, double read_rate, double write_rate)
224 { SNDFILE *file ;
225 SF_INFO sfinfo ;
226 clock_t start_clock, clock_time ;
227 double performance ;
228 int k, item_count, retval, op_count ;
229 const char* subtype ;
230 [+ (get "type_name") +] *[+ (get "type_name") +]_data ;
231 const char *filename ;
232
233 filename = "benchmark.dat" ;
234 subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;
235
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) ;
240
241 /* Collect write stats */
242 printf (" Write %-5s to %s : ", "[+ (get "type_name") +]", subtype) ;
243 fflush (stdout) ;
244
245 sfinfo.channels = 1 ;
246 sfinfo.format = format ;
247 sfinfo.frames = 1 ;
248 sfinfo.samplerate = 32000 ;
249
250 clock_time = 0 ;
251 op_count = 0 ;
252 start_clock = clock () ;
253
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) ;
257 perror ("") ;
258 exit (1) ;
259 } ;
260
261 /* Turn off the addition of a PEAK chunk. */
262 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
263
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) ;
267 exit (1) ;
268 } ;
269 } ;
270
271 sf_close (file) ;
272
273 clock_time = clock () - start_clock ;
274 op_count ++ ;
275 } ;
276
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) ;
280
281 /* Collect read stats */
282 printf (" Read %-5s from %s : ", "[+ (get "type_name") +]", subtype) ;
283 fflush (stdout) ;
284
285 clock_time = 0 ;
286 op_count = 0 ;
287 start_clock = clock () ;
288
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) ;
292 perror ("") ;
293 exit (1) ;
294 } ;
295
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) ;
299 exit (1) ;
300 } ;
301 } ;
302
303 sf_close (file) ;
304
305 clock_time = clock () - start_clock ;
306 op_count ++ ;
307 } ;
308
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) ;
312
313 unlink (filename) ;
314
315 } /* calc_[+ (get "type_name") +]_performance */
316 [+ ENDFOR data_type
317 +]
318
319 /*==============================================================================
320 */
321
322 static int
323 cpu_is_big_endian (void)
324 { unsigned char *cptr ;
325 int endtest ;
326
327 endtest = 0x12345678 ;
328
329 cptr = (unsigned char*) (&endtest) ;
330
331 if (cptr [0] == 0x12 && cptr [1] == 0x34 && cptr [3] == 0x78)
332 return SF_TRUE ;
333
334 return SF_FALSE ;
335 } /* cpu_is_big_endian */
336
337 static const char*
338 get_subtype_str (int subtype)
339 { switch (subtype)
340 { case SF_FORMAT_PCM_16 :
341 return "PCM_16" ;
342
343 case SF_FORMAT_PCM_24 :
344 return "PCM_24" ;
345
346 case SF_FORMAT_PCM_32 :
347 return "PCM_32" ;
348
349 case SF_FORMAT_FLOAT :
350 return "FLOAT " ;
351
352 case SF_FORMAT_DOUBLE :
353 return "DOUBLE" ;
354
355 default : break ;
356 } ;
357
358 return "UNKNOWN" ;
359 } /* get_subtype_str */
360