diff options
author | Karel Zak <kzak@redhat.com> | 2006-05-09 15:35:46 +0000 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2006-05-09 15:35:46 +0000 |
commit | 0f579f785cff4c840fa2e0918a958f7e6f56a1f0 (patch) | |
tree | 15014496432625bd4faaf96ea9957af210e781e3 /tests/testutils.c | |
parent | * src/hash.c src/internal.h src/libvirt.c src/virterror.c (diff) | |
download | libvirt-0f579f785cff4c840fa2e0918a958f7e6f56a1f0.tar.gz libvirt-0f579f785cff4c840fa2e0918a958f7e6f56a1f0.tar.bz2 libvirt-0f579f785cff4c840fa2e0918a958f7e6f56a1f0.zip |
XML-RPC tests
Diffstat (limited to 'tests/testutils.c')
-rw-r--r-- | tests/testutils.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/testutils.c b/tests/testutils.c new file mode 100644 index 000000000..d79b9e5fb --- /dev/null +++ b/tests/testutils.c @@ -0,0 +1,74 @@ +/* + * utils.c: basic test utils + * + * Copyright (C) 2005 Red Hat, Inc. + * + * See COPYING.LIB for the License of this software + * + * Karel Zak <kzak@redhat.com> + * + * $Id$ + */ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/time.h> + +#include "testutils.h" + +#define GETTIMEOFDAY(T) gettimeofday(T, NULL) +#define DIFF_MSEC(T, U) \ + ((((int) ((T)->tv_sec - (U)->tv_sec)) * 1000000.0 + \ + ((int) ((T)->tv_usec - (U)->tv_usec))) / 1000.0) + +double +virtTestCountAverage(double *items, int nitems) +{ + long double sum = 0; + int i; + + for (i=1; i < nitems; i++) + sum += items[i]; + + return (double) (sum / nitems); +} + +/* + * Runs test anf count average time (if the nloops is grater than 1) + * + * returns: -1 = error, 0 = success + */ +int +virtTestRun(const char *title, int nloops, int (*body)(void *data), void *data) +{ + int i, ret = 0; + double *ts = NULL; + + if (nloops > 1 && (ts = calloc(nloops, + sizeof(double)))==NULL) + return -1; + + for (i=0; i < nloops; i++) { + struct timeval before, after; + + if (ts) + GETTIMEOFDAY(&before); + if ((ret = body(data)) != 0) + break; + if (ts) { + GETTIMEOFDAY(&after); + ts[i] = DIFF_MSEC(&after, &before); + } + } + if (ret == 0 && ts) + fprintf(stderr, "%-50s ... OK [%.5f ms]\n", title, + virtTestCountAverage(ts, nloops)); + else if (ret == 0) + fprintf(stderr, "%-50s ... OK\n", title); + else + fprintf(stderr, "%-50s ... FAILED\n", title); + + if (ts) + free(ts); + return ret; +} |