summaryrefslogtreecommitdiff
path: root/tests/benchmark.c
blob: f33bfee43b63e93abaa95be2ec7fe22cebd3624f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "sfconfig.h"

#include <stdio.h>
#include <stdlib.h>

#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#if (HAVE_DECL_S_IRGRP == 0)
#include <sf_unistd.h>
#endif

#include <string.h>
#include <math.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>

#include <sndfile.h>

#ifndef		M_PI
#define		M_PI		3.14159265358979323846264338
#endif

/*
**	Neat solution to the Win32/OS2 binary file flage requirement.
**	If O_BINARY isn't already defined by the inclusion of the system
**	headers, set it to zero.
*/
#ifndef O_BINARY
#define O_BINARY 0
#endif

#define	WRITE_FLAGS	(O_WRONLY | O_CREAT | O_TRUNC | O_BINARY)
#define	READ_FLAGS	(O_RDONLY | O_BINARY)

#if (defined (WIN32) || defined (_WIN32) || defined (__OS2__))
	#define WRITE_PERMS	0777
#else
	#define WRITE_PERMS	(S_IRUSR | S_IWUSR | S_IRGRP)
#endif

#define	BUFFER_SIZE		(1<<18)
#define	BLOCK_COUNT		(30)
#define	TEST_DURATION	(5)		/* 5 Seconds. */

typedef struct
{	double	write_rate ;
	double	read_rate ;
} PERF_STATS ;

static void	*data = NULL ;

static void calc_raw_performance (PERF_STATS *stats) ;

static void	calc_short_performance (int format, double read_rate, double write_rate) ;
static void	calc_int_performance (int format, double read_rate, double write_rate) ;
static void	calc_float_performance (int format, double read_rate, double write_rate) ;


static int cpu_is_big_endian (void) ;

static const char* get_subtype_str (int subtype) ;

int
main (int argc, char *argv [])
{	PERF_STATS	stats ;
	char		buffer [256] = "Benchmarking " ;
	int			format_major ;

	if (! (data = malloc (BUFFER_SIZE * sizeof (double))))
	{	perror ("Error : malloc failed") ;
		exit (1) ;
		} ;

	sf_command (NULL, SFC_GET_LIB_VERSION, buffer + strlen (buffer), sizeof (buffer) - strlen (buffer)) ;

	puts (buffer) ;
	memset (buffer, '-', strlen (buffer)) ;
	puts (buffer) ;
	printf ("Each test takes a little over %d seconds.\n\n", TEST_DURATION) ;

	calc_raw_performance (&stats) ;

	if (argc < 2 || strcmp ("--native-only", argv [1]) == 0)
	{	puts ("\nNative endian I/O :") ;
		format_major = cpu_is_big_endian () ? SF_FORMAT_AIFF : SF_FORMAT_WAV ;

		calc_short_performance	(format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
		calc_int_performance	(format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
		calc_int_performance	(format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
		calc_float_performance	(format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
		calc_float_performance	(format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
		calc_float_performance	(format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
		calc_float_performance	(format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
		} ;

	if (argc < 2 || strcmp ("--swap-only", argv [1]) == 0)
	{	puts ("\nEndian swapped I/O :") ;
		format_major = cpu_is_big_endian () ? SF_FORMAT_WAV : SF_FORMAT_AIFF ;

		calc_short_performance	(format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
		calc_int_performance	(format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
		calc_int_performance	(format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
		calc_float_performance	(format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
		calc_float_performance	(format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
		calc_float_performance	(format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
		calc_float_performance	(format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
		} ;

	puts ("") ;

	free (data) ;

	return 0 ;
} /* main */

/*==============================================================================
*/

static void
calc_raw_performance (PERF_STATS *stats)
{	clock_t start_clock, clock_time ;
	int fd, k, byte_count, retval, op_count ;
	const char *filename ;

	filename = "benchmark.dat" ;

	byte_count = BUFFER_SIZE * sizeof (short) ;

	/* Collect write stats */
	printf ("    Raw write PCM_16  : ") ;
	fflush (stdout) ;

	clock_time = 0 ;
	op_count = 0 ;
	start_clock = clock () ;

	while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
	{	if ((fd = open (filename, WRITE_FLAGS, WRITE_PERMS)) < 0)
		{	printf ("Error : not able to open file : %s\n", filename) ;
			perror ("") ;
			exit (1) ;
			} ;

		for (k = 0 ; k < BLOCK_COUNT ; k++)
		{	if ((retval = write (fd, data, byte_count)) != byte_count)
			{	printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
				exit (1) ;
				} ;
			} ;

		close (fd) ;

		clock_time = clock () - start_clock ;
		op_count ++ ;
		} ;

	stats->write_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
	stats->write_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
	printf ("%10.0f samples per sec\n", stats->write_rate) ;

	/* Collect read stats */
	printf ("    Raw read  PCM_16  : ") ;
	fflush (stdout) ;

	clock_time = 0 ;
	op_count = 0 ;
	start_clock = clock () ;

	while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
	{	if ((fd = open (filename, READ_FLAGS)) < 0)
		{	printf ("Error : not able to open file : %s\n", filename) ;
			perror ("") ;
			exit (1) ;
			} ;

		for (k = 0 ; k < BLOCK_COUNT ; k++)
		{	if ((retval = read (fd, data, byte_count)) != byte_count)
			{	printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
				exit (1) ;
				} ;
			} ;

		close (fd) ;

		clock_time = clock () - start_clock ;
		op_count ++ ;
		} ;

	stats->read_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
	stats->read_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
	printf ("%10.0f samples per sec\n", stats->read_rate) ;

	unlink (filename) ;
} /* calc_raw_performance */

/*------------------------------------------------------------------------------
*/

static void
calc_short_performance (int format, double read_rate, double write_rate)
{	SNDFILE *file ;
	SF_INFO	sfinfo ;
	clock_t start_clock, clock_time ;
	double	performance ;
	int k, item_count, retval, op_count ;
	const char* subtype ;
	short *short_data ;
	const char *filename ;

	filename = "benchmark.dat" ;
	subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;

	short_data = data ;
	item_count = BUFFER_SIZE ;
	for (k = 0 ; k < item_count ; k++)
		short_data [k] = 32700.0 * sin (2 * M_PI * k / 32000.0) ;

	/* Collect write stats */
	printf ("    Write %-5s   to  %s : ", "short", subtype) ;
	fflush (stdout) ;

	sfinfo.channels = 1 ;
	sfinfo.format = format ;
	sfinfo.frames = 1 ;
	sfinfo.samplerate = 32000 ;

	clock_time = 0 ;
	op_count = 0 ;
	start_clock = clock () ;

	while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
	{	if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
		{	printf ("Error : not able to open file : %s\n", filename) ;
			perror ("") ;
			exit (1) ;
			} ;

		/* Turn off the addition of a PEAK chunk. */
		sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;

		for (k = 0 ; k < BLOCK_COUNT ; k++)
		{	if ((retval = sf_write_short (file, short_data, item_count)) != item_count)
			{	printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ;
				exit (1) ;
				} ;
			} ;

		sf_close (file) ;

		clock_time = clock () - start_clock ;
		op_count ++ ;
		} ;

	performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
	performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
	printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ;

	/* Collect read stats */
	printf ("    Read  %-5s  from %s : ", "short", subtype) ;
	fflush (stdout) ;

	clock_time = 0 ;
	op_count = 0 ;
	start_clock = clock () ;

	while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
	{	if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
		{	printf ("Error : not able to open file : %s\n", filename) ;
			perror ("") ;
			exit (1) ;
			} ;

		for (k = 0 ; k < BLOCK_COUNT ; k++)
		{	if ((retval = sf_read_short (file, short_data, item_count)) != item_count)
			{	printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ;
				exit (1) ;
				} ;
			} ;

		sf_close (file) ;

		clock_time = clock () - start_clock ;
		op_count ++ ;
		} ;

	performance = (1.0 * item_count) * BLOCK_COUNT * op_count ;
	performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
	printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ;

	unlink (filename) ;

} /* calc_short_performance */
static void
calc_int_performance (int format, double read_rate, double write_rate)
{	SNDFILE *file ;
	SF_INFO	sfinfo ;
	clock_t start_clock, clock_time ;
	double	performance ;
	int k, item_count, retval, op_count ;
	const char* subtype ;
	int *int_data ;
	const char *filename ;

	filename = "benchmark.dat" ;
	subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;

	int_data = data ;
	item_count = BUFFER_SIZE ;
	for (k = 0 ; k < item_count ; k++)
		int_data [k] = 32700.0 * (1<<16) * sin (2 * M_PI * k / 32000.0) ;

	/* Collect write stats */
	printf ("    Write %-5s   to  %s : ", "int", subtype) ;
	fflush (stdout) ;

	sfinfo.channels = 1 ;
	sfinfo.format = format ;
	sfinfo.frames = 1 ;
	sfinfo.samplerate = 32000 ;

	clock_time = 0 ;
	op_count = 0 ;
	start_clock = clock () ;

	while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
	{	if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
		{	printf ("Error : not able to open file : %s\n", filename) ;
			perror ("") ;
			exit (1) ;
			} ;

		/* Turn off the addition of a PEAK chunk. */
		sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;

		for (k = 0 ; k < BLOCK_COUNT ; k++)
		{	if ((retval = sf_write_int (file, int_data, item_count)) != item_count)
			{	printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ;
				exit (1) ;
				} ;
			} ;

		sf_close (file) ;

		clock_time = clock () - start_clock ;
		op_count ++ ;
		} ;

	performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
	performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
	printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ;

	/* Collect read stats */
	printf ("    Read  %-5s  from %s : ", "int", subtype) ;
	fflush (stdout) ;

	clock_time = 0 ;
	op_count = 0 ;
	start_clock = clock () ;

	while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
	{	if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
		{	printf ("Error : not able to open file : %s\n", filename) ;
			perror ("") ;
			exit (1) ;
			} ;

		for (k = 0 ; k < BLOCK_COUNT ; k++)
		{	if ((retval = sf_read_int (file, int_data, item_count)) != item_count)
			{	printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ;
				exit (1) ;
				} ;
			} ;

		sf_close (file) ;

		clock_time = clock () - start_clock ;
		op_count ++ ;
		} ;

	performance = (1.0 * item_count) * BLOCK_COUNT * op_count ;
	performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
	printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ;

	unlink (filename) ;

} /* calc_int_performance */
static void
calc_float_performance (int format, double read_rate, double write_rate)
{	SNDFILE *file ;
	SF_INFO	sfinfo ;
	clock_t start_clock, clock_time ;
	double	performance ;
	int k, item_count, retval, op_count ;
	const char* subtype ;
	float *float_data ;
	const char *filename ;

	filename = "benchmark.dat" ;
	subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;

	float_data = data ;
	item_count = BUFFER_SIZE ;
	for (k = 0 ; k < item_count ; k++)
		float_data [k] = 1.0 * sin (2 * M_PI * k / 32000.0) ;

	/* Collect write stats */
	printf ("    Write %-5s   to  %s : ", "float", subtype) ;
	fflush (stdout) ;

	sfinfo.channels = 1 ;
	sfinfo.format = format ;
	sfinfo.frames = 1 ;
	sfinfo.samplerate = 32000 ;

	clock_time = 0 ;
	op_count = 0 ;
	start_clock = clock () ;

	while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
	{	if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
		{	printf ("Error : not able to open file : %s\n", filename) ;
			perror ("") ;
			exit (1) ;
			} ;

		/* Turn off the addition of a PEAK chunk. */
		sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;

		for (k = 0 ; k < BLOCK_COUNT ; k++)
		{	if ((retval = sf_write_float (file, float_data, item_count)) != item_count)
			{	printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ;
				exit (1) ;
				} ;
			} ;

		sf_close (file) ;

		clock_time = clock () - start_clock ;
		op_count ++ ;
		} ;

	performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
	performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
	printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ;

	/* Collect read stats */
	printf ("    Read  %-5s  from %s : ", "float", subtype) ;
	fflush (stdout) ;

	clock_time = 0 ;
	op_count = 0 ;
	start_clock = clock () ;

	while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
	{	if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
		{	printf ("Error : not able to open file : %s\n", filename) ;
			perror ("") ;
			exit (1) ;
			} ;

		for (k = 0 ; k < BLOCK_COUNT ; k++)
		{	if ((retval = sf_read_float (file, float_data, item_count)) != item_count)
			{	printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ;
				exit (1) ;
				} ;
			} ;

		sf_close (file) ;

		clock_time = clock () - start_clock ;
		op_count ++ ;
		} ;

	performance = (1.0 * item_count) * BLOCK_COUNT * op_count ;
	performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
	printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ;

	unlink (filename) ;

} /* calc_float_performance */


/*==============================================================================
*/

static int
cpu_is_big_endian (void)
{	unsigned char 	*cptr ;
	int 			endtest ;

	endtest = 0x12345678 ;

	cptr = (unsigned char*) (&endtest) ;

	if (cptr [0] == 0x12 && cptr [1] == 0x34 && cptr [3] == 0x78)
		return SF_TRUE ;

	return SF_FALSE ;
} /* cpu_is_big_endian */

static const char*
get_subtype_str (int subtype)
{	switch (subtype)
	{	case SF_FORMAT_PCM_16 :
				return "PCM_16" ;

		case SF_FORMAT_PCM_24 :
				return "PCM_24" ;

		case SF_FORMAT_PCM_32 :
				return "PCM_32" ;

		case SF_FORMAT_FLOAT :
				return "FLOAT " ;

		case SF_FORMAT_DOUBLE :
				return "DOUBLE" ;

		default : break ;
		} ;

	return "UNKNOWN" ;
} /* get_subtype_str */