benchmark.tpl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. [+ AutoGen5 template c +]
  2. /*
  3. ** Copyright (C) 2002-2012 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. #include "sfconfig.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #if (HAVE_DECL_S_IRGRP == 0)
  26. #include <sf_unistd.h>
  27. #endif
  28. #include <string.h>
  29. #include <math.h>
  30. #include <time.h>
  31. #include <fcntl.h>
  32. #include <sys/stat.h>
  33. #include <sndfile.h>
  34. #ifndef M_PI
  35. #define M_PI 3.14159265358979323846264338
  36. #endif
  37. /*
  38. ** Neat solution to the Win32/OS2 binary file flage requirement.
  39. ** If O_BINARY isn't already defined by the inclusion of the system
  40. ** headers, set it to zero.
  41. */
  42. #ifndef O_BINARY
  43. #define O_BINARY 0
  44. #endif
  45. #define WRITE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC | O_BINARY)
  46. #define READ_FLAGS (O_RDONLY | O_BINARY)
  47. #if (defined (WIN32) || defined (_WIN32) || defined (__OS2__))
  48. #define WRITE_PERMS 0777
  49. #else
  50. #define WRITE_PERMS (S_IRUSR | S_IWUSR | S_IRGRP)
  51. #endif
  52. #define BUFFER_SIZE (1 << 18)
  53. #define BLOCK_COUNT (30)
  54. #define TEST_DURATION (5) /* 5 Seconds. */
  55. typedef struct
  56. { double write_rate ;
  57. double read_rate ;
  58. } PERF_STATS ;
  59. static void *data = NULL ;
  60. static void calc_raw_performance (PERF_STATS *stats) ;
  61. [+ FOR data_type
  62. +]static void calc_[+ (get "type_name") +]_performance (int format, double read_rate, double write_rate) ;
  63. [+ ENDFOR data_type
  64. +]
  65. static int cpu_is_big_endian (void) ;
  66. static const char* get_subtype_str (int subtype) ;
  67. int
  68. main (int argc, char *argv [])
  69. { PERF_STATS stats ;
  70. char buffer [256] = "Benchmarking " ;
  71. int format_major ;
  72. if (! (data = malloc (BUFFER_SIZE * sizeof (double))))
  73. { perror ("Error : malloc failed") ;
  74. exit (1) ;
  75. } ;
  76. sf_command (NULL, SFC_GET_LIB_VERSION, buffer + strlen (buffer), sizeof (buffer) - strlen (buffer)) ;
  77. puts (buffer) ;
  78. memset (buffer, '-', strlen (buffer)) ;
  79. puts (buffer) ;
  80. printf ("Each test takes a little over %d seconds.\n\n", TEST_DURATION) ;
  81. calc_raw_performance (&stats) ;
  82. if (argc < 2 || strcmp ("--native-only", argv [1]) == 0)
  83. { puts ("\nNative endian I/O :") ;
  84. format_major = cpu_is_big_endian () ? SF_FORMAT_AIFF : SF_FORMAT_WAV ;
  85. calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
  86. calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
  87. calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
  88. calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
  89. calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
  90. calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
  91. calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
  92. } ;
  93. if (argc < 2 || strcmp ("--swap-only", argv [1]) == 0)
  94. { puts ("\nEndian swapped I/O :") ;
  95. format_major = cpu_is_big_endian () ? SF_FORMAT_WAV : SF_FORMAT_AIFF ;
  96. calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
  97. calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
  98. calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
  99. calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
  100. calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
  101. calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
  102. calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
  103. } ;
  104. puts ("") ;
  105. free (data) ;
  106. return 0 ;
  107. } /* main */
  108. /*==============================================================================
  109. */
  110. static void
  111. calc_raw_performance (PERF_STATS *stats)
  112. { clock_t start_clock, clock_time ;
  113. int fd, k, byte_count, retval, op_count ;
  114. const char *filename ;
  115. filename = "benchmark.dat" ;
  116. byte_count = BUFFER_SIZE * sizeof (short) ;
  117. /* Collect write stats */
  118. printf (" Raw write PCM_16 : ") ;
  119. fflush (stdout) ;
  120. clock_time = 0 ;
  121. op_count = 0 ;
  122. start_clock = clock () ;
  123. while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
  124. { if ((fd = open (filename, WRITE_FLAGS, WRITE_PERMS)) < 0)
  125. { printf ("Error : not able to open file : %s\n", filename) ;
  126. perror ("") ;
  127. exit (1) ;
  128. } ;
  129. for (k = 0 ; k < BLOCK_COUNT ; k++)
  130. { if ((retval = write (fd, data, byte_count)) != byte_count)
  131. { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
  132. exit (1) ;
  133. } ;
  134. } ;
  135. close (fd) ;
  136. clock_time = clock () - start_clock ;
  137. op_count ++ ;
  138. } ;
  139. stats->write_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
  140. stats->write_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
  141. printf ("%10.0f samples per sec\n", stats->write_rate) ;
  142. /* Collect read stats */
  143. printf (" Raw read PCM_16 : ") ;
  144. fflush (stdout) ;
  145. clock_time = 0 ;
  146. op_count = 0 ;
  147. start_clock = clock () ;
  148. while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
  149. { if ((fd = open (filename, READ_FLAGS)) < 0)
  150. { printf ("Error : not able to open file : %s\n", filename) ;
  151. perror ("") ;
  152. exit (1) ;
  153. } ;
  154. for (k = 0 ; k < BLOCK_COUNT ; k++)
  155. { if ((retval = read (fd, data, byte_count)) != byte_count)
  156. { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
  157. exit (1) ;
  158. } ;
  159. } ;
  160. close (fd) ;
  161. clock_time = clock () - start_clock ;
  162. op_count ++ ;
  163. } ;
  164. stats->read_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
  165. stats->read_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
  166. printf ("%10.0f samples per sec\n", stats->read_rate) ;
  167. unlink (filename) ;
  168. } /* calc_raw_performance */
  169. /*------------------------------------------------------------------------------
  170. */
  171. [+ FOR data_type
  172. +]static void
  173. calc_[+ (get "type_name") +]_performance (int format, double read_rate, double write_rate)
  174. { SNDFILE *file ;
  175. SF_INFO sfinfo ;
  176. clock_t start_clock, clock_time ;
  177. double performance ;
  178. int k, item_count, retval, op_count ;
  179. const char* subtype ;
  180. [+ (get "type_name") +] *[+ (get "type_name") +]_data ;
  181. const char *filename ;
  182. filename = "benchmark.dat" ;
  183. subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;
  184. [+ (get "type_name") +]_data = data ;
  185. item_count = BUFFER_SIZE ;
  186. for (k = 0 ; k < item_count ; k++)
  187. [+ (get "type_name") +]_data [k] = [+ (get "multiplier") +] * sin (2 * M_PI * k / 32000.0) ;
  188. /* Collect write stats */
  189. printf (" Write %-5s to %s : ", "[+ (get "type_name") +]", subtype) ;
  190. fflush (stdout) ;
  191. sfinfo.channels = 1 ;
  192. sfinfo.format = format ;
  193. sfinfo.frames = 1 ;
  194. sfinfo.samplerate = 32000 ;
  195. clock_time = 0 ;
  196. op_count = 0 ;
  197. start_clock = clock () ;
  198. while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
  199. { if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
  200. { printf ("Error : not able to open file : %s\n", filename) ;
  201. perror ("") ;
  202. exit (1) ;
  203. } ;
  204. /* Turn off the addition of a PEAK chunk. */
  205. sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
  206. for (k = 0 ; k < BLOCK_COUNT ; k++)
  207. { if ((retval = sf_write_[+ (get "type_name") +] (file, [+ (get "type_name") +]_data, item_count)) != item_count)
  208. { printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ;
  209. exit (1) ;
  210. } ;
  211. } ;
  212. sf_close (file) ;
  213. clock_time = clock () - start_clock ;
  214. op_count ++ ;
  215. } ;
  216. performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
  217. performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
  218. printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ;
  219. /* Collect read stats */
  220. printf (" Read %-5s from %s : ", "[+ (get "type_name") +]", subtype) ;
  221. fflush (stdout) ;
  222. clock_time = 0 ;
  223. op_count = 0 ;
  224. start_clock = clock () ;
  225. while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
  226. { if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
  227. { printf ("Error : not able to open file : %s\n", filename) ;
  228. perror ("") ;
  229. exit (1) ;
  230. } ;
  231. for (k = 0 ; k < BLOCK_COUNT ; k++)
  232. { if ((retval = sf_read_[+ (get "type_name") +] (file, [+ (get "type_name") +]_data, item_count)) != item_count)
  233. { printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ;
  234. exit (1) ;
  235. } ;
  236. } ;
  237. sf_close (file) ;
  238. clock_time = clock () - start_clock ;
  239. op_count ++ ;
  240. } ;
  241. performance = (1.0 * item_count) * BLOCK_COUNT * op_count ;
  242. performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
  243. printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ;
  244. unlink (filename) ;
  245. } /* calc_[+ (get "type_name") +]_performance */
  246. [+ ENDFOR data_type
  247. +]
  248. /*==============================================================================
  249. */
  250. static int
  251. cpu_is_big_endian (void)
  252. { unsigned char *cptr ;
  253. int endtest ;
  254. endtest = 0x12345678 ;
  255. cptr = (unsigned char*) (&endtest) ;
  256. if (cptr [0] == 0x12 && cptr [1] == 0x34 && cptr [3] == 0x78)
  257. return SF_TRUE ;
  258. return SF_FALSE ;
  259. } /* cpu_is_big_endian */
  260. static const char*
  261. get_subtype_str (int subtype)
  262. { switch (subtype)
  263. { case SF_FORMAT_PCM_16 :
  264. return "PCM_16" ;
  265. case SF_FORMAT_PCM_24 :
  266. return "PCM_24" ;
  267. case SF_FORMAT_PCM_32 :
  268. return "PCM_32" ;
  269. case SF_FORMAT_FLOAT :
  270. return "FLOAT " ;
  271. case SF_FORMAT_DOUBLE :
  272. return "DOUBLE" ;
  273. default : break ;
  274. } ;
  275. return "UNKNOWN" ;
  276. } /* get_subtype_str */