【修改】更新单元测试
This commit is contained in:
@@ -7,75 +7,159 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <getopt.h>
|
||||
#include <time.h>
|
||||
|
||||
#define PLUGIN_CONFIG_PATH "./conf/plugin_manage.toml"
|
||||
#define LUA_CONFIG_PATH "./conf/lua_plugin_manage.toml"
|
||||
|
||||
// #define DEBUG_PLUGIN_SCHEMA
|
||||
|
||||
static struct lua_config_specific *config_load(const char *config_file_name, int *specific_num);
|
||||
#ifdef DEBUG_PLUGIN_SCHEMA
|
||||
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema);
|
||||
#endif
|
||||
|
||||
#define PACKET_COUNT 10
|
||||
#define PACKET_COUNT 3
|
||||
#define SESSION_COUNT 10
|
||||
|
||||
int main()
|
||||
struct arg_config
|
||||
{
|
||||
struct stellar st;
|
||||
memset(&st, 0, sizeof(st));
|
||||
int session_count;
|
||||
int packet_count;
|
||||
int wait_time;
|
||||
int thread_num;
|
||||
unsigned char lua_mod;
|
||||
unsigned char session_mod;
|
||||
unsigned char print_mod;
|
||||
unsigned char time_mod;
|
||||
char *config_path;
|
||||
};
|
||||
|
||||
int num = 0;
|
||||
struct lua_config_specific *specific = config_load(LUA_CONFIG_PATH, &num);
|
||||
struct plugin_manager_schema *plug_mgr = plugin_manager_init(&st, PLUGIN_CONFIG_PATH);
|
||||
/* 初始化lua插件 */
|
||||
struct lua_plugin_manage_schema *lua_schema = lua_plugin_manage_init(&st, num, specific);
|
||||
st.lua_plug_mgr = lua_schema;
|
||||
struct arg_config global_arg_config = {SESSION_COUNT, PACKET_COUNT, 10, 1, 1, 1, 1, 1, LUA_CONFIG_PATH};
|
||||
|
||||
struct lua_config_specific add_specific = {NULL, NULL, NULL};
|
||||
add_specific.config_specific_file = "./plugin/example_plugin-2.lua";
|
||||
add_specific.config_specific_load_func = "plugin_load";
|
||||
add_specific.config_specific_unload_func = "plugin_unload";
|
||||
static struct lua_config_specific *config_load(const char *config_file_name, int *specific_num);
|
||||
static void parse_args(int argc, char *argv[]);
|
||||
// #ifdef DEBUG_PLUGIN_SCHEMA
|
||||
static void debug_plugin_manage_specifics(struct lua_config_specific *specifics, int num);
|
||||
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema);
|
||||
// #endif
|
||||
|
||||
lua_plugin_manage_load_one_specific(st.lua_plug_mgr, &add_specific);
|
||||
#ifdef DEBUG_PLUGIN_SCHEMA
|
||||
debug_plugin_manage_schema(plug_mgr);
|
||||
#endif
|
||||
|
||||
unsigned char ip_proto=6;
|
||||
struct packet pkt={&st, TCP, ip_proto};
|
||||
struct session sess[SESSION_COUNT];
|
||||
memset(&sess, 0, sizeof(sess));
|
||||
|
||||
for(int i=0; i < SESSION_COUNT; i++)
|
||||
{
|
||||
sess[i].plug_mgr_rt=plugin_manager_session_runtime_new(plug_mgr, &sess[i]);
|
||||
sess[i].type=SESSION_TYPE_TCP;
|
||||
}
|
||||
|
||||
for (int j = 0; j < PACKET_COUNT; j++)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
parse_args(argc, argv);
|
||||
if (global_arg_config.print_mod)
|
||||
{
|
||||
// plugin_manager_on_packet_ingress(plug_mgr, &pkt);
|
||||
|
||||
for (int i = 0; i < SESSION_COUNT; i++)
|
||||
{
|
||||
sess[i].sess_pkt_cnt+=1;
|
||||
plugin_manager_on_session_ingress(&sess[i], &pkt);
|
||||
plugin_manager_on_session_egress(&sess[i], &pkt);
|
||||
}
|
||||
|
||||
// plugin_manager_on_packet_egress(plug_mgr, &pkt);
|
||||
printf("***** ***** ***** ***** ***** *****\n");
|
||||
printf("load example config:\n");
|
||||
printf("session count is %d\n", global_arg_config.session_count);
|
||||
printf("packet count is %d\n", global_arg_config.packet_count);
|
||||
printf("thread num is %d\n", global_arg_config.thread_num);
|
||||
printf("load config from path %s\n", global_arg_config.config_path);
|
||||
printf("***** ***** ***** ***** ***** *****\n\n");
|
||||
}
|
||||
|
||||
for(int i=0; i < SESSION_COUNT; i++)
|
||||
{
|
||||
plugin_manager_on_session_closing(&sess[i]);
|
||||
plugin_manager_session_runtime_free(sess[i].plug_mgr_rt);
|
||||
}
|
||||
clock_t start_clock, end_clock;
|
||||
|
||||
plugin_manager_exit(plug_mgr);
|
||||
struct stellar st;
|
||||
memset(&st, 0, sizeof(st));
|
||||
st.thread_num = global_arg_config.thread_num;
|
||||
|
||||
/* ***** ***** 进行初始化 ***** ***** */
|
||||
struct plugin_manager_schema *plug_mgr = plugin_manager_init(&st, PLUGIN_CONFIG_PATH);
|
||||
assert(plug_mgr);
|
||||
/* 初始化lua插件 */
|
||||
|
||||
printf("now wait time to compute memory ... ...\n");
|
||||
sleep(global_arg_config.wait_time);
|
||||
|
||||
struct lua_plugin_manage_schema *lua_schema = NULL;
|
||||
if (global_arg_config.lua_mod)
|
||||
{
|
||||
int num = 0;
|
||||
struct lua_config_specific *specific = config_load(LUA_CONFIG_PATH, &num);
|
||||
assert(specific);
|
||||
if (global_arg_config.print_mod)
|
||||
debug_plugin_manage_specifics(specific, num);
|
||||
|
||||
lua_schema = lua_plugin_manage_init(&st, num, specific);
|
||||
st.lua_plug_mgr = lua_schema;
|
||||
assert(lua_schema);
|
||||
if (specific)
|
||||
free(specific);
|
||||
|
||||
/* 测试单个插件加载函数 */
|
||||
struct lua_config_specific add_specific = {NULL, NULL, NULL};
|
||||
add_specific.config_specific_file = "./plugin/example_plugin_load.lua";
|
||||
add_specific.config_specific_load_func = "plugin_load";
|
||||
add_specific.config_specific_unload_func = "plugin_unload";
|
||||
lua_plugin_manage_load_one_specific(st.lua_plug_mgr, &add_specific);
|
||||
|
||||
if (global_arg_config.print_mod)
|
||||
debug_lua_plugin_manage_schema(lua_schema);
|
||||
|
||||
printf("now wait time to compute memory ... ...\n");
|
||||
sleep(global_arg_config.wait_time);
|
||||
}
|
||||
|
||||
// #ifdef DEBUG_PLUGIN_SCHEMA
|
||||
if (global_arg_config.print_mod)
|
||||
debug_plugin_manage_schema(plug_mgr);
|
||||
// #endif
|
||||
/* ***** ***** 初始化完成 ***** ***** */
|
||||
|
||||
/* ***** ***** 会话相关测试 ***** ***** */
|
||||
if (global_arg_config.session_mod)
|
||||
{
|
||||
unsigned char ip_proto = 6;
|
||||
struct packet pkt = {&st, TCP, ip_proto};
|
||||
struct session *sess = (struct session *)calloc(global_arg_config.session_count, sizeof(struct session));
|
||||
memset(sess, 0, global_arg_config.session_count * sizeof(struct session));
|
||||
|
||||
/* 创建session */
|
||||
for (int i = 0; i < global_arg_config.session_count; i++)
|
||||
{
|
||||
sess[i].plug_mgr_rt = plugin_manager_session_runtime_new(plug_mgr, &sess[i]);
|
||||
sess[i].type = SESSION_TYPE_TCP;
|
||||
}
|
||||
|
||||
clock_t total_clock = 0;
|
||||
for (int j = 0; j < global_arg_config.packet_count; j++)
|
||||
{
|
||||
plugin_manager_on_packet_ingress(plug_mgr, &pkt);
|
||||
|
||||
for (int i = 0; i < global_arg_config.session_count; i++)
|
||||
{
|
||||
sess[i].sess_pkt_cnt += 1;
|
||||
if (global_arg_config.time_mod)
|
||||
start_clock = clock();
|
||||
plugin_manager_on_session_ingress(&sess[i], &pkt);
|
||||
plugin_manager_on_session_egress(&sess[i], &pkt);
|
||||
if (global_arg_config.time_mod)
|
||||
{
|
||||
end_clock = clock();
|
||||
total_clock += (end_clock - start_clock);
|
||||
}
|
||||
}
|
||||
|
||||
if (global_arg_config.time_mod)
|
||||
{
|
||||
printf("for session count %d, total time is %ld\n", global_arg_config.session_count, total_clock);
|
||||
printf("trans to second, time is %f\n", ((double)total_clock / CLOCKS_PER_SEC));
|
||||
}
|
||||
|
||||
plugin_manager_on_packet_egress(plug_mgr, &pkt);
|
||||
}
|
||||
|
||||
/* 释放session */
|
||||
for (int i = 0; i < global_arg_config.session_count; i++)
|
||||
{
|
||||
plugin_manager_on_session_closing(&sess[i]);
|
||||
plugin_manager_session_runtime_free(sess[i].plug_mgr_rt);
|
||||
}
|
||||
}
|
||||
/* ***** ***** 会话相关完成 ***** ***** */
|
||||
|
||||
/* ***** ***** 释放 ***** ***** */
|
||||
lua_plugin_manage_exit(lua_schema);
|
||||
plugin_manager_exit(plug_mgr);
|
||||
/* ***** ***** 释放 ***** ***** */
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -132,15 +216,83 @@ static struct lua_config_specific *config_load(const char *config_file_name, int
|
||||
return new_spec;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_PLUGIN_SCHEMA
|
||||
static void parse_args(int argc, char *argv[])
|
||||
{
|
||||
int opt;
|
||||
static const char *shortopts = "s:p:w:c:t:lnde";
|
||||
static struct option longopts[] = {
|
||||
{"session_count", required_argument, NULL, 's'},
|
||||
{"packet_count", required_argument, NULL, 'p'},
|
||||
{"wait_time", required_argument, NULL, 'w'},
|
||||
{"config_path", required_argument, NULL, 'c'},
|
||||
{"thread_num", required_argument, NULL, 't'},
|
||||
{"disable_lua", no_argument, NULL, 'l'},
|
||||
{"disable_session", no_argument, NULL, 'n'},
|
||||
{"print_debug", no_argument, NULL, 'd'},
|
||||
{"disable_time", no_argument, NULL, 'e'},
|
||||
{0, 0, 0, 0},
|
||||
};
|
||||
|
||||
while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 's':
|
||||
global_arg_config.session_count = atoi(optarg);
|
||||
break;
|
||||
case 'p':
|
||||
global_arg_config.packet_count = atoi(optarg);
|
||||
break;
|
||||
case 'w':
|
||||
global_arg_config.wait_time = atoi(optarg);
|
||||
break;
|
||||
case 'c':
|
||||
global_arg_config.config_path = strdup(optarg);
|
||||
break;
|
||||
case 't':
|
||||
global_arg_config.thread_num = atoi(optarg);
|
||||
break;
|
||||
case 'l':
|
||||
global_arg_config.lua_mod = 0;
|
||||
break;
|
||||
case 'n':
|
||||
global_arg_config.session_mod = 0;
|
||||
break;
|
||||
case 'd':
|
||||
global_arg_config.print_mod = 1;
|
||||
break;
|
||||
case 'e':
|
||||
global_arg_config.time_mod = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// #ifdef DEBUG_PLUGIN_SCHEMA
|
||||
static void debug_plugin_manage_specifics(struct lua_config_specific *specifics, int num)
|
||||
{
|
||||
printf("***** ***** ***** ***** ***** *****\n");
|
||||
printf("load config specifics count is %d\n", num);
|
||||
for (int i = 0; i < num; ++i)
|
||||
{
|
||||
printf("[%d]file path: %s\n", i, specifics[i].config_specific_file);
|
||||
printf("[%d]load func: %s\n", i, specifics[i].config_specific_load_func);
|
||||
printf("[%d]unload func: %s\n", i, specifics[i].config_specific_unload_func);
|
||||
}
|
||||
printf("***** ***** ***** ***** ***** *****\n\n");
|
||||
}
|
||||
|
||||
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema)
|
||||
{
|
||||
struct registered_session_plugin_schema * plugin = NULL;
|
||||
for (int i = 0; i < (int)utarray_len(schema->registered_session_plugin_array); ++i) {
|
||||
struct registered_session_plugin_schema *plugin = NULL;
|
||||
for (int i = 0; i < (int)utarray_len(schema->registered_session_plugin_array); ++i)
|
||||
{
|
||||
plugin = (struct registered_session_plugin_schema *)utarray_eltptr(schema->registered_session_plugin_array, (unsigned int)i);
|
||||
printf("plugin[%d]: new func %p, free func %p, env %p\n", i, plugin->on_ctx_new, plugin->on_ctx_free, plugin->plugin_env);
|
||||
}
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// #endif
|
||||
|
||||
Reference in New Issue
Block a user