Debian Codegraph @

tests/throughput_test.c

raw
1 /*
2 ** Copyright (c) 2004-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<<16)
34
35 static float input [BUFFER_LEN] ;
36 static float output [BUFFER_LEN] ;
37
38 static long
39 throughput_test (int converter, long best_throughput)
40 { SRC_DATA src_data ;
41 clock_t start_time, clock_time ;
42 double duration ;
43 long total_frames = 0, throughput ;
44 int error ;
45
46 printf (" %-30s ", src_get_name (converter)) ;
47 fflush (stdout) ;
48
49 src_data.data_in = input ;
50 src_data.input_frames = ARRAY_LEN (input) ;
51
52 src_data.data_out = output ;
53 src_data.output_frames = ARRAY_LEN (output) ;
54
55 src_data.src_ratio = 0.99 ;
56
57 #ifdef _WIN32
58 Sleep (2000) ;
59 #else
60 sleep (2) ;
61 #endif
62
63 start_time = clock () ;
64
65 do
66 {
67 if ((error = src_simple (&src_data, converter, 1)) != 0)
68 { puts (src_strerror (error)) ;
69 exit (1) ;
70 } ;
71
72 total_frames += src_data.output_frames_gen ;
73
74 clock_time = clock () - start_time ;
75 #ifdef __GNU__ /* Clock resolution is 10ms on GNU/Hurd */
76 duration = (10000.0 * clock_time) / CLOCKS_PER_SEC ;
77 #else
78 duration = (1.0 * clock_time) / CLOCKS_PER_SEC ;
79 #endif
80 }
81 while (duration < 3.0) ;
82
83 if (src_data.input_frames_used != ARRAY_LEN (input))
84 { printf ("\n\nLine %d : input frames used %ld should be %d\n", __LINE__, src_data.input_frames_used, ARRAY_LEN (input)) ;
85 exit (1) ;
86 } ;
87
88 if (fabs (src_data.src_ratio * src_data.input_frames_used - src_data.output_frames_gen) > 2)
89 { printf ("\n\nLine %d : input / output length mismatch.\n\n", __LINE__) ;
90 printf (" input len : %d\n", ARRAY_LEN (input)) ;
91 printf (" output len : %ld (should be %g +/- 2)\n\n", src_data.output_frames_gen,
92 floor (0.5 + src_data.src_ratio * src_data.input_frames_used)) ;
93 exit (1) ;
94 } ;
95
96 throughput = lrint (floor (total_frames / duration)) ;
97
98 if (best_throughput == 0)
99 { best_throughput = MAX (throughput, best_throughput) ;
100 printf ("%5.2f %10ld\n", duration, throughput) ;
101 }
102 else
103 { best_throughput = MAX (throughput, best_throughput) ;
104 printf ("%5.2f %10ld %10ld\n", duration, throughput, best_throughput) ;
105 }
106
107
108 return best_throughput ;
109 } /* throughput_test */
110
111 static void
112 single_run (void)
113 {
114
115 printf ("\n CPU name : %s\n", get_cpu_name ()) ;
116
117 puts (
118 "\n"
119 " Converter Duration Throughput\n"
120 " -----------------------------------------------------------"
121 ) ;
122
123 throughput_test (SRC_ZERO_ORDER_HOLD, 0) ;
124 throughput_test (SRC_LINEAR, 0) ;
125 #ifdef ENABLE_SINC_FAST_CONVERTER
126 throughput_test (SRC_SINC_FASTEST, 0) ;
127 #endif
128 #ifdef ENABLE_SINC_MEDIUM_CONVERTER
129 throughput_test (SRC_SINC_MEDIUM_QUALITY, 0) ;
130 #endif
131 #ifdef ENABLE_SINC_BEST_CONVERTER
132 throughput_test (SRC_SINC_BEST_QUALITY, 0) ;
133 #endif
134
135 puts ("") ;
136 return ;
137 } /* single_run */
138
139 static void
140 multi_run (int run_count)
141 { long zero_order_hold = 0, linear = 0 ;
142
143 #ifdef ENABLE_SINC_FAST_CONVERTER
144 long sinc_fastest = 0 ;
145 #endif
146
147 #ifdef ENABLE_SINC_MEDIUM_CONVERTER
148 long sinc_medium = 0 ;
149 #endif
150
151 #ifdef ENABLE_SINC_BEST_CONVERTER
152 long sinc_best = 0 ;
153 #endif
154 int k ;
155
156 puts (
157 "\n"
158 " Converter Duration Throughput Best Throughput\n"
159 " --------------------------------------------------------------------------------"
160 ) ;
161
162 for (k = 0 ; k < run_count ; k++)
163 { zero_order_hold = throughput_test (SRC_ZERO_ORDER_HOLD, zero_order_hold) ;
164 linear = throughput_test (SRC_LINEAR, linear) ;
165 #ifdef ENABLE_SINC_FAST_CONVERTER
166 sinc_fastest = throughput_test (SRC_SINC_FASTEST, sinc_fastest) ;
167 #endif
168 #ifdef ENABLE_SINC_MEDIUM_CONVERTER
169 sinc_medium = throughput_test (SRC_SINC_MEDIUM_QUALITY, sinc_medium) ;
170 #endif
171 #ifdef ENABLE_SINC_BEST_CONVERTER
172 sinc_best = throughput_test (SRC_SINC_BEST_QUALITY, sinc_best) ;
173 #endif
174 puts ("") ;
175
176 /* Let the CPU cool down. We might be running on a laptop. */
177 #ifdef _WIN32
178 Sleep (10000) ;
179 #else
180 sleep (10) ;
181 #endif
182 } ;
183
184 printf ("\n CPU name : %s\n", get_cpu_name ()) ;
185
186 puts (
187 "\n"
188 " Converter Best Throughput\n"
189 " ------------------------------------------------"
190 ) ;
191 printf (" %-30s %10ld\n", src_get_name (SRC_ZERO_ORDER_HOLD), zero_order_hold) ;
192 printf (" %-30s %10ld\n", src_get_name (SRC_LINEAR), linear) ;
193 #ifdef ENABLE_SINC_FAST_CONVERTER
194 printf (" %-30s %10ld\n", src_get_name (SRC_SINC_FASTEST), sinc_fastest) ;
195 #endif
196 #ifdef ENABLE_SINC_MEDIUM_CONVERTER
197 printf (" %-30s %10ld\n", src_get_name (SRC_SINC_MEDIUM_QUALITY), sinc_medium) ;
198 #endif
199 #ifdef ENABLE_SINC_BEST_CONVERTER
200 printf (" %-30s %10ld\n", src_get_name (SRC_SINC_BEST_QUALITY), sinc_best) ;
201 #endif
202
203 puts ("") ;
204 } /* multi_run */
205
206 static void
207 usage_exit (const char * argv0)
208 { const char * cptr ;
209
210 if ((cptr = strrchr (argv0, '/')) != NULL)
211 argv0 = cptr ;
212
213 printf (
214 "Usage :\n"
215 " %s - Single run of the throughput test.\n"
216 " %s --best-of N - Do N runs of test a print bext result.\n"
217 "\n",
218 argv0, argv0) ;
219
220 exit (0) ;
221 } /* usage_exit */
222
223 int
224 main (int argc, char ** argv)
225 { double freq ;
226
227 memset (input, 0, sizeof (input)) ;
228 freq = 0.01 ;
229 gen_windowed_sines (1, &freq, 1.0, input, BUFFER_LEN) ;
230
231 if (argc == 1)
232 single_run () ;
233 else if (argc == 3 && strcmp (argv [1], "--best-of") == 0)
234 { int run_count = atoi (argv [2]) ;
235
236 if (run_count < 1 || run_count > 20)
237 { printf ("Please be sensible. Run count should be in range (1, 10].\n") ;
238 exit (1) ;
239 } ;
240
241 multi_run (run_count) ;
242 }
243 else
244 usage_exit (argv [0]) ;
245
246 puts (
247 " Duration is in seconds.\n"
248 " Throughput is in samples/sec (more is better).\n"
249 ) ;
250
251 return 0 ;
252 } /* main */
253