summaryrefslogtreecommitdiff
path: root/zttest.c
blob: 86551a02033cb92a062d7da9db4a7c511131e7b9 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <math.h>

#define SIZE 8000

static int pass = 0;
static float best = 0.0;
static float worst = 100.0;

void hup_handler(int sig)
{
	printf("\n--- Results after %d passes ---\n", pass);
	printf("Best: %f -- Worst: %f\n", best, worst);
	exit(0);
}

int main(int argc, char *argv[])
{
	int fd;
	int res;
	int count=0;
	int ms;
	int curarg = 1;
	int verbose=0;
	char buf[8192];
	float score;
	struct timeval start, now;
	fd = open("/dev/zap/pseudo", O_RDWR);
	if (fd < 0) {
		fprintf(stderr, "Unable to open zap interface: %s\n", strerror(errno));
		exit(1);
	}
	while(curarg < argc) {
		if (!strcasecmp(argv[curarg], "-v"))
			verbose++;
		curarg++;
	}
	printf("Opened pseudo zap interface, measuring accuracy...\n");
	signal(SIGHUP, hup_handler);
	signal(SIGINT, hup_handler);
	/* Flush input buffer */
	for (count = 0;count < 4; count++)
		res = read(fd, buf, sizeof(buf));
	count = 0;
	gettimeofday(&start, NULL);
	for(;;) {
		res = read(fd, buf, sizeof(buf));
		if (res < 0) {
			fprintf(stderr, "Failed to read from pseudo interface: %s\n", strerror(errno));
			exit(1);
		}
		count += res;
		if (count >= SIZE) {
			gettimeofday(&now, NULL);
			ms = (now.tv_sec - start.tv_sec) * 8000;
			ms += (now.tv_usec - start.tv_usec) / 125;
			start = now;
			if (verbose)
				printf("\n%d samples in %d sample intervals ", count, ms);
			else if ((pass % 8) == 7) printf("\n");
			score = 100.0 - 100.0 * fabs((float)count - (float)ms) / (float)count;
			if (score > best)
				best = score;
			if (score < worst)
				worst = score;
			printf("%f%% ", score);
			fflush(stdout);
			count = 0;
			pass++;
		}
	}
}