annotate src/libsndfile-1.0.25/tests/benchmark.tpl @ 36:55ece8862b6d

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