@@ -194,6 +194,115 @@ static void clar_print_tap_onabort(const char *fmt, va_list arg)
194194 fflush (stdout );
195195}
196196
197+ /* timings format: useful for benchmarks */
198+
199+ static void clar_print_timing_init (int test_count , int suite_count , const char * suite_names )
200+ {
201+ (void )test_count ;
202+ (void )suite_count ;
203+ (void )suite_names ;
204+
205+ printf ("Started benchmarks (mean time ± stddev / min time … max time):\n\n" );
206+ }
207+
208+ static void clar_print_timing_shutdown (int test_count , int suite_count , int error_count )
209+ {
210+ (void )test_count ;
211+ (void )suite_count ;
212+ (void )error_count ;
213+ }
214+
215+ static void clar_print_timing_error (int num , const struct clar_report * report , const struct clar_error * error )
216+ {
217+ (void )num ;
218+ (void )report ;
219+ (void )error ;
220+ }
221+
222+ static void clar_print_timing_test_start (const char * suite_name , const char * test_name , int test_number )
223+ {
224+ (void )test_number ;
225+
226+ printf ("%s::%s: " , suite_name , test_name );
227+ fflush (stdout );
228+ }
229+
230+ static void clar_print_timing_time (double t )
231+ {
232+ static const char * units [] = { "sec" , "ms" , "μs" , "ns" };
233+ static const int units_len = sizeof (units ) / sizeof (units [0 ]);
234+ int unit = 0 , exponent = 0 , digits ;
235+
236+ while (t < 1.0 && unit < units_len - 1 ) {
237+ t *= 1000.0 ;
238+ unit ++ ;
239+ }
240+
241+ while (t > 0.0 && t < 1.0 && exponent < 10 ) {
242+ t *= 10.0 ;
243+ exponent ++ ;
244+ }
245+
246+ digits = (t < 10.0 ) ? 3 : ((t < 100.0 ) ? 2 : 1 );
247+
248+ printf ("%.*f" , digits , t );
249+
250+ if (exponent > 0 )
251+ printf ("e-%d" , exponent );
252+
253+ printf (" %s" , units [unit ]);
254+ }
255+
256+ static void clar_print_timing_test_finish (const char * suite_name , const char * test_name , int test_number , const struct clar_report * report )
257+ {
258+ const struct clar_error * error = _clar .last_report -> errors ;
259+
260+ (void )suite_name ;
261+ (void )test_name ;
262+ (void )test_number ;
263+
264+ switch (report -> status ) {
265+ case CL_TEST_OK :
266+ clar_print_timing_time (report -> time_mean );
267+
268+ if (report -> runs > 1 ) {
269+ printf (" ± " );
270+ clar_print_timing_time (report -> time_stddev );
271+
272+ printf (" / range: " );
273+ clar_print_timing_time (report -> time_min );
274+ printf (" … " );
275+ clar_print_timing_time (report -> time_max );
276+ printf (" (%d runs)" , report -> runs );
277+ }
278+
279+ printf ("\n" );
280+ break ;
281+ case CL_TEST_FAILURE :
282+ printf ("failed: %s\n" , error -> error_msg );
283+ break ;
284+ case CL_TEST_SKIP :
285+ case CL_TEST_NOTRUN :
286+ printf ("skipped\n" );
287+ break ;
288+ }
289+
290+ fflush (stdout );
291+ }
292+
293+ static void clar_print_timing_suite_start (const char * suite_name , int suite_index )
294+ {
295+ if (_clar .verbosity == 1 )
296+ printf ("\n%s" , suite_name );
297+
298+ (void )suite_index ;
299+ }
300+
301+ static void clar_print_timing_onabort (const char * fmt , va_list arg )
302+ {
303+ vfprintf (stderr , fmt , arg );
304+ }
305+
197306/* indirection between protocol output selection */
198307
199308#define PRINT (FN , ...) do { \
@@ -204,6 +313,9 @@ static void clar_print_tap_onabort(const char *fmt, va_list arg)
204313 case CL_OUTPUT_TAP: \
205314 clar_print_tap_##FN (__VA_ARGS__); \
206315 break; \
316+ case CL_OUTPUT_TIMING: \
317+ clar_print_timing_##FN (__VA_ARGS__); \
318+ break; \
207319 default: \
208320 abort(); \
209321 } \
0 commit comments