annotate src/libsndfile-1.0.27/tests/benchmark.tpl @ 84:08ae793730bd

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