author | Alberto Bertogli
<albertito@gmail.com> 2007-01-06 18:16:16 UTC |
committer | Alberto Bertogli
<albertito@gmail.com> 2007-01-06 18:16:16 UTC |
parent | 9087d369b90324bc74d4fec468fbc0dc78ee208c |
libnmdb/Makefile | +10 | -3 |
libnmdb/test2cm.c | +114 | -0 |
libnmdb/test2dm.c | +106 | -0 |
diff --git a/libnmdb/Makefile b/libnmdb/Makefile index 5007826..39a5d5d 100644 --- a/libnmdb/Makefile +++ b/libnmdb/Makefile @@ -13,7 +13,7 @@ endif PREFIX=/usr/local -OBJS = libnmdb.o test1c.o test1d.o test2c.o test2d.o +OBJS = libnmdb.o test1c.o test1d.o test2c.o test2cm.o test2d.o test2dm.o default: all @@ -30,7 +30,7 @@ libnmdb.a: libnmdb.o $(AR) cr libnmdb.a libnmdb.o -tests: test1c test1d test2c test2d +tests: test1c test1d test2c test2cm test2d test2dm test1c: test1c.o libnmdb.a $(CC) $(CFLAGS) test1c.o libnmdb.a -o test1c @@ -41,9 +41,15 @@ test1d: test1d.o libnmdb.a test2c: test2c.o libnmdb.a $(CC) $(CFLAGS) test2c.o libnmdb.a -o test2c +test2cm: test2cm.o libnmdb.a + $(CC) $(CFLAGS) test2cm.o libnmdb.a -o test2cm + test2d: test2d.o libnmdb.a $(CC) $(CFLAGS) test2d.o libnmdb.a -o test2d +test2dm: test2dm.o libnmdb.a + $(CC) $(CFLAGS) test2dm.o libnmdb.a -o test2dm + install: libs install -d $(PREFIX)/lib @@ -62,7 +68,8 @@ install: libs $(CC) $(CFLAGS) -c $< -o $@ clean: - rm -f $(OBJS) libnmdb.so libnmdb.a test1c test1d test2c test2d + rm -f $(OBJS) libnmdb.so libnmdb.a + rm -f test1c test1d test2c test2cm test2d test2dm rm -f *.bb *.bbg *.da *.gcov gmon.out .PHONY: default all libs tests install clean diff --git a/libnmdb/test2cm.c b/libnmdb/test2cm.c new file mode 100644 index 0000000..f2e4365 --- /dev/null +++ b/libnmdb/test2cm.c @@ -0,0 +1,114 @@ + +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <sys/time.h> +#include <stdlib.h> + +#include "nmdb.h" +#include "timer.h" + + +int main(int argc, char **argv) +{ + int i, r, times; + unsigned char *key, *val, *gval; + size_t ksize, vsize; + unsigned long s_elapsed, g_elapsed, d_elapsed, misses = 0; + nmdb_t *db; + + if (argc != 4) { + printf("Usage: test2 TIMES KSIZE VSIZE\n"); + return 1; + } + + times = atoi(argv[1]); + ksize = atoi(argv[2]); + vsize = atoi(argv[3]); + if (times < 1) { + printf("Error: TIMES must be >= 1\n"); + return 1; + } + if (ksize < sizeof(int) || vsize < sizeof(int)) { + printf("Error: KSIZE and VSIZE must be >= sizeof(int)\n"); + return 1; + } + + key = malloc(ksize); + memset(key, 0, ksize); + val = malloc(vsize); + memset(val, 0, vsize); + + if (key == NULL || val == NULL) { + perror("Error: malloc()"); + return 1; + } + + db = nmdb_init(-1); + if (db == NULL) { + perror("nmdb_init() failed"); + return 1; + } + + if (!nmdb_add_server(db, 11) || + !nmdb_add_server(db, 12) || + !nmdb_add_server(db, 13)) { + perror("nmdb_add_server() failed"); + return 1; + } + + timer_start(); + for (i = 0; i < times; i++) { + * (int *) key = i; + * (int *) val = i; + r = nmdb_cache_set(db, key, ksize, val, vsize); + if (r < 0) { + perror("Set"); + return 1; + } + } + s_elapsed = timer_stop(); + + memset(key, 0, ksize); + gval = malloc(128 * 1024); + timer_start(); + for (i = 0; i < times; i++) { + * (int *) key = i; + r = nmdb_cache_get(db, key, ksize, gval, vsize); + if (r < 0) { + perror("Get"); + return 1; + } else if (r == 0) { + misses++; + continue; + } + * (int *) val = i; + if (memcmp((void *) val, (void *) gval, vsize) != 0) { + printf("Values differ for key %s: %s - %s\n", + key, val, gval); + printf("i: %d\n", i); + return 1; + } + } + g_elapsed = timer_stop(); + free(gval); + free(val); + + timer_start(); + for (i = 0; i < times; i++) { + * (int *) key = i; + r = nmdb_cache_del(db, key, ksize); + if (r < 0) { + perror("Del"); + return 1; + } + } + d_elapsed = timer_stop(); + printf("%lu %lu %lu %lu\n", s_elapsed, g_elapsed, d_elapsed, misses); + + free(key); + nmdb_free(db); + + return 0; +} + diff --git a/libnmdb/test2dm.c b/libnmdb/test2dm.c new file mode 100644 index 0000000..4ff5528 --- /dev/null +++ b/libnmdb/test2dm.c @@ -0,0 +1,106 @@ + +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <sys/time.h> +#include <stdlib.h> + +#include "nmdb.h" +#include "timer.h" + + +int main(int argc, char **argv) +{ + int i, r, times; + unsigned char *key, *val; + size_t ksize, vsize; + unsigned long s_elapsed, g_elapsed, d_elapsed, misses = 0; + nmdb_t *db; + + if (argc != 4) { + printf("Usage: test2 TIMES KSIZE VSIZE\n"); + return 1; + } + + times = atoi(argv[1]); + ksize = atoi(argv[2]); + vsize = atoi(argv[3]); + if (times < 1) { + printf("Error: TIMES must be >= 1\n"); + return 1; + } + if (ksize < sizeof(int) || vsize < sizeof(int)) { + printf("Error: KSIZE and VSIZE must be >= sizeof(int)\n"); + return 1; + } + + key = malloc(ksize); + memset(key, 0, ksize); + val = malloc(vsize); + memset(val, 0, vsize); + + if (key == NULL || val == NULL) { + perror("Error: malloc()"); + return 1; + } + + db = nmdb_init(-1); + if (db == NULL) { + perror("nmdb_init() failed"); + return 1; + } + + if (!nmdb_add_server(db, 11) || + !nmdb_add_server(db, 12) || + !nmdb_add_server(db, 13)) { + perror("nmdb_add_server() failed"); + return 1; + } + + timer_start(); + for (i = 0; i < times; i++) { + * (int *) key = i; + * (int *) val = i; + r = nmdb_set(db, key, ksize, val, vsize); + if (r < 0) { + perror("Set"); + return 1; + } + } + s_elapsed = timer_stop(); + + memset(key, 0, ksize); + free(val); + val = malloc(128 * 1024); + timer_start(); + for (i = 0; i < times; i++) { + * (int *) key = i; + r = nmdb_get(db, key, ksize, val, vsize); + if (r < 0) { + perror("Get"); + return 1; + } else if (r == 0) { + misses++; + } + } + g_elapsed = timer_stop(); + free(val); + + timer_start(); + for (i = 0; i < times; i++) { + * (int *) key = i; + r = nmdb_del(db, key, ksize); + if (r < 0) { + perror("Del"); + return 1; + } + } + d_elapsed = timer_stop(); + printf("%lu %lu %lu %lu\n", s_elapsed, g_elapsed, d_elapsed, misses); + + free(key); + nmdb_free(db); + + return 0; +} +