#include <gtest/gtest.h>

#include "log/log.h"
#include "maat.h"
#include "maat_rule.h"
#include "maat_utils.h"
#include "ip_matcher.h"
#include "maat_command.h"
#include "json2iris.h"
#include "maat_config_monitor.h"

#define MODULE_FRAMEWORK_PERF_GTEST	 module_name_str("maat.framework_perf_gtest")

#define ARRAY_SIZE              10
#define	WAIT_FOR_EFFECTIVE_S	2
#define PERF_THREAD_NUM         5

const char *table_info_path = "./table_info.conf";
const char *json_filename = "maat_json.json";

struct thread_param {
	int thread_id;
	int test_count;
	struct maat *maat_instance;
	const char *table_name;
	long long time_elapse_ms;
    struct log_handle *logger;
};

void random_keyword_generate(char *keyword_buf, size_t sz)
{
#define MIN_KEYWORD_LEN	4
    size_t i = 0;
	size_t len = random() % (sz - 1 - MIN_KEYWORD_LEN) + MIN_KEYWORD_LEN;
	for (i = 0; i < len; i++) {
		keyword_buf[i] = 'a' + random() % ('z' - 'a');
	}
	keyword_buf[i] = '\0';
}

int count_line_num_cb(const char *table_name, const char *line, void *u_para)
{
	(*((unsigned int *)u_para))++;
	return 0;
}

int line_idx = 0;
long long absolute_expire_time=0;
int make_serial_rule(const char *table_name, const char *line, void *u_para)
{
	struct serial_rule *s_rule=(struct serial_rule *)u_para;
	int rule_id = 0;
	char *buff = ALLOC(char, strlen(line) + 1);
    
    memcpy(buff, line, strlen(line) + 1);

	while (buff[strlen(buff) - 1] == '\n' || buff[strlen(buff) - 1] == '\t') {
		buff[strlen(buff) - 1] = '\0';
	}

    int j = 0;
    char *str1 = NULL;
    char *token = NULL;
    char *saveptr1 = NULL;

	for (j = 0,str1 = buff; ; j++, str1 = NULL) {
		 token = strtok_r(str1, "\t ", &saveptr1);
		 if (token == NULL)
			 break;
		 if (j == 0) {
			sscanf(token,"%d", &rule_id);
		 }
 	}
	
    memcpy(buff, line, strlen(line)+1);
	while(buff[strlen(buff)-1]=='\n'||buff[strlen(buff)-1]=='\t') {
		buff[strlen(buff)-1]='\0';
	}

	maat_cmd_set_serial_rule(s_rule + line_idx, MAAT_OP_ADD, rule_id, table_name, buff, absolute_expire_time);
	line_idx++;

    FREE(buff);

	return 0;
}

static int write_config_to_redis(char *redis_ip, int redis_port, int redis_db,
                          struct log_handle *logger)
{
    char json_iris_path[512] = {0};

    snprintf(json_iris_path, sizeof(json_iris_path), "./%s_iris_tmp", json_filename);

    redisContext *c = maat_cmd_connect_redis(redis_ip, redis_port, redis_db, logger);
    if (NULL == c) {
        return -1;
    }

    redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb");
    if (NULL == reply) {
        return -1;
    } else {
        freeReplyObject(reply);
	    reply = NULL;
    }
    
    if (0 == access(json_iris_path, F_OK)) {
        system_cmd_rmdir(json_iris_path);
    }

    if (access(json_iris_path, F_OK) < 0) {
        char tmp_iris_path[128] = {0};
        char *json_buff = NULL;
        size_t json_buff_sz = 0;
        
        int ret = load_file_to_memory(json_filename, (unsigned char **)&json_buff, &json_buff_sz);
        if (ret < 0) {
            return -1;
        }

        ret = json2iris(json_buff, json_filename, c, tmp_iris_path, 
                        sizeof(tmp_iris_path), NULL, NULL, logger);
        FREE(json_buff);
        if (ret < 0) {
            return -1;
        }
    }

    size_t total_line_cnt = 0;
    char tmp_iris_full_idx_path[PATH_MAX] = {0};
    snprintf(tmp_iris_full_idx_path, sizeof(tmp_iris_full_idx_path), "%s/index", json_iris_path);
    config_monitor_traverse(0, tmp_iris_full_idx_path, NULL, count_line_num_cb, NULL,
                            &total_line_cnt, NULL, logger);

    struct serial_rule *s_rule = ALLOC(struct serial_rule, total_line_cnt);
    long long server_time = maat_cmd_redis_server_time_s(c);
    if (server_time < 0) {
        return -1;
    }

    absolute_expire_time = server_time + 300;
    config_monitor_traverse(0, tmp_iris_full_idx_path, NULL, make_serial_rule, NULL,
                            s_rule, NULL, logger);
    line_idx = 0;
    absolute_expire_time = 0;

    int success_cnt = 0;
    do {
        success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt, server_time, logger);
    } while (success_cnt < 0);

    EXPECT_EQ(success_cnt, (int)total_line_cnt);

    for (size_t i = 0; i < total_line_cnt; i++) {
        maat_cmd_clear_rule_cache(s_rule + i);
    }
    FREE(s_rule);
    redisFree(c);

    return 0;
}

static int compile_table_set_line(struct maat *maat_instance, const char *table_name,
                                  enum maat_operation op, long long compile_id,
                                  const char *user_region, int clause_num, int expire_after)
{
    char table_line[1024 * 16] = {0};
    sprintf(table_line, "%lld\t0\t0\t0\t0\t0\t%s\t%d\t%d\t0.0", 
            compile_id, user_region, op, clause_num);

    struct maat_cmd_line line_rule;
    line_rule.rule_id = compile_id;
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_instance, &line_rule);
}

static int group2compile_table_set_line(struct maat *maat_instance, const char *table_name,
                                        enum maat_operation op, long long group_id, long long compile_id,
                                        int not_flag, const char *vtable_name, int clause_num,
                                        int expire_after)
{
    char table_line[128] = {0};
    sprintf(table_line, "%lld\t%lld\t%d\t%d\t%s\t%d", 
            group_id, compile_id, op, not_flag, vtable_name, clause_num);

    struct maat_cmd_line line_rule;
    line_rule.rule_id = group_id;
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_instance, &line_rule);
}

static int expr_table_set_line(struct maat *maat_instance, const char *table_name, enum maat_operation op,
                               long long item_id, long long group_id, const char *keywords, const char *district,
                               int expr_type, int match_method, int is_hexbin, int expire_after)
{
    char table_line[1024] = {0};
    int table_id = maat_get_table_id(maat_instance, table_name);
    if (table_id < 0) {
        return 0;
    }
    
    enum table_type table_type = table_manager_get_table_type(maat_instance->tbl_mgr, table_id);
    assert(table_type == TABLE_TYPE_EXPR || table_type == TABLE_TYPE_EXPR_PLUS);

    if (table_type == TABLE_TYPE_EXPR_PLUS) {
        sprintf(table_line, "%lld\t%lld\t%s\t%s\t%d\t%d\t%d\t%d", item_id, group_id, district,
                keywords, expr_type, match_method, is_hexbin, op);
    } else {
        sprintf(table_line, "%lld\t%lld\t%s\t%d\t%d\t%d\t%d", item_id, group_id, keywords, expr_type,
            match_method, is_hexbin, op);
    }

    struct maat_cmd_line line_rule;
    line_rule.rule_id = item_id;
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_instance, &line_rule);
}

static int ip_table_set_line(struct maat *maat_instance, const char *table_name, enum maat_operation op,
                             long long item_id, long long group_id, enum IP_TYPE type, const char *ip1, 
                             const char *ip2, uint16_t port_min, uint16_t port_max, int expire_after)
{
    char table_line[1024] = {0};
    int table_id = maat_get_table_id(maat_instance, table_name);
    if (table_id < 0) {
        return 0;
    }

    int ip_type = 4;
    if (type == IPv6) {
        ip_type = 6;
    }

    sprintf(table_line, "%lld\t%lld\t%d\t%s\t%s\t%s\t%s\t%u\t%u\t6\t%d", 
            item_id, group_id, ip_type, "range", ip1, ip2, 
            "range", port_min, port_max, op);
    struct maat_cmd_line line_rule;
    
    line_rule.rule_id = item_id;
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_instance, &line_rule);
}

void test_add_expr_command(struct maat *maat_instance, const char *table_name, 
                           const char *keywords)
{
    long long compile_id = maat_cmd_incrby(maat_instance, "TEST_SEQ", 1);
    int ret = compile_table_set_line(maat_instance, "COMPILE", MAAT_OP_ADD, compile_id, "null", 1, 0);
    EXPECT_EQ(ret, 1);

    long long group_id = maat_cmd_incrby(maat_instance, "SEQUENCE_GROUP", 1);
    ret = group2compile_table_set_line(maat_instance, "GROUP2COMPILE", MAAT_OP_ADD, group_id,
                                       compile_id, 0, "null", 1, 0);
    EXPECT_EQ(ret, 1);

    long long item_id = maat_cmd_incrby(maat_instance, "SEQUENCE_REGION", 1);
    ret = expr_table_set_line(maat_instance, table_name, MAAT_OP_ADD, item_id, group_id,
                              keywords, NULL, 1, 0, 0, 0);
    EXPECT_EQ(ret, 1);
}

void test_add_ip_command(struct maat *maat_instance, const char *table_name, 
                         const char *ip, uint16_t port)
{
    long long compile_id = maat_cmd_incrby(maat_instance, "TEST_SEQ", 1);
    int ret = compile_table_set_line(maat_instance, "COMPILE", MAAT_OP_ADD, compile_id, "null", 1, 0);
    EXPECT_EQ(ret, 1);

    long long group_id = maat_cmd_incrby(maat_instance, "SEQUENCE_GROUP", 1);
    ret = group2compile_table_set_line(maat_instance, "GROUP2COMPILE", MAAT_OP_ADD, group_id,
                                       compile_id, 0, "null", 1, 0);
    EXPECT_EQ(ret, 1);

    long long item_id = maat_cmd_incrby(maat_instance, "SEQUENCE_REGION", 1);
    ret = ip_table_set_line(maat_instance, table_name, MAAT_OP_ADD, item_id, group_id,
                            IPv4, ip, ip, port, port, 0);
    EXPECT_EQ(ret, 1);
}

class MaatPerfStringScan : public testing::Test
{
protected:
    static void SetUpTestCase() {
        const char *accept_tags = "{\"tags\":[{\"tag\":\"location\",\"value\":\"北京/朝阳/华严北里/甲22号\"},{\"tag\":\"isp\",\"value\":\"移动\"},{\"tag\":\"location\",\"value\":\"Astana\"}]}";
        char redis_ip[64] = "127.0.0.1";
        int redis_port = 6379;
        int redis_db = 0;

        logger = log_handle_create("./maat_framework_perf_gtest.log", 0);        
        int ret = write_config_to_redis(redis_ip, redis_port, redis_db, logger);
        if (ret < 0) {
            log_error(logger, MODULE_FRAMEWORK_PERF_GTEST,
                      "[%s:%d] write config to redis failed.", __FUNCTION__, __LINE__);
        }

        struct maat_options *opts = maat_options_new();
        maat_options_set_redis(opts, redis_ip, redis_port, redis_db);
        maat_options_set_logger(opts, "./maat_framework_gtest.log", LOG_LEVEL_INFO);
        maat_options_set_accept_tags(opts, accept_tags);
        maat_options_set_caller_thread_number(opts, 5);

        _shared_maat_instance = maat_new(opts, table_info_path);
        maat_options_free(opts);
        if (NULL == _shared_maat_instance) {
            log_error(logger, MODULE_FRAMEWORK_PERF_GTEST,
                      "[%s:%d] create maat instance in MaatFlagScan failed.",
                      __FUNCTION__, __LINE__);
        }
    }

    static void TearDownTestCase() {
        maat_free(_shared_maat_instance);
        log_handle_destroy(logger);
    }

    static struct log_handle *logger;
    static struct maat *_shared_maat_instance;
};

struct maat *MaatPerfStringScan::_shared_maat_instance;
struct log_handle *MaatPerfStringScan::logger;

void *perf_string_scan_thread(void *arg)
{
    struct thread_param *param = (struct thread_param *)arg;
    struct maat *maat_instance = param->maat_instance;
    const char *table_name = param->table_name;
    struct timespec start, end;
    const char *scan_data = "String TEST should hit";
    long long results[ARRAY_SIZE] = {0};
    int hit_times = 0;
    size_t n_hit_result = 0;
    struct maat_state *state = maat_state_new(maat_instance, param->thread_id);

    int table_id = maat_get_table_id(maat_instance, table_name);
    
    clock_gettime(CLOCK_MONOTONIC, &start);
    for (int i = 0; i < param->test_count; i++) {
        int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
                                   results, ARRAY_SIZE, &n_hit_result, state);
        if (ret == MAAT_SCAN_HIT) {
            hit_times++;
        }
        maat_state_reset(state);
    }
    clock_gettime(CLOCK_MONOTONIC, &end);

    param->time_elapse_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000;
    int *is_all_hit = ALLOC(int, 1);
    *is_all_hit = (hit_times == param->test_count ? 1 : 0);
    log_info(param->logger, MODULE_FRAMEWORK_PERF_GTEST, 
             "thread_id:%d time_elapse:%lldms hit_times:%d", 
             param->thread_id, param->time_elapse_ms, hit_times);
    return is_all_hit;
}

void *perf_string_update_thread(void *arg)
{
    struct thread_param *param = (struct thread_param *)arg;
    struct maat *maat_instance = param->maat_instance;
    const char *table_name = param->table_name;
    const int CMD_EXPR_NUM = 10;
    char keyword_buf[128];

    for (int i = 0; i < CMD_EXPR_NUM; i++) {
        random_keyword_generate(keyword_buf, sizeof(keyword_buf));
        test_add_expr_command(maat_instance, table_name, keyword_buf);
        sleep(1);
    }

    int *is_all_hit = ALLOC(int, 1);
    *is_all_hit = 1;
    
    return is_all_hit;
}

void *perf_ip_scan_thread(void *arg)
{
    struct thread_param *param = (struct thread_param *)arg;
    struct maat *maat_instance = param->maat_instance;
    const char *table_name = param->table_name;
    struct timespec start, end;
    char ip_str[32] = "10.0.7.100";
    uint32_t ip_addr;
    uint16_t port = htons(65530);

    int ret = inet_pton(AF_INET, ip_str, &ip_addr);
    EXPECT_EQ(ret, 1);
    
    int hit_times = 0;
    long long results[ARRAY_SIZE] = {0};
    size_t n_hit_result = 0;
    struct maat_state *state = maat_state_new(maat_instance, param->thread_id);
    int table_id = maat_get_table_id(maat_instance, table_name);

    clock_gettime(CLOCK_MONOTONIC, &start);
    for (int i = 0; i < param->test_count; i++) {
        int ret = maat_scan_ipv4(maat_instance, table_id, ip_addr, port, 6,
                                 results, ARRAY_SIZE, &n_hit_result, state);
        if (ret == MAAT_SCAN_HIT) {
            hit_times++;
        }
        maat_state_reset(state);
    }
    clock_gettime(CLOCK_MONOTONIC, &end);

    param->time_elapse_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000;
    int *is_all_hit = ALLOC(int, 1);
    *is_all_hit = (hit_times == param->test_count ? 1 : 0);
    log_info(param->logger, MODULE_FRAMEWORK_PERF_GTEST, 
             "thread_id:%d time_elapse:%lldms hit_times:%d", 
             param->thread_id, param->time_elapse_ms, hit_times);
    return is_all_hit;
}

void *perf_ip_update_thread(void *arg)
{
    struct thread_param *param = (struct thread_param *)arg;
    struct maat *maat_instance = param->maat_instance;
    const char *table_name = param->table_name;
    const int CMD_EXPR_NUM = 10;
    char ip_str[10][32] = {
                            "10.0.7.101",
                            "10.0.7.102",
                            "10.0.7.103",
                            "10.0.7.104",
                            "10.0.7.105",
                            "10.0.7.106",
                            "10.0.7.107",
                            "10.0.7.108",
                            "10.0.7.109",
                            "10.0.7.110"};

    uint16_t port = 65530;
    for (int i = 0; i < CMD_EXPR_NUM; i++) {
        test_add_ip_command(maat_instance, table_name, ip_str[i], port);
        sleep(1);
    }

    int *is_all_hit = ALLOC(int, 1);
    *is_all_hit = 1;
    
    return is_all_hit;
}

TEST_F(MaatPerfStringScan, basic) {
    const char *table_name = "KEYWORDS_TABLE";
    struct maat *maat_instance = MaatPerfStringScan::_shared_maat_instance;

    int table_id = maat_get_table_id(maat_instance, table_name);
    ASSERT_GT(table_id, 0);

    pthread_t threads[PERF_THREAD_NUM + 1];
    struct thread_param thread_params[PERF_THREAD_NUM + 1];
    int i = 0;
    int *is_all_hit = NULL;

    for (i = 0; i < PERF_THREAD_NUM + 1; i++) {
        thread_params[i].maat_instance = maat_instance;
        thread_params[i].thread_id = i;
        thread_params[i].table_name = table_name;
        thread_params[i].test_count = 1 * 1000 * 1000;
        thread_params[i].time_elapse_ms = 0;
        thread_params[i].logger = logger;

        if (i < PERF_THREAD_NUM) {
            pthread_create(&threads[i], NULL, perf_string_scan_thread, thread_params+i);
        } else {
            thread_params[i].test_count = 0;
            pthread_create(&threads[i], NULL, perf_string_update_thread, thread_params+i);
        }
    }

    long long time_elapse_ms = 0;
    long long scan_count = 0;
    long long scan_per_second = 0;
    for (i = 0; i < PERF_THREAD_NUM + 1; i++) {
        pthread_join(threads[i], (void **)&is_all_hit);
        time_elapse_ms += thread_params[i].time_elapse_ms;
		scan_count += thread_params[i].test_count;

		EXPECT_EQ(*is_all_hit, 1);
		*is_all_hit = 0;
		free(is_all_hit);
    }
    scan_per_second = scan_count * 1000 / time_elapse_ms;
    //EXPECT_GT(scan_per_second, 800 * 1000);
    printf("High match rate on %d-threads speed %lld lookups/s/thread\n", PERF_THREAD_NUM, scan_per_second);
}

class MaatPerfIPScan : public testing::Test
{
protected:
    static void SetUpTestCase() {
        const char *accept_tags = "{\"tags\":[{\"tag\":\"location\",\"value\":\"北京/朝阳/华严北里/甲22号\"},{\"tag\":\"isp\",\"value\":\"移动\"},{\"tag\":\"location\",\"value\":\"Astana\"}]}";
        char redis_ip[64] = "127.0.0.1";
        int redis_port = 6379;
        int redis_db = 0;

        logger = log_handle_create("./maat_framework_perf_gtest.log", 0);        
        int ret = write_config_to_redis(redis_ip, redis_port, redis_db, logger);
        if (ret < 0) {
            log_error(logger, MODULE_FRAMEWORK_PERF_GTEST,
                      "[%s:%d] write config to redis failed.", __FUNCTION__, __LINE__);
        }

        struct maat_options *opts = maat_options_new();
        maat_options_set_redis(opts, redis_ip, redis_port, redis_db);
        maat_options_set_logger(opts, "./maat_framework_perf_gtest.log", LOG_LEVEL_INFO);
        maat_options_set_accept_tags(opts, accept_tags);
        maat_options_set_caller_thread_number(opts, 5);

        _shared_maat_instance = maat_new(opts, table_info_path);
        maat_options_free(opts);
        if (NULL == _shared_maat_instance) {
            log_error(logger, MODULE_FRAMEWORK_PERF_GTEST,
                      "[%s:%d] create maat instance in MaatFlagScan failed.",
                      __FUNCTION__, __LINE__);
        }
    }

    static void TearDownTestCase() {
        maat_free(_shared_maat_instance);
        log_handle_destroy(logger);
    }

    static struct log_handle *logger;
    static struct maat *_shared_maat_instance;
};

struct maat *MaatPerfIPScan::_shared_maat_instance;
struct log_handle *MaatPerfIPScan::logger;

TEST_F(MaatPerfIPScan, basic)
{
    const char *table_name = "IP_PLUS_CONFIG";
    struct maat *maat_instance = MaatPerfIPScan::_shared_maat_instance;

    int table_id = maat_get_table_id(maat_instance, table_name);
    ASSERT_GT(table_id, 0);

    pthread_t threads[PERF_THREAD_NUM + 1];
    struct thread_param thread_params[PERF_THREAD_NUM + 1];
    int i = 0;
    int *is_all_hit = NULL;

    for (i = 0; i < PERF_THREAD_NUM + 1; i++) {
        thread_params[i].maat_instance = maat_instance;
        thread_params[i].thread_id = i;
        thread_params[i].table_name = table_name;
        thread_params[i].test_count = 1 * 1000 * 1000;
        thread_params[i].time_elapse_ms = 0;
        thread_params[i].logger = logger;

        if (i < PERF_THREAD_NUM) {
            pthread_create(&threads[i], NULL, perf_ip_scan_thread, thread_params+i);
        } else {
            thread_params[i].test_count = 0;
            pthread_create(&threads[i], NULL, perf_ip_update_thread, thread_params+i);
        }
    }

    long long time_elapse_ms = 0;
    long long scan_count = 0;
    long long scan_per_second = 0;
    for (i = 0; i < PERF_THREAD_NUM + 1; i++) {
        pthread_join(threads[i], (void **)&is_all_hit);
        time_elapse_ms += thread_params[i].time_elapse_ms;
		scan_count += thread_params[i].test_count;

		EXPECT_EQ(*is_all_hit, 1);
		*is_all_hit = 0;
		free(is_all_hit);
    }
    scan_per_second = scan_count * 1000 / time_elapse_ms;
    //EXPECT_GT(scan_per_second, 800 * 1000);
    printf("High match rate on %d-threads speed %lld lookups/s/thread\n", PERF_THREAD_NUM, scan_per_second);
}

int main(int argc, char ** argv)
{
	int ret=0;
	::testing::InitGoogleTest(&argc, argv);

    ret=RUN_ALL_TESTS();

	return ret;
}