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