Debian Codegraph @

tests/multichan_throughput_test.c

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