annotate src/libsamplerate-0.1.9/tests/multichan_throughput_test.c @ 166:cbd6d7e562c7

Merge build update
author Chris Cannam <cannam@all-day-breakfast.com>
date Thu, 31 Oct 2019 13:36:58 +0000
parents 4a7071416412
children
rev   line source
cannam@126 1 /*
cannam@126 2 ** Copyright (c) 2008-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@126 3 ** All rights reserved.
cannam@126 4 **
cannam@126 5 ** This code is released under 2-clause BSD license. Please see the
cannam@126 6 ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
cannam@126 7 */
cannam@126 8
cannam@126 9 #include <stdio.h>
cannam@126 10 #include <stdlib.h>
cannam@126 11 #include <string.h>
cannam@126 12 #include <time.h>
cannam@126 13 #include <unistd.h>
cannam@126 14
cannam@126 15 #include <samplerate.h>
cannam@126 16
cannam@126 17 #include "config.h"
cannam@126 18
cannam@126 19 #include "util.h"
cannam@126 20 #include "float_cast.h"
cannam@126 21
cannam@126 22 #define BUFFER_LEN (1<<17)
cannam@126 23
cannam@126 24 static float input [BUFFER_LEN] ;
cannam@126 25 static float output [BUFFER_LEN] ;
cannam@126 26
cannam@126 27 static long
cannam@126 28 throughput_test (int converter, int channels, long best_throughput)
cannam@126 29 { SRC_DATA src_data ;
cannam@126 30 clock_t start_time, clock_time ;
cannam@126 31 double duration ;
cannam@126 32 long total_frames = 0, throughput ;
cannam@126 33 int error ;
cannam@126 34
cannam@126 35 printf (" %-30s %2d ", src_get_name (converter), channels) ;
cannam@126 36 fflush (stdout) ;
cannam@126 37
cannam@126 38 src_data.data_in = input ;
cannam@126 39 src_data.input_frames = ARRAY_LEN (input) / channels ;
cannam@126 40
cannam@126 41 src_data.data_out = output ;
cannam@126 42 src_data.output_frames = ARRAY_LEN (output) / channels ;
cannam@126 43
cannam@126 44 src_data.src_ratio = 0.99 ;
cannam@126 45
cannam@126 46 sleep (2) ;
cannam@126 47
cannam@126 48 start_time = clock () ;
cannam@126 49
cannam@126 50 do
cannam@126 51 {
cannam@126 52 if ((error = src_simple (&src_data, converter, channels)) != 0)
cannam@126 53 { puts (src_strerror (error)) ;
cannam@126 54 exit (1) ;
cannam@126 55 } ;
cannam@126 56
cannam@126 57 total_frames += src_data.output_frames_gen ;
cannam@126 58
cannam@126 59 clock_time = clock () - start_time ;
cannam@126 60 duration = (1.0 * clock_time) / CLOCKS_PER_SEC ;
cannam@126 61 }
cannam@126 62 while (duration < 5.0) ;
cannam@126 63
cannam@126 64 if (src_data.input_frames_used != src_data.input_frames)
cannam@126 65 { printf ("\n\nLine %d : input frames used %ld should be %ld\n", __LINE__, src_data.input_frames_used, src_data.input_frames) ;
cannam@126 66 exit (1) ;
cannam@126 67 } ;
cannam@126 68
cannam@126 69 if (fabs (src_data.src_ratio * src_data.input_frames_used - src_data.output_frames_gen) > 2)
cannam@126 70 { printf ("\n\nLine %d : input / output length mismatch.\n\n", __LINE__) ;
cannam@126 71 printf (" input len : %d\n", ARRAY_LEN (input) / channels) ;
cannam@126 72 printf (" output len : %ld (should be %g +/- 2)\n\n", src_data.output_frames_gen,
cannam@126 73 floor (0.5 + src_data.src_ratio * src_data.input_frames_used)) ;
cannam@126 74 exit (1) ;
cannam@126 75 } ;
cannam@126 76
cannam@126 77 throughput = lrint (floor (total_frames / duration)) ;
cannam@126 78
cannam@126 79 if (best_throughput == 0)
cannam@126 80 { best_throughput = MAX (throughput, best_throughput) ;
cannam@126 81 printf ("%5.2f %10ld\n", duration, throughput) ;
cannam@126 82 }
cannam@126 83 else
cannam@126 84 { best_throughput = MAX (throughput, best_throughput) ;
cannam@126 85 printf ("%5.2f %10ld %10ld\n", duration, throughput, best_throughput) ;
cannam@126 86 }
cannam@126 87
cannam@126 88 return best_throughput ;
cannam@126 89 } /* throughput_test */
cannam@126 90
cannam@126 91 static void
cannam@126 92 single_run (void)
cannam@126 93 { const int max_channels = 10 ;
cannam@126 94 int k ;
cannam@126 95
cannam@126 96 printf ("\n CPU name : %s\n", get_cpu_name ()) ;
cannam@126 97
cannam@126 98 puts (
cannam@126 99 "\n"
cannam@126 100 " Converter Channels Duration Throughput\n"
cannam@126 101 " ---------------------------------------------------------------------"
cannam@126 102 ) ;
cannam@126 103
cannam@126 104 for (k = 1 ; k <= max_channels / 2 ; k++)
cannam@126 105 throughput_test (SRC_SINC_FASTEST, k, 0) ;
cannam@126 106
cannam@126 107 puts ("") ;
cannam@126 108 for (k = 1 ; k <= max_channels / 2 ; k++)
cannam@126 109 throughput_test (SRC_SINC_MEDIUM_QUALITY, k, 0) ;
cannam@126 110
cannam@126 111 puts ("") ;
cannam@126 112 for (k = 1 ; k <= max_channels ; k++)
cannam@126 113 throughput_test (SRC_SINC_BEST_QUALITY, k, 0) ;
cannam@126 114
cannam@126 115 puts ("") ;
cannam@126 116 return ;
cannam@126 117 } /* single_run */
cannam@126 118
cannam@126 119 static void
cannam@126 120 multi_run (int run_count)
cannam@126 121 { int k, ch ;
cannam@126 122
cannam@126 123 printf ("\n CPU name : %s\n", get_cpu_name ()) ;
cannam@126 124
cannam@126 125 puts (
cannam@126 126 "\n"
cannam@126 127 " Converter Channels Duration Throughput Best Throughput\n"
cannam@126 128 " ----------------------------------------------------------------------------------------"
cannam@126 129 ) ;
cannam@126 130
cannam@126 131 for (ch = 1 ; ch <= 5 ; ch++)
cannam@126 132 { long sinc_fastest = 0, sinc_medium = 0, sinc_best = 0 ;
cannam@126 133
cannam@126 134 for (k = 0 ; k < run_count ; k++)
cannam@126 135 { sinc_fastest = throughput_test (SRC_SINC_FASTEST, ch, sinc_fastest) ;
cannam@126 136 sinc_medium = throughput_test (SRC_SINC_MEDIUM_QUALITY, ch, sinc_medium) ;
cannam@126 137 sinc_best = throughput_test (SRC_SINC_BEST_QUALITY, ch, sinc_best) ;
cannam@126 138
cannam@126 139 puts ("") ;
cannam@126 140
cannam@126 141 /* Let the CPU cool down. We might be running on a laptop. */
cannam@126 142 sleep (10) ;
cannam@126 143 } ;
cannam@126 144
cannam@126 145 puts (
cannam@126 146 "\n"
cannam@126 147 " Converter Best Throughput\n"
cannam@126 148 " ------------------------------------------------"
cannam@126 149 ) ;
cannam@126 150
cannam@126 151 printf (" %-30s %10ld\n", src_get_name (SRC_SINC_FASTEST), sinc_fastest) ;
cannam@126 152 printf (" %-30s %10ld\n", src_get_name (SRC_SINC_MEDIUM_QUALITY), sinc_medium) ;
cannam@126 153 printf (" %-30s %10ld\n", src_get_name (SRC_SINC_BEST_QUALITY), sinc_best) ;
cannam@126 154 } ;
cannam@126 155
cannam@126 156 puts ("") ;
cannam@126 157 } /* multi_run */
cannam@126 158
cannam@126 159 static void
cannam@126 160 usage_exit (const char * argv0)
cannam@126 161 { const char * cptr ;
cannam@126 162
cannam@126 163 if ((cptr = strrchr (argv0, '/')) != NULL)
cannam@126 164 argv0 = cptr ;
cannam@126 165
cannam@126 166 printf (
cannam@126 167 "Usage :\n"
cannam@126 168 " %s - Single run of the throughput test.\n"
cannam@126 169 " %s --best-of N - Do N runs of test a print bext result.\n"
cannam@126 170 "\n",
cannam@126 171 argv0, argv0) ;
cannam@126 172
cannam@126 173 exit (0) ;
cannam@126 174 } /* usage_exit */
cannam@126 175
cannam@126 176 int
cannam@126 177 main (int argc, char ** argv)
cannam@126 178 { double freq ;
cannam@126 179
cannam@126 180 memset (input, 0, sizeof (input)) ;
cannam@126 181 freq = 0.01 ;
cannam@126 182 gen_windowed_sines (1, &freq, 1.0, input, BUFFER_LEN) ;
cannam@126 183
cannam@126 184 if (argc == 1)
cannam@126 185 single_run () ;
cannam@126 186 else if (argc == 3 && strcmp (argv [1], "--best-of") == 0)
cannam@126 187 { int run_count = atoi (argv [2]) ;
cannam@126 188
cannam@126 189 if (run_count < 1 || run_count > 20)
cannam@126 190 { printf ("Please be sensible. Run count should be in range (1, 10].\n") ;
cannam@126 191 exit (1) ;
cannam@126 192 } ;
cannam@126 193
cannam@126 194 multi_run (run_count) ;
cannam@126 195 }
cannam@126 196 else
cannam@126 197 usage_exit (argv [0]) ;
cannam@126 198
cannam@126 199 puts (
cannam@126 200 " Duration is in seconds.\n"
cannam@126 201 " Throughput is in frames/sec (more is better).\n"
cannam@126 202 ) ;
cannam@126 203
cannam@126 204 return 0 ;
cannam@126 205 } /* main */
cannam@126 206