diff options
author | Kevin P. Fleming <kpfleming@digium.com> | 2008-05-23 14:21:58 +0000 |
---|---|---|
committer | Kevin P. Fleming <kpfleming@digium.com> | 2008-05-23 14:21:58 +0000 |
commit | 3403af6f5eba79740c98abb2678f9a66ef5a8190 (patch) | |
tree | 93415a9598e5363284832757de0596988a421aa6 /dahdi_test.c | |
parent | db9cffbffeedcde4ee52027160b96a2548262c5d (diff) |
initial copy
git-svn-id: http://svn.asterisk.org/svn/dahdi/tools/trunk@4335 a0bf4364-ded3-4de4-8d8a-66a801d63aff
Diffstat (limited to 'dahdi_test.c')
-rw-r--r-- | dahdi_test.c | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/dahdi_test.c b/dahdi_test.c new file mode 100644 index 0000000..1ee1a2c --- /dev/null +++ b/dahdi_test.c @@ -0,0 +1,135 @@ +#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> +#include <getopt.h> + +#define SIZE 8000 + +static int pass = 0; +static float best = 0.0; +static float worst = 100.0; +static double total = 0.0; +static double delay_total = 0.0; + +void hup_handler(int sig) +{ + printf("\n--- Results after %d passes ---\n", pass); + printf("Best: %.3f -- Worst: %.3f -- Average: %f, Difference: %f\n", + best, worst, pass ? total/pass : 100.00, pass ? delay_total/pass : 100); + exit(0); +} + +static void usage(char *argv0) +{ + char *c; + c = strrchr(argv0, '/'); + if (!c) + c = argv0; + else + c++; + fprintf(stderr, + "Usage: %s [-c COUNT] [-v]\n" + " Valid options are:\n" + " -c COUNT Run just COUNT cycles (otherwise: forever).\n" + " -v More verbose output.\n" + " -h This help text.\n" + ,c); +} + +int main(int argc, char *argv[]) +{ + int fd; + int res; + int c; + int count=0; + int seconds = 0; + int curarg = 1; + int verbose=0; + char buf[8192]; + float score; + float ms; + 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((c = getopt(argc, argv, "c:hv")) != -1) { + switch(c) { + case 'c': + seconds = atoi(optarg); + break; + case 'h': + usage(argv[0]); + exit(0); + break; + case '?': + usage(argv[0]); + exit(1); + break; + case 'v': + verbose++; + break; + } + } + while(curarg < argc) { + if (!strcasecmp(argv[curarg], "-v")) + verbose++; + if (!strcasecmp(argv[curarg], "-c") && argc > curarg) + seconds = atoi(argv[curarg + 1]); + curarg++; + } + printf("Opened pseudo zap interface, measuring accuracy...\n"); + signal(SIGHUP, hup_handler); + signal(SIGINT, hup_handler); + signal(SIGALRM, hup_handler); + /* Flush input buffer */ + for (count = 0;count < 4; count++) + res = read(fd, buf, sizeof(buf)); + count = 0; + ms = 0; /* Makes the compiler happy */ + if (seconds > 0) + alarm(seconds + 1); /* This will give 'seconds' cycles */ + for(;;) { + if (count == 0) + ms = 0; + gettimeofday(&start, NULL); + 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; + gettimeofday(&now, NULL); + ms += (now.tv_sec - start.tv_sec) * 8000; + ms += (now.tv_usec - start.tv_usec) / 125.0; + if (count >= SIZE) { + double percent; + + percent = 100.0 * (count - ms) / count; + if (verbose) + printf("\n%d zaptel samples in %0.3f system clock sample intervals (%.3f%%)", + count, ms, 100 - percent); + else if ((pass % 8) == 7) printf("\n"); + score = 100.0 - fabs(percent); + if (score > best) + best = score; + if (score < worst) + worst = score; + if (!verbose) + printf("%f%% ", score); + total += score; + delay_total += 100 - percent; + fflush(stdout); + count = 0; + pass++; + } + } +} |