#include "Maat_rule.h" #include "bool_matcher.h" #include "stream_fuzzy_hash.h" #include "Maat_command.h" #include using namespace std; #include #include #include #include int global_thread_num=4; struct bool_expr_wrapper { struct bool_expr expr; TAILQ_ENTRY(bool_expr_wrapper) entries; }; TAILQ_HEAD(bool_expr_q, bool_expr_wrapper); TEST(BoolMatcher, Match) { struct bool_matcher * bm=NULL; struct bool_expr *expr_array=NULL; struct bool_expr_wrapper *p=NULL; bool_expr_q expr_queue; unsigned long long i=0; TAILQ_INIT(&expr_queue); const char* bool_expr_filename="./testdata/bool-matcher-test-exprs.txt"; char line[512]={0}; int ret=0, expr_num=0; FILE* fp=fopen(bool_expr_filename, "r"); memset(line, 0, sizeof(line)); while(NULL!=fgets(line,sizeof(line),fp)) { if(line[0]=='#'||line[0]==' '||line[0]=='\t'||strlen(line)<4) { continue; } p=(struct bool_expr_wrapper*)calloc(sizeof(struct bool_expr_wrapper), 1); ret=sscanf(line, "%lld %lld %lld %lld %lld %lld %lld %lld %lld", &p->expr.expr_id, &p->expr.items[0].item_id, &p->expr.items[1].item_id, &p->expr.items[2].item_id, &p->expr.items[3].item_id, &p->expr.items[4].item_id, &p->expr.items[5].item_id, &p->expr.items[6].item_id, &p->expr.items[7].item_id); if(ret<2) { free(p); continue; } p->expr.item_num=ret-1; p->expr.user_tag=NULL; TAILQ_INSERT_TAIL(&expr_queue, p, entries); expr_num++; memset(line, 0, sizeof(line)); } fclose(fp); expr_array=(struct bool_expr*)malloc(sizeof(struct bool_expr)*expr_num); p=TAILQ_FIRST(&expr_queue); while(p != NULL) { TAILQ_REMOVE(&expr_queue, p, entries); memcpy(expr_array+i, &(p->expr), sizeof(p->expr)); free(p); p = TAILQ_FIRST(&expr_queue); i++; } size_t mem_size=0; bm=bool_matcher_new(expr_array, expr_num, 4, &mem_size); unsigned long long test_count=2*1000*1000, match_count=0, unmatch_count=0; long int j=0; size_t k=0; unsigned long long input_item_ids[256], time_elapse_ms=0, scan_per_second=0; size_t input_item_num=0; struct bool_expr_match result_array[1024]; srand(19); struct timespec start,end; clock_gettime(CLOCK_MONOTONIC, &start); for(i=0; i0) { match_count++; } } EXPECT_EQ(match_count, test_count); input_item_ids[0]=123; input_item_ids[1]=124; input_item_ids[2]=125; input_item_ids[3]=7; input_item_ids[4]=3; input_item_ids[5]=128; input_item_ids[6]=129; input_item_ids[7]=130; input_item_ids[8]=131; input_item_ids[9]=132; input_item_ids[10]=133; input_item_ids[11]=777; input_item_ids[12]=999; input_item_ids[13]=788; input_item_ids[14]=222; input_item_ids[15]=333; input_item_num=8; sort(input_item_ids, input_item_ids+input_item_num); for(i=0; iaddrtype=ADDR_TYPE_IPV4; inet_pton(AF_INET, src_ip, &(v4_addr->saddr)); v4_addr->source=htons(sport); inet_pton(AF_INET, dest_ip, &(v4_addr->daddr)); v4_addr->dest=htons(dport); ipv4_addr->v4=v4_addr; return; } void random_fqdn_generate(char* fqdn_buff, size_t sz) { size_t min_fqdn_len=6, max_fqdn_len=32; size_t i=0, j=0, len=0; len=random()%(max_fqdn_len-min_fqdn_len)+min_fqdn_len; if(len>=sz) { len=sz-1; } for(i=0; i5) { if(random()%3==0) { fqdn_buff[i]='.'; j=0; } } j++; } fqdn_buff[i]='\0'; } void random_keyword_generate(char* keyword_buf, size_t sz) { #define MIN_KEYWORD_LEN 4 size_t i=0, len=0; len=random()%(sz-1-MIN_KEYWORD_LEN)+MIN_KEYWORD_LEN; for(i=0; ifeather; const char* expr_table_name=para->table_name; struct Maat_rule_t result; scan_status_t mid=NULL; int table_id=0, i=0, ret=0, hit_times=0; table_id=Maat_table_register(feather, expr_table_name); memset(&result, 0, sizeof(result)); struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); for(i=0; itest_count; i++) { ret=Maat_full_scan_string(feather, table_id, CHARSET_GBK, high_match_string, strlen(high_match_string), &result, NULL, 1, &mid, para->thread_id); if(ret==-2) { hit_times++; } Maat_clean_status(&mid); } clock_gettime(CLOCK_MONOTONIC, &end); para->time_elapse_ms=(end.tv_sec-start.tv_sec)*1000+(end.tv_nsec-start.tv_nsec)/1000000; int* is_all_hit=(int*)malloc(sizeof(int)); *is_all_hit=(hit_times==para->test_count)?1:0; return is_all_hit; } void* high_match_rate_update_thread(void *arg) { struct thread_para* para=(struct thread_para*)arg; Maat_feather_t feather=para->feather; const int CMD_EXPR_NUM=10; int config_id=0; config_id=(int)Maat_cmd_incrby(feather, "TEST_SEQ", CMD_EXPR_NUM); config_id-=CMD_EXPR_NUM; int i=0; char keyword_buf[128]; struct Maat_command_batch* batch=NULL; batch=Maat_command_batch_new(feather); for(i=0; itable_name, config_id+i, keyword_buf, 1); sleep(1); } int* is_all_hit=(int*)malloc(sizeof(int)); *is_all_hit=1; return is_all_hit; } TEST_F(MaatCMDPerfTest, HighMatchRateOnMultiThread) { const char* expr_table_name="HTTP_URL"; const int CMD_EXPR_NUM=2*1000; int srand_keyword=717; int config_id=0; int* is_all_hit=NULL; Maat_feather_t feather=MaatCMDPerfTest::_shared_feather; config_id=(int)Maat_cmd_incrby(feather, "TEST_SEQ", CMD_EXPR_NUM); config_id-=CMD_EXPR_NUM; int i=0; char keyword_buf[128]; struct Maat_command_batch* batch=NULL; batch=Maat_command_batch_new(feather); srand(srand_keyword); for(i=0; irule_id=atoi(table_line+column_offset); ret=Maat_helper_read_column(table_line, 5, &column_offset, &column_len); EXPECT_EQ(ret, 0); ud->buffer=(char*)calloc(sizeof(char), column_len+1); strncpy(ud->buffer, table_line+column_offset, column_len); ud->ref_cnt=1; *ad=ud; (*counter)++; return; } void ip_plugin_EX_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp) { struct ip_plugin_ud* u=(struct ip_plugin_ud*)(*ad); if ((__sync_sub_and_fetch(&u->ref_cnt, 1) == 0)) { free(u->buffer); free(u); *ad=NULL; } } void ip_plugin_EX_dup_cb(int table_id, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *from, long argl, void *argp) { struct ip_plugin_ud* u=(struct ip_plugin_ud*)(*from); __sync_add_and_fetch(&(u->ref_cnt), 1); *to=u; } TEST_F(MaatCMDPerfTest, UpdateIPPlugin) { #define IP_Plugin_EX_data Maat_feather_t feather=MaatCMDPerfTest::_shared_feather; int ret=0, i=0; int table_id=0, ip_plugin_ex_data_counter=0; const char* table_name="TEST_IP_PLUGIN_WITH_EXDATA"; const int TEST_CMD_LINE_NUM=4; const struct Maat_cmd_line *p_line[TEST_CMD_LINE_NUM]; struct Maat_cmd_line line_rule[TEST_CMD_LINE_NUM]; const char* table_line[TEST_CMD_LINE_NUM]={ "101\t4\t192.168.30.99\t192.168.30.101\tSomething-like-json\t1", "102\t4\t192.168.30.90\t192.168.30.128\tBigger-range-should-in-the-back\t1", "103\t6\t2001:db8:1234::\t2001:db8:1235::\tBigger-range-should-in-the-back\t1", "104\t6\t2001:db8:1234::1\t2001:db8:1234::5210\tSomething-like-json\t1" }; table_id=Maat_table_register(feather, table_name); ASSERT_GT(table_id, 0); memset(&line_rule,0,sizeof(line_rule)); for(i=0;i=0); EXPECT_EQ(ip_plugin_ex_data_counter, 4); struct ip_address ipv4, ipv6; struct ip_plugin_ud* result[4]; ipv4.ip_type=4; inet_pton(AF_INET, "192.168.30.100", &(ipv4.ipv4)); memset(&result, 0, sizeof(result)); ret=Maat_ip_plugin_get_EX_data(feather, table_id, &ipv4, (void**)result, 4); ASSERT_EQ(ret, 2); EXPECT_EQ(result[0]->rule_id, 101); EXPECT_EQ(result[1]->rule_id, 102); for(i=0; irule_id, 104); EXPECT_EQ(result[1]->rule_id, 103); for(i=0; irule_id=atoi(table_line+column_offset); ret=Maat_helper_read_column(table_line, 4, &column_offset, &column_len); EXPECT_EQ(ret, 0); sscanf(table_line+column_offset, "catid=%d",&ud->catid); ud->ref_cnt=1; *ad=ud; (*counter)++; return; } void perf_fqdn_plugin_EX_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp) { struct perf_fqdn_plugin_ud* u=(struct perf_fqdn_plugin_ud*)(*ad); if ((__sync_sub_and_fetch(&u->ref_cnt, 1) == 0)) { free(u); *ad=NULL; } } void perf_fqdn_plugin_EX_dup_cb(int table_id, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *from, long argl, void *argp) { struct perf_fqdn_plugin_ud* u=(struct perf_fqdn_plugin_ud*)(*from); __sync_add_and_fetch(&(u->ref_cnt), 1); *to=u; } void* fqdn_plugin_scan_thread(void *arg) { struct thread_para* para=(struct thread_para*)arg; Maat_feather_t feather=para->feather; struct perf_fqdn_plugin_ud* result[4]; int table_id=0, i=0, j=0, ret=0, hit_times=0; table_id=Maat_table_register(feather, para->table_name); memset(&result, 0, sizeof(result)); struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); for(i=0; itest_count; i++) { ret=Maat_fqdn_plugin_get_EX_data(feather, table_id, "r3---sn-i3belne6.example2.com", (void**)result, 4); if(ret==2) { hit_times++; } for(j=0; jtime_elapse_ms=(end.tv_sec-start.tv_sec)*1000+(end.tv_nsec-start.tv_nsec)/1000000; int* is_all_hit=(int*)malloc(sizeof(int)); *is_all_hit=(hit_times==para->test_count)?1:0; return is_all_hit; } void* fqdn_plugin_update_thread(void *arg) { struct thread_para* para=(struct thread_para*)arg; Maat_feather_t feather=para->feather; const int CMD_EXPR_NUM=20; int i=0; struct Maat_cmd_line line_rule; char line_buff[1024], fqdn_buff[256]; for(i=0; itable_name; random_fqdn_generate(fqdn_buff, sizeof(fqdn_buff)); snprintf(line_buff, 1024, "%d\t1\t%s\tcatid=4\t1", line_rule.rule_id, fqdn_buff); line_rule.table_line=line_buff; line_rule.expire_after=0; Maat_cmd_set_line(feather, &line_rule, MAAT_OP_ADD); sleep(1); } int* is_all_hit=(int*)malloc(sizeof(int)); *is_all_hit=1; return is_all_hit; } TEST_F(MaatCMDPerfTest, FQDNPluginOnMultiThread) { #define FQDN_Plugin_EX_data Maat_feather_t feather=MaatCMDPerfTest::_shared_feather; int* is_all_hit=NULL; int ret=0; int i=0, j=0; int table_id=0, fqdn_plugin_ex_data_counter=0; const char* table_name="TEST_FQDN_PLUGIN_WITH_EXDATA"; #define FIXED_LINE_CNT 5 int randomed_line_cnt=100*1000; int total_line_cnt=FIXED_LINE_CNT+randomed_line_cnt; const struct Maat_cmd_line **p_line=(const struct Maat_cmd_line**)calloc(sizeof(struct Maat_cmd_line*), total_line_cnt); struct Maat_cmd_line *line_rule=(struct Maat_cmd_line*)calloc(sizeof(struct Maat_cmd_line), total_line_cnt); const char* fixed_table_line[FIXED_LINE_CNT]={ "201\t0\twww.example1.com\tcatid=1\t1", "202\t1\t.example1.com\tcatid=1\t1", "203\t0\tnews.example1.com\tcatid=2\t1", "204\t0\tr3---sn-i3belne6.example2.com\tcatid=3\t1", "205\t0\tr3---sn-i3belne6.example2.com\tcatid=3\t1" }; char fqdn_buff[256]; char** random_lines=(char**)calloc(sizeof(char*), randomed_line_cnt); table_id=Maat_table_register(feather, table_name); ASSERT_GT(table_id, 0); srand(521); Maat_cmd_incrby(feather,"TEST_PLUG_SEQ", 1000);//jump over IDs of fixed table line. for(i=0, j=0; i=0); EXPECT_EQ(fqdn_plugin_ex_data_counter, total_line_cnt); struct perf_fqdn_plugin_ud* result[4]; ret=Maat_fqdn_plugin_get_EX_data(feather, table_id, "r3---sn-i3belne6.example2.com", (void**)result, 4); EXPECT_EQ(ret, 2); for(i=0; (int)irule_id=atoi(table_line+column_offset); ret=Maat_helper_read_column(table_line, 5, &column_offset, &column_len); EXPECT_EQ(ret, 0); ud->ref_cnt=1; *ad=ud; (*counter)++; return; } void perf_ip_plugin_EX_free_cb(int table_id, MAAT_PLUGIN_EX_DATA* ad, long argl, void *argp) { struct perf_ip_plugin_ud* u=(struct perf_ip_plugin_ud*)(*ad); if ((__sync_sub_and_fetch(&u->ref_cnt, 1) == 0)) { free(u); *ad=NULL; } } void perf_ip_plugin_EX_dup_cb(int table_id, MAAT_PLUGIN_EX_DATA *to, MAAT_PLUGIN_EX_DATA *from, long argl, void *argp) { struct perf_ip_plugin_ud* u=(struct perf_ip_plugin_ud*)(*from); __sync_add_and_fetch(&(u->ref_cnt), 1); *to=u; } static void* ip_plugin_get_thread(void* arg) { const char* table_name="TSG_IP_LOCATION_BUILT_IN"; int test_times=1000*1000, hit_times=0; int table_id=0; int ret=0, i=0, j=0; Maat_feather_t feather=(Maat_feather_t)arg; table_id=Maat_table_register(feather, table_name); struct perf_ip_plugin_ud* result[4]; struct ip_address ip; ip.ip_type=4; inet_pton(AF_INET, "191.70.72.1", &(ip.ipv4)); for(i=0; i0) { hit_times++; } for(j=0; j=0); pthread_t threads[global_thread_num]; for(i=0; i