diff options
author | Luca Deri <deri@ntop.org> | 2022-01-16 12:47:33 +0100 |
---|---|---|
committer | Luca Deri <deri@ntop.org> | 2022-01-16 12:47:56 +0100 |
commit | f3af39ee42b954ec0486986c7cfac9ee44cd63e4 (patch) | |
tree | 648a553b6285d807f32a3f9f66ce9f622f912a6a | |
parent | 42d74171b232686d00157dd6b0dcbb81da7ae620 (diff) |
Added performance tests tools
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/include/ndpi_api.h.in | 3 | ||||
-rw-r--r-- | src/lib/ndpi_main.c | 20 | ||||
-rw-r--r-- | tests/performance/Makefile.in | 39 | ||||
-rw-r--r-- | tests/performance/patriciasearch.c | 104 | ||||
-rw-r--r-- | tests/performance/substringsearch.c | 99 |
6 files changed, 263 insertions, 4 deletions
diff --git a/configure.ac b/configure.ac index dc2dc9202..b71a2ddee 100644 --- a/configure.ac +++ b/configure.ac @@ -281,7 +281,7 @@ dnl> ADDITIONAL_LIBS="${ADDITIONAL_LIBS} -lcurl" dnl> AC_DEFINE_UNQUOTED(HAVE_CURL, 1, [curl is present]) dnl> fi -AC_CONFIG_FILES([Makefile example/Makefile example/Makefile.dpdk tests/Makefile tests/unit/Makefile tests/dga/Makefile rrdtool/Makefile libndpi.pc src/include/ndpi_define.h src/lib/Makefile python/Makefile fuzz/Makefile src/include/ndpi_api.h]) +AC_CONFIG_FILES([Makefile example/Makefile example/Makefile.dpdk tests/Makefile tests/unit/Makefile tests/performance/Makefile tests/dga/Makefile rrdtool/Makefile libndpi.pc src/include/ndpi_define.h src/lib/Makefile python/Makefile fuzz/Makefile src/include/ndpi_api.h]) AC_CONFIG_FILES([tests/do.sh], [chmod +x tests/do.sh]) AC_CONFIG_HEADERS(src/include/ndpi_config.h) AC_SUBST(GIT_RELEASE) diff --git a/src/include/ndpi_api.h.in b/src/include/ndpi_api.h.in index 02436b285..f94fd4093 100644 --- a/src/include/ndpi_api.h.in +++ b/src/include/ndpi_api.h.in @@ -118,7 +118,8 @@ extern "C" { void ndpi_free(void *ptr); void * ndpi_flow_malloc(size_t size); void ndpi_flow_free(void *ptr); - + u_int32_t ndpi_get_tot_allocated_memory(); + /** * Search the first occurrence of substring -find- in -s- * The search is limited to the first -slen- characters of the string diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 8d88b5c2e..20df63de6 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -139,9 +139,23 @@ static inline uint8_t flow_is_proto(struct ndpi_flow_struct *flow, u_int16_t p) /* ****************************************** */ +static u_int32_t ndpi_tot_allocated_memory; + +/* ****************************************** */ + +u_int32_t ndpi_get_tot_allocated_memory() { + return(ndpi_tot_allocated_memory); +} + +/* ****************************************** */ + void *ndpi_malloc(size_t size) { + ndpi_tot_allocated_memory += size; return(_ndpi_malloc ? _ndpi_malloc(size) : malloc(size)); } + +/* ****************************************** */ + void *ndpi_flow_malloc(size_t size) { return(_ndpi_flow_malloc ? _ndpi_flow_malloc(size) : ndpi_malloc(size)); } @@ -152,9 +166,11 @@ void *ndpi_calloc(unsigned long count, size_t size) { size_t len = count * size; void *p = ndpi_malloc(len); - if(p) + if(p) { memset(p, 0, len); - + ndpi_tot_allocated_memory += size; + } + return(p); } diff --git a/tests/performance/Makefile.in b/tests/performance/Makefile.in new file mode 100644 index 000000000..9c0bdfefd --- /dev/null +++ b/tests/performance/Makefile.in @@ -0,0 +1,39 @@ +INC=-I ../../src/include/ +LIB=../../src/lib/libndpi.a @ADDITIONAL_LIBS@ + +TOOLS=substringsearch patriciasearch +TESTS=substring_test patricia_test + + +all: $(TESTS) + +tools: $(TOOLS) + + +substringsearch: substringsearch.c Makefile + $(CC) -O2 $(INC) substringsearch.c -o substringsearch $(LIB) + +substring_test: substringsearch top-1m.csv + ./substringsearch + +# + +patriciasearch: patriciasearch.c Makefile + $(CC) -O2 $(INC) patriciasearch.c -o patriciasearch $(LIB) + +patricia_test: patriciasearch blacklist-ip.txt + ./patriciasearch + + +# Alexa Top 1 Million +top-1m.csv: + wget http://s3.amazonaws.com/alexa-static/top-1m.csv.zip + unzip top-1m.csv.zip + +blacklist-ip.txt: + wget http://blacklists.ntop.org/blacklist-ip.txt + +clean: + /bin/rm -f *zip top-1m.csv blacklist-ip.txt + + diff --git a/tests/performance/patriciasearch.c b/tests/performance/patriciasearch.c new file mode 100644 index 000000000..03736769c --- /dev/null +++ b/tests/performance/patriciasearch.c @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2011-22 - ntop.org + * + * nDPI is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nDPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with nDPI. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <assert.h> +#include <math.h> + +#include "ndpi_api.h" + +u_int64_t timeval2usec(const struct timeval *tv) { + return(tv->tv_sec*1000000+tv->tv_usec); +} + +float tdiff2sec(const struct timeval *begin, const struct timeval *end) { + u_int64_t b = timeval2usec(begin); + u_int64_t e = timeval2usec(end); + u_int64_t diff = e - b; + + return((float)diff / 1000000.); +} + + +int main(int argc, char *argv[]) { + const char* top_file = "blacklist-ip.txt"; + FILE *fd = fopen(top_file, "r"); + ndpi_patricia_tree_t *p_v4; + char * line = NULL; + size_t len = 0; + u_int32_t num = 0, num_search = 100, i; + ssize_t read; + struct timeval search, begin, end; + u_int64_t tdiff; + ndpi_prefix_t prefix; + struct in_addr a; + u_int16_t maxbits = 32; /* use 128 for IPv6 */ + + if(fd == NULL) { + printf("Unable to open file %s\n", top_file); + return(-1); + } + + assert(p_v4 = ndpi_patricia_new(32)); + + printf("Building the patricia tree...\n"); + + gettimeofday(&begin, NULL); + + while ((read = getline(&line, &len, fd)) != -1) { + ndpi_patricia_node_t *node; + u_int len = strlen(line); + + line[len] = '\0'; /* Remove trailer \n */ + + a.s_addr = inet_addr(line); + ndpi_fill_prefix_v4(&prefix, &a, 32, maxbits); + assert(ndpi_patricia_lookup(p_v4, &prefix) != NULL /* node added */); + num++; + } + + fclose(fd); + + gettimeofday(&search, NULL); + + printf("Patricia tree (IPv4) with %u IP prefixes built successfully in %.2f sec [%.1f MB]\n", + num, tdiff2sec(&begin, &search), + (float)ndpi_get_tot_allocated_memory()/(float)(1024*1024)); + + gettimeofday(&search, NULL); + +#if !defined(SEARCH_LAST_IP_ADDED) + /* Nothing to do */ +#else + a.s_addr = inet_addr("1.2.3.4"); + ndpi_fill_prefix_v4(&prefix, &a, 32, maxbits); +#endif + + for(i=0; i<num_search; i++) + assert(ndpi_patricia_search_best(p_v4, &prefix)); + gettimeofday(&end, NULL); + + printf("String searched in %.2f usec\n", + (float)(timeval2usec(&end) - timeval2usec(&search))/(float)num_search); + + ndpi_patricia_destroy(p_v4, NULL); + + return(0); +} diff --git a/tests/performance/substringsearch.c b/tests/performance/substringsearch.c new file mode 100644 index 000000000..be4f61e4f --- /dev/null +++ b/tests/performance/substringsearch.c @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2011-22 - ntop.org + * + * nDPI is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nDPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with nDPI. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <assert.h> +#include <math.h> + +#include "ndpi_api.h" + +u_int64_t timeval2usec(const struct timeval *tv) { + return(tv->tv_sec*1000000+tv->tv_usec); +} + +float tdiff2sec(const struct timeval *begin, const struct timeval *end) { + u_int64_t b = timeval2usec(begin); + u_int64_t e = timeval2usec(end); + u_int64_t diff = e - b; + + return((float)diff / 1000000.); +} + + +int main(int argc, char *argv[]) { + const char* top_file = "top-1m.csv"; + FILE *fd = fopen(top_file, "r"); + void *automa = ndpi_init_automa(); + char * line = NULL; + size_t len = 0; + u_int32_t num = 0, num_search = 100, i; + ssize_t read; + struct timeval search, begin, end; + u_int64_t tdiff; + + if(fd == NULL) { + printf("Unable to open file %s\n", top_file); + return(-1); + } + assert(automa); + + printf("Building the automa...\n"); + + gettimeofday(&begin, NULL); + + while ((read = getline(&line, &len, fd)) != -1) { + char *t = strtok(line, ","); + + if(t) t = strtok(NULL, "\n"); + + if(t != NULL) { + u_int len = strlen(t); + + t[len] = '\0'; + + assert(ndpi_add_string_to_automa(automa, ndpi_strdup(t)) == 0); + num++; + + if(num == 100000) break; + } + } + + fclose(fd); + + ndpi_finalize_automa(automa); + + gettimeofday(&search, NULL); + + printf("Automa with %u words built successfully in %.1f sec [%.1f MB]\n", + num, tdiff2sec(&begin, &search), + (float)ndpi_get_tot_allocated_memory()/(float)(1024*1024)); + + gettimeofday(&search, NULL); + for(i=0; i<num_search; i++) + assert(ndpi_match_string(automa, "github.com") == 1); + gettimeofday(&end, NULL); + + printf("String searched in %.2f usec\n", + (float)(timeval2usec(&end) - timeval2usec(&search))/(float)num_search); + + ndpi_free_automa(automa); + + return(0); +} |