X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Ftst-trie.c;h=df9e1d27bc23de2d33c4493f014adbcb7a640959;hb=7fa8c1bc673add68529fa2bda8134be5089e8745;hp=4202261bb1af5049d3d27aad1bf1dc4771cd75d6;hpb=87ce54fc19930cf850a3926199851ab99c808e92;p=apps%2Fpfixtools.git diff --git a/common/tst-trie.c b/common/tst-trie.c index 4202261..df9e1d2 100644 --- a/common/tst-trie.c +++ b/common/tst-trie.c @@ -33,51 +33,30 @@ * Copyright © 2008 Florent Bruneau */ -#include -#include -#include -#include -#include - +#include +#include #include "common.h" #include "str.h" #include "trie.h" +#include "file.h" static trie_t *create_trie_from_file(const char *file) { trie_t *db; - const char *map, *p, *end; - struct stat st; - int fd; + file_map_t map; + const char *p, *end; char line[BUFSIZ]; - fd = open(file, O_RDONLY, 0000); - if (fd < 0) { - UNIXERR("open"); - return NULL; - } - - if (fstat(fd, &st) < 0) { - UNIXERR("fstat"); - close(fd); + if (!file_map_open(&map, file, false)) { return NULL; } - - p = map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (map == MAP_FAILED) { - UNIXERR("mmap"); - close(fd); - return NULL; - } - close(fd); - - end = map + st.st_size; - while (end > map && end[-1] != '\n') { + p = map.map; + end = map.end; + while (end > p && end[-1] != '\n') { --end; } - if (end != map + st.st_size) { - syslog(LOG_WARNING, "file %s miss a final \\n, ignoring last line", - file); + if (end != map.end) { + warn("file %s miss a final \\n, ignoring last line", file); } db = trie_new(); @@ -102,10 +81,8 @@ static trie_t *create_trie_from_file(const char *file) trie_insert(db, line); p = eol + 1; } - munmap((void*)map, st.st_size); + file_map_close(&map); trie_compile(db, false); - printf("OK\n"); - sleep(10); return db; } @@ -122,7 +99,7 @@ int main(int argc, char *argv[]) trie_insert(trie, "coucou chez vous"); trie_insert(trie, "debout !"); trie_compile(trie, false); - trie_inspect(trie); + trie_inspect(trie, true); #define ASSERT_TRUE(str) \ if (!trie_lookup(trie, str)) { \ @@ -140,6 +117,7 @@ int main(int argc, char *argv[]) ASSERT_FALSE("abcde"); ASSERT_FALSE("coucou chez vous tous"); ASSERT_TRUE("abcde123456789"); + ASSERT_TRUE("abcde123456789"); ASSERT_TRUE("abcde123654789"); ASSERT_TRUE("abcdefghi"); ASSERT_TRUE("coucou"); @@ -152,7 +130,20 @@ int main(int argc, char *argv[]) */ if (argc > 1) { trie = create_trie_from_file(argv[1]); - trie_inspect(trie); + trie_inspect(trie, false); + if (argc > 2) { + const uint32_t how_many = 8 * 1000 * 1000; + struct timeval start, end; + double diff; + + gettimeofday(&start, NULL); + for (uint32_t i = 0 ; i < how_many ; ++i) { + trie_lookup(trie, argv[2]); + } + gettimeofday(&end, NULL); + diff = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 10e6; + printf("%u lookups per second\n", (int)(how_many / diff)); + } trie_delete(&trie); } return 0;