annotate src/libsamplerate-0.1.8/tests/multichan_throughput_test.c @ 85:545efbb81310

Import initial set of sources
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 18 Mar 2013 14:12:14 +0000
parents
children
rev   line source
cannam@85 1 /*
cannam@85 2 ** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@85 3 **
cannam@85 4 ** This program is free software; you can redistribute it and/or modify
cannam@85 5 ** it under the terms of the GNU General Public License as published by
cannam@85 6 ** the Free Software Foundation; either version 2 of the License, or
cannam@85 7 ** (at your option) any later version.
cannam@85 8 **
cannam@85 9 ** This program is distributed in the hope that it will be useful,
cannam@85 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@85 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@85 12 ** GNU General Public License for more details.
cannam@85 13 **
cannam@85 14 ** You should have received a copy of the GNU General Public License
cannam@85 15 ** along with this program; if not, write to the Free Software
cannam@85 16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
cannam@85 17 */
cannam@85 18
cannam@85 19 #include <stdio.h>
cannam@85 20 #include <stdlib.h>
cannam@85 21 #include <string.h>
cannam@85 22 #include <time.h>
cannam@85 23 #include <unistd.h>
cannam@85 24
cannam@85 25 #include <samplerate.h>
cannam@85 26
cannam@85 27 #include "config.h"
cannam@85 28
cannam@85 29 #include "util.h"
cannam@85 30 #include "float_cast.h"
cannam@85 31
cannam@85 32 #define BUFFER_LEN (1<<17)
cannam@85 33
cannam@85 34 static float input [BUFFER_LEN] ;
cannam@85 35 static float output [BUFFER_LEN] ;
cannam@85 36
cannam@85 37 static long
cannam@85 38 throughput_test (int converter, int channels, long best_throughput)
cannam@85 39 { SRC_DATA src_data ;
cannam@85 40 clock_t start_time, clock_time ;
cannam@85 41 double duration ;
cannam@85 42 long total_frames = 0, throughput ;
cannam@85 43 int error ;
cannam@85 44
cannam@85 45 printf (" %-30s %2d ", src_get_name (converter), channels) ;
cannam@85 46 fflush (stdout) ;
cannam@85 47
cannam@85 48 src_data.data_in = input ;
cannam@85 49 src_data.input_frames = ARRAY_LEN (input) / channels ;
cannam@85 50
cannam@85 51 src_data.data_out = output ;
cannam@85 52 src_data.output_frames = ARRAY_LEN (output) / channels ;
cannam@85 53
cannam@85 54 src_data.src_ratio = 0.99 ;
cannam@85 55
cannam@85 56 sleep (2) ;
cannam@85 57
cannam@85 58 start_time = clock () ;
cannam@85 59
cannam@85 60 do
cannam@85 61 {
cannam@85 62 if ((error = src_simple (&src_data, converter, channels)) != 0)
cannam@85 63 { puts (src_strerror (error)) ;
cannam@85 64 exit (1) ;
cannam@85 65 } ;
cannam@85 66
cannam@85 67 total_frames += src_data.output_frames_gen ;
cannam@85 68
cannam@85 69 clock_time = clock () - start_time ;
cannam@85 70 duration = (1.0 * clock_time) / CLOCKS_PER_SEC ;
cannam@85 71 }
cannam@85 72 while (duration < 5.0) ;
cannam@85 73
cannam@85 74 if (src_data.input_frames_used != src_data.input_frames)
cannam@85 75 { printf ("\n\nLine %d : input frames used %ld should be %ld\n", __LINE__, src_data.input_frames_used, src_data.input_frames) ;
cannam@85 76 exit (1) ;
cannam@85 77 } ;
cannam@85 78
cannam@85 79 if (fabs (src_data.src_ratio * src_data.input_frames_used - src_data.output_frames_gen) > 2)
cannam@85 80 { printf ("\n\nLine %d : input / output length mismatch.\n\n", __LINE__) ;
cannam@85 81 printf (" input len : %d\n", ARRAY_LEN (input) / channels) ;
cannam@85 82 printf (" output len : %ld (should be %g +/- 2)\n\n", src_data.output_frames_gen,
cannam@85 83 floor (0.5 + src_data.src_ratio * src_data.input_frames_used)) ;
cannam@85 84 exit (1) ;
cannam@85 85 } ;
cannam@85 86
cannam@85 87 throughput = lrint (floor (total_frames / duration)) ;
cannam@85 88
cannam@85 89 if (best_throughput == 0)
cannam@85 90 { best_throughput = MAX (throughput, best_throughput) ;
cannam@85 91 printf ("%5.2f %10ld\n", duration, throughput) ;
cannam@85 92 }
cannam@85 93 else
cannam@85 94 { best_throughput = MAX (throughput, best_throughput) ;
cannam@85 95 printf ("%5.2f %10ld %10ld\n", duration, throughput, best_throughput) ;
cannam@85 96 }
cannam@85 97
cannam@85 98 return best_throughput ;
cannam@85 99 } /* throughput_test */
cannam@85 100
cannam@85 101 static void
cannam@85 102 single_run (void)
cannam@85 103 { const int max_channels = 10 ;
cannam@85 104 int k ;
cannam@85 105
cannam@85 106 printf ("\n CPU name : %s\n", get_cpu_name ()) ;
cannam@85 107
cannam@85 108 puts (
cannam@85 109 "\n"
cannam@85 110 " Converter Channels Duration Throughput\n"
cannam@85 111 " ---------------------------------------------------------------------"
cannam@85 112 ) ;
cannam@85 113
cannam@85 114 for (k = 1 ; k <= max_channels / 2 ; k++)
cannam@85 115 throughput_test (SRC_SINC_FASTEST, k, 0) ;
cannam@85 116
cannam@85 117 puts ("") ;
cannam@85 118 for (k = 1 ; k <= max_channels / 2 ; k++)
cannam@85 119 throughput_test (SRC_SINC_MEDIUM_QUALITY, k, 0) ;
cannam@85 120
cannam@85 121 puts ("") ;
cannam@85 122 for (k = 1 ; k <= max_channels ; k++)
cannam@85 123 throughput_test (SRC_SINC_BEST_QUALITY, k, 0) ;
cannam@85 124
cannam@85 125 puts ("") ;
cannam@85 126 return ;
cannam@85 127 } /* single_run */
cannam@85 128
cannam@85 129 static void
cannam@85 130 multi_run (int run_count)
cannam@85 131 { int k, ch ;
cannam@85 132
cannam@85 133 printf ("\n CPU name : %s\n", get_cpu_name ()) ;
cannam@85 134
cannam@85 135 puts (
cannam@85 136 "\n"
cannam@85 137 " Converter Channels Duration Throughput Best Throughput\n"
cannam@85 138 " ----------------------------------------------------------------------------------------"
cannam@85 139 ) ;
cannam@85 140
cannam@85 141 for (ch = 1 ; ch <= 5 ; ch++)
cannam@85 142 { long sinc_fastest = 0, sinc_medium = 0, sinc_best = 0 ;
cannam@85 143
cannam@85 144 for (k = 0 ; k < run_count ; k++)
cannam@85 145 { sinc_fastest = throughput_test (SRC_SINC_FASTEST, ch, sinc_fastest) ;
cannam@85 146 sinc_medium = throughput_test (SRC_SINC_MEDIUM_QUALITY, ch, sinc_medium) ;
cannam@85 147 sinc_best = throughput_test (SRC_SINC_BEST_QUALITY, ch, sinc_best) ;
cannam@85 148
cannam@85 149 puts ("") ;
cannam@85 150
cannam@85 151 /* Let the CPU cool down. We might be running on a laptop. */
cannam@85 152 sleep (10) ;
cannam@85 153 } ;
cannam@85 154
cannam@85 155 puts (
cannam@85 156 "\n"
cannam@85 157 " Converter Best Throughput\n"
cannam@85 158 " ------------------------------------------------"
cannam@85 159 ) ;
cannam@85 160
cannam@85 161 printf (" %-30s %10ld\n", src_get_name (SRC_SINC_FASTEST), sinc_fastest) ;
cannam@85 162 printf (" %-30s %10ld\n", src_get_name (SRC_SINC_MEDIUM_QUALITY), sinc_medium) ;
cannam@85 163 printf (" %-30s %10ld\n", src_get_name (SRC_SINC_BEST_QUALITY), sinc_best) ;
cannam@85 164 } ;
cannam@85 165
cannam@85 166 puts ("") ;
cannam@85 167 } /* multi_run */
cannam@85 168
cannam@85 169 static void
cannam@85 170 usage_exit (const char * argv0)
cannam@85 171 { const char * cptr ;
cannam@85 172
cannam@85 173 if ((cptr = strrchr (argv0, '/')) != NULL)
cannam@85 174 argv0 = cptr ;
cannam@85 175
cannam@85 176 printf (
cannam@85 177 "Usage :\n"
cannam@85 178 " %s - Single run of the throughput test.\n"
cannam@85 179 " %s --best-of N - Do N runs of test a print bext result.\n"
cannam@85 180 "\n",
cannam@85 181 argv0, argv0) ;
cannam@85 182
cannam@85 183 exit (0) ;
cannam@85 184 } /* usage_exit */
cannam@85 185
cannam@85 186 int
cannam@85 187 main (int argc, char ** argv)
cannam@85 188 { double freq ;
cannam@85 189
cannam@85 190 memset (input, 0, sizeof (input)) ;
cannam@85 191 freq = 0.01 ;
cannam@85 192 gen_windowed_sines (1, &freq, 1.0, input, BUFFER_LEN) ;
cannam@85 193
cannam@85 194 if (argc == 1)
cannam@85 195 single_run () ;
cannam@85 196 else if (argc == 3 && strcmp (argv [1], "--best-of") == 0)
cannam@85 197 { int run_count = atoi (argv [2]) ;
cannam@85 198
cannam@85 199 if (run_count < 1 || run_count > 20)
cannam@85 200 { printf ("Please be sensible. Run count should be in range (1, 10].\n") ;
cannam@85 201 exit (1) ;
cannam@85 202 } ;
cannam@85 203
cannam@85 204 multi_run (run_count) ;
cannam@85 205 }
cannam@85 206 else
cannam@85 207 usage_exit (argv [0]) ;
cannam@85 208
cannam@85 209 puts (
cannam@85 210 " Duration is in seconds.\n"
cannam@85 211 " Throughput is in frames/sec (more is better).\n"
cannam@85 212 ) ;
cannam@85 213
cannam@85 214 return 0 ;
cannam@85 215 } /* main */
cannam@85 216