36 #include <sys/timeb.h> 48 #define PRINT_CCI_ERROR(ERRCODE, CCI_ERROR, result_fp) \ 50 T_CCI_ERROR *cci_error_p = CCI_ERROR; \ 51 if ((ERRCODE) == CCI_ER_DBMS && cci_error_p != NULL) { \ 52 if (!ignore_error(cci_error_p->err_code)) { \ 54 if (cubrid_manager_run) { \ 55 fprintf(cas_error_fp, "server error : (%d) %s\n", cci_error_p->err_code, cci_error_p->err_msg); \ 59 fprintf(result_fp, "%s: server error : %d %s\n", exec_script_file, cci_error_p->err_code, cci_error_p->err_msg); \ 61 fprintf(cas_error_fp, "%s: server error : %d %s\n", exec_script_file, cci_error_p->err_code, cci_error_p->err_msg); \ 65 else if ((ERRCODE) < 0) { \ 66 char msgbuf[1024] = ""; \ 68 if (cubrid_manager_run) { \ 69 cci_get_error_msg(ERRCODE, NULL, msgbuf, sizeof(msgbuf)); \ 70 fprintf(cas_error_fp, "%s\n", msgbuf); \ 73 cci_get_error_msg(ERRCODE, NULL, msgbuf, sizeof(msgbuf)); \ 75 fprintf(result_fp, "%s: cci_error : %d %s\n", exec_script_file, (ERRCODE), msgbuf); \ 77 fprintf(cas_error_fp, "%s: cci_error : %d %s\n", exec_script_file, (ERRCODE), msgbuf); \ 82 #define FREE_BIND_INFO(NUM_BIND, BIND_INFO) \ 85 for (i=0 ; i < NUM_BIND ; i++) { \ 86 FREE_MEM(BIND_INFO[i].value); \ 91 #define MAX_NODE_INFO 100 92 #define MAX_IGN_SRV_ERR 100 95 #define strcasecmp(X, Y) _stricmp(X, Y) 99 #define THREAD_FUNC unsigned __stdcall 100 #define SLEEP_SEC(X) Sleep((X) * 1000) 101 #define SLEEP_MILISEC(sec, msec) Sleep((sec) * 1000 + (msec)) 103 #define THREAD_FUNC void* 104 #define SLEEP_SEC(X) sleep(X) 105 #define SLEEP_MILISEC(sec, msec) \ 107 struct timeval sleep_time_val; \ 108 sleep_time_val.tv_sec = sec; \ 109 sleep_time_val.tv_usec = (msec) * 1000; \ 110 select(0, 0, 0, 0, &sleep_time_val); \ 114 #define STRDUP(TARGET, SOURCE) \ 116 if(TARGET != NULL) free(TARGET); \ 117 TARGET = strdup (SOURCE); \ 120 #define SERVER_HANDLE_ALLOC_SIZE (MAX_SERVER_H_ID + 1) 146 static void cas_runner (FILE * fp, FILE * result_fp,
double *ret_exec_time,
double *ret_prepare_time);
149 double *sum_execute_time);
153 static void print_result (
int cci_res,
int req_id, FILE * fp);
197 const char *err_str =
"-";
201 char *cm_out_msg_fname =
NULL;
202 FILE *cm_out_msg_fp =
NULL;
204 #if !defined(WINDOWS) 205 signal (SIGPIPE, SIG_IGN);
216 cm_out_msg_fname = getenv (
"CUBRID_MANAGER_OUT_MSG_FILE");
217 if (cm_out_msg_fname !=
NULL)
226 fprintf (stderr,
"error:node (%s)\n",
node_name);
233 fprintf (stderr,
"error:broker_host\n");
238 fprintf (stderr,
"error:broker_port(%d)\n",
broker_port);
243 fprintf (stderr,
"errorr:dbname\n");
247 dbuser = (
char *)
"PUBLIC";
271 fprintf (stdout,
"broker_host = %s\n",
broker_host);
272 fprintf (stdout,
"broker_port = %d\n",
broker_port);
273 fprintf (stdout,
"num_thread = %d\n",
num_thread);
275 fprintf (stdout,
"dbname = %s\n",
dbname);
276 fprintf (stdout,
"dbuser = %s\n",
dbuser);
277 fprintf (stdout,
"dbpasswd = %s\n",
dbpasswd);
280 fprintf (stdout,
"result_file = %s\n",
result_file);
284 thr_id = (pthread_t *) malloc (
sizeof (pthread_t) *
num_thread);
287 fprintf (stderr,
"malloc error\n");
294 fprintf (stderr,
"malloc error\n");
303 T_CCI_ERROR cci_error;
304 con_handle = (
int *) malloc (
sizeof (
int) *
num_thread);
305 if (con_handle ==
NULL)
308 fprintf (stderr,
"malloc error\n");
314 cci_get_db_version (con_handle[i],
NULL, 0);
318 cci_disconnect (con_handle[i], &cci_error);
330 if (pthread_create (&thr_id[i],
NULL,
thr_main, (
void *) &i) < 0)
333 perror (
"Error:cannot create thread");
340 if (pthread_join (thr_id[i],
NULL) < 0)
342 perror (
"pthread_join");
346 if (cm_out_msg_fname !=
NULL)
348 cm_out_msg_fp = fopen (cm_out_msg_fname,
"w");
351 if (cm_out_msg_fp ==
NULL)
353 cm_out_msg_fp = stdout;
360 if (cm_out_msg_fname !=
NULL)
362 err_fp = cm_out_msg_fp;
369 fprintf (err_fp,
"\n");
370 fprintf (err_fp,
"********************************\n");
383 while ((readlen = fread (buf, 1,
sizeof (buf), fp)) > 0)
385 if (readlen >
sizeof (buf))
387 readlen =
sizeof (buf);
389 fwrite (buf, 1, readlen, err_fp);
394 fprintf (err_fp,
"********************************\n");
408 fprintf (cm_out_msg_fp,
"min : %.6f\n", min);
409 fprintf (cm_out_msg_fp,
"max : %.6f\n", max);
410 fprintf (cm_out_msg_fp,
"avg : %.6f\n", avg);
411 fprintf (cm_out_msg_fp,
"stddev : %.6f\n", stddev);
415 fprintf (stdout,
"%.6f %.6f %s\n", avg, stddev, err_str);
418 if (cm_out_msg_fname !=
NULL)
420 fflush (cm_out_msg_fp);
421 if (cm_out_msg_fp != stdout)
423 fclose (cm_out_msg_fp);
444 for (i = 1; i <
count; i++)
458 for (i = 0; i <
count; i++)
462 return (sum / count);
470 for (i = 0; i <
count; i++)
472 sum += ((t[
i] - avg) * (t[i] - avg));
483 while ((c =
getopt (argc, argv,
"saQbqI:P:d:u:p:t:r:o:e:f:n:h:R:")) != EOF)
570 "usage : %s [OPTION] exec_script_file\n" "\n" "valid options:\n" " -I broker host\n" 571 " -P broker port\n" " -d database name\n" " -u user name\n" " -p user password\n" 572 " -t the number of thread\n" " -r the number of times to execute entire query by each thread\n" 573 " -Q enable to print a plan per query\n" " -o result file\n" 574 " -s enable to print a statdump per query\n" " -a enable auto commit mode\n", argv[0]);
581 int id = *(
int *) arg;
607 char result_filename[256];
608 sprintf (result_filename,
"%s.%d",
result_file,
id);
609 result_fp = fopen (result_filename,
"w");
616 fseek (fp, 0, SEEK_SET);
625 fseek (fp, 0, SEEK_SET);
631 if (result_fp !=
NULL && result_fp != stderr && result_fp != stdout)
643 cas_runner (FILE * fp, FILE * result_fp,
double *ret_exec_time,
double *ret_prepare_time)
645 char *sql_stmt =
NULL;
647 T_CCI_ERROR cci_error;
649 double prepare_time = 0;
650 double sum_execute_time = 0;
651 double sum_prepare_time = 0;
652 char *linebuf =
NULL;
663 int *dup_req_h =
NULL;
673 if (linebuf_tstr ==
NULL || req_h ==
NULL || bind_info ==
NULL 679 fprintf (stderr,
"malloc error\n");
685 memset (dup_req_h, 0,
sizeof (
int) * SERVER_HANDLE_ALLOC_SIZE);
698 fprintf (stderr,
"DUP_RUN cci_connect error\n");
705 if (cci_set_autocommit (con_h, CCI_AUTOCOMMIT_TRUE) < 0)
707 fprintf (stderr,
"cannot set autocommit mode");
711 if (cci_set_autocommit (dup_con_h, CCI_AUTOCOMMIT_TRUE) < 0)
713 fprintf (stderr,
"DUP_RUN cannot set autocommit mode");
721 req_stat_h = cci_prepare (con_h,
"set @collect_exec_stats = 1", 0, &cci_error);
724 fprintf (stderr,
"cci_prepare error\n");
729 error = cci_execute (req_stat_h, 0, 0, &cci_error);
731 res = cci_close_req_handle (req_stat_h);
734 fprintf (stderr,
"cci_close_req_error\n");
740 fprintf (stderr,
"cci_execute error\n");
744 req_stat_h = cci_prepare (con_h,
"show exec statistics", 0, &cci_error);
747 fprintf (stderr,
"cci_prepare error\n");
757 fprintf (stderr,
"malloc error\n");
764 if (linebuf[
strlen (linebuf) - 1] ==
'\n')
765 linebuf[
strlen (linebuf) - 1] =
'\0';
767 if (linebuf[0] ==
'Q')
771 if (sql_stmt ==
NULL)
778 fprintf (result_fp,
"-------------- query -----------------\n");
779 fprintf (result_fp,
"%s\n", sql_stmt);
782 else if (linebuf[0] ==
'P')
784 int req_id, prepare_flag;
785 struct timeval begin, end;
787 if (sscanf (linebuf + 2,
"%d %d", &req_id, &prepare_flag) < 2)
789 fprintf (stderr,
"file format error : %s\n", linebuf);
793 if (req_id < 0 || req_id >= SERVER_HANDLE_ALLOC_SIZE)
795 fprintf (stderr,
"request id error : %d (valid range 0-%d)\n", req_id, SERVER_HANDLE_ALLOC_SIZE - 1);
799 gettimeofday (&begin,
NULL);
800 req_h[req_id] = cci_prepare (con_h, sql_stmt, prepare_flag, &cci_error);
801 gettimeofday (&end,
NULL);
803 sum_prepare_time += prepare_time;
807 fprintf (result_fp,
"cci_prepare elapsed time : %.3f \n", prepare_time);
810 if (req_h[req_id] < 0)
812 fprintf (
cas_error_fp,
"prepare error\n%s\nrequest id %d\n", linebuf, req_id);
816 dup_req_h[req_id] = cci_prepare (dup_con_h, sql_stmt, prepare_flag, &cci_error);
820 else if (linebuf[0] ==
'B')
828 else if (linebuf[0] ==
'E')
831 res =
process_execute (linebuf, req_h, num_bind, bind_info, result_fp, &sum_execute_time);
840 else if (linebuf[0] ==
'C')
848 else if (linebuf[0] ==
'T')
855 fprintf (stderr,
"DUP_RUN end_transaction error\n");
860 error = cci_execute (req_stat_h, 0, 0, &cci_error);
863 fprintf (
cas_error_fp,
"execute error\nshow exec statistics\nrequest id %d\n", req_stat_h);
868 fprintf (result_fp,
"SHOW EXEC STATISTICS\n");
873 error = cci_cursor (req_stat_h, 1, CCI_CURSOR_CURRENT, &cci_error);
875 if (error == CCI_ER_NO_MORE_DATA)
882 fprintf (
cas_error_fp,
"cursor error\nrequest id %d\n", req_stat_h);
887 error = cci_fetch (req_stat_h, &cci_error);
890 fprintf (
cas_error_fp,
"fetch error\nrequest id %d\n", req_stat_h);
894 for (i = 1; i <= 2; i++)
896 error = cci_get_data (req_stat_h, i, CCI_A_TYPE_STR, &data, &ind);
899 fprintf (
cas_error_fp,
"get data error\nrequest id %d\n", req_stat_h);
903 if (ind < 0 || data ==
NULL)
907 fprintf (result_fp,
"<NULL>\t|");
914 fprintf (result_fp,
"%s\t|", data);
920 fprintf (result_fp,
"\n");
927 fprintf (stderr,
"file format error : %s\n", linebuf);
934 cci_close_req_handle (req_stat_h);
939 cci_disconnect (con_h, &cci_error);
944 cci_disconnect (dup_con_h, &cci_error);
958 *ret_exec_time = sum_execute_time;
959 if (ret_prepare_time)
960 *ret_prepare_time = sum_prepare_time;
968 char buf1[1024], buf2[1024], buf3[1024];
970 const char *conf_file;
981 if (conf_file ==
NULL)
984 fp = fopen (conf_file,
"r");
992 while (fgets (read_buf,
sizeof (read_buf), fp))
996 p = strchr (read_buf,
'#');
1001 num_token = sscanf (read_buf,
"%1023s %1023s %1023s", buf1, buf2, buf3);
1009 if (strcasecmp (buf1,
"node") == 0)
1024 if (strcasecmp (buf1,
"CAS_IP") == 0)
1028 else if (strcasecmp (buf1,
"CAS_PORT") == 0)
1032 else if (strcasecmp (buf1,
"DBNAME") == 0)
1036 else if (strcasecmp (buf1,
"NUM_THREAD") == 0)
1040 else if (strcasecmp (buf1,
"DBUSER") == 0)
1044 else if (strcasecmp (buf1,
"DBPASSWD") == 0)
1048 else if (strcasecmp (buf1,
"REPEAT") == 0)
1052 else if (strcasecmp (buf1,
"RESULT_FILE") == 0)
1056 else if (strcasecmp (buf1,
"CAS_ERROR_FILE") == 0)
1060 else if (strcasecmp (buf1,
"FORK_DELAY") == 0)
1064 else if (strcasecmp (buf1,
"IGNORE_SERVER_ERROR") == 0)
1066 int ign_err = atoi (buf2);
1092 int num_bind = *num_bind_p;
1096 fprintf (stderr,
"bind buffer overflow[%d]\n", num_bind);
1100 bind_info[num_bind].
type = atoi (linebuf + 2);
1101 p = strchr (linebuf + 2,
' ');
1104 fprintf (stderr,
"file format error : %s\n", linebuf);
1108 if ((bind_info[num_bind].
type == CCI_U_TYPE_CHAR) || (bind_info[num_bind].
type == CCI_U_TYPE_STRING)
1109 || (bind_info[num_bind].
type == CCI_U_TYPE_NCHAR) || (bind_info[num_bind].
type == CCI_U_TYPE_VARNCHAR)
1110 || (bind_info[num_bind].
type == CCI_U_TYPE_BIT) || (bind_info[num_bind].
type == CCI_U_TYPE_VARBIT)
1111 || (bind_info[num_bind].
type == CCI_U_TYPE_ENUM) || (bind_info[num_bind].
type == CCI_U_TYPE_JSON))
1113 bind_info[num_bind].
len = atoi (p + 1);
1114 p = strchr (p + 1,
' ');
1117 fprintf (stderr,
"file format error : %s\n", linebuf);
1121 else if (bind_info[num_bind].
type == CCI_U_TYPE_BLOB || bind_info[num_bind].
type == CCI_U_TYPE_CLOB)
1123 fprintf (stderr,
"binding BLOB/CLOB is not implemented : %s\nreplaced with NULL value.\n", p + 1);
1124 bind_info[num_bind].
type = CCI_U_TYPE_NULL;
1130 fprintf (stderr,
"malloc error\n");
1133 *num_bind_p = num_bind + 1;
1139 double *sum_execute_time)
1141 int req_id, exec_flag;
1142 T_CCI_ERROR cci_error;
1143 struct timeval begin, end;
1144 double elapsed_time = 0;
1146 if (sscanf (linebuf + 2,
"%d %d", &req_id, &exec_flag) < 2)
1148 fprintf (stderr,
"file format error : %s\n", linebuf);
1159 exec_flag |= CCI_EXEC_QUERY_ALL;
1162 if (req_h[req_id] > 0)
1170 for (i = 0; i < num_bind; i++)
1172 if ((bind_info[i].
type == CCI_U_TYPE_VARBIT) || (bind_info[i].
type == CCI_U_TYPE_BIT))
1175 memset ((
char *) &vptr, 0x00,
sizeof (T_CCI_BIT));
1176 vptr.size = bind_info[
i].
len;
1177 vptr.buf = (
char *) bind_info[i].
value;
1179 cci_bind_param (req_h[req_id], (k * num_bind) + i + 1, CCI_A_TYPE_BIT, (
void *) &(vptr),
1180 (T_CCI_U_TYPE) bind_info[i].
type, CCI_BIND_PTR);
1185 cci_bind_param (req_h[req_id], (k * num_bind) + i + 1, CCI_A_TYPE_STR, bind_info[i].
value,
1186 (T_CCI_U_TYPE) bind_info[i].
type, 0);
1190 fprintf (
cas_error_fp,
"bind error\n%s\nrequest id %d bind %d\n", linebuf, req_id, i);
1198 exec_flag |= CCI_EXEC_QUERY_INFO;
1200 gettimeofday (&begin,
NULL);
1201 res = cci_execute (req_h[req_id], exec_flag, 0, &cci_error);
1202 gettimeofday (&end,
NULL);
1206 fprintf (stdout,
"exec_time : %.3f \n", elapsed_time);
1211 fprintf (result_fp,
"cci_execute elapsed_time : %.3f \n", elapsed_time);
1216 fprintf (
cas_error_fp,
"execute error\n%s\nrequest id %d\n", linebuf, req_id);
1224 if (sum_execute_time)
1225 *sum_execute_time += elapsed_time;
1235 req_id = atoi (linebuf + 2);
1238 fprintf (
cas_error_fp,
"close error\n%s\nrequest id %d\n", linebuf, req_id);
1242 if (req_h[req_id] > 0)
1244 res = cci_close_req_handle (req_h[req_id]);
1247 fprintf (
cas_error_fp,
"close error\n%s\nrequest id %d\n", linebuf, req_id);
1259 T_CCI_ERROR cci_error;
1261 struct timeval begin, end;
1266 gettimeofday (&begin,
NULL);
1267 res = cci_end_tran (con_h, CCI_TRAN_ROLLBACK, &cci_error);
1268 gettimeofday (&end,
NULL);
1273 fprintf (result_fp,
"cci_end_tran elapsed_time : %.3f \n", commit_time);
1278 fprintf (
cas_error_fp,
"end tran error\nconnection handle id %d\n", con_h);
1299 T_CCI_ERROR cci_error;
1301 T_CCI_CUBRID_STMT cmd_type;
1304 if (result_fp ==
NULL)
1307 fprintf (result_fp,
"cci_execute:%d\n", cci_res);
1311 if (cci_get_query_plan (req_id, &plan) >= 0)
1313 fprintf (result_fp,
"---------- query plan --------------\n");
1314 fprintf (result_fp,
"%s\n", (plan ? plan :
""));
1315 cci_query_info_free (plan);
1320 cci_get_result_info (req_id, &cmd_type, &column_count);
1329 res = cci_cursor (req_id, 1, CCI_CURSOR_FIRST, &cci_error);
1330 if (res == CCI_ER_NO_MORE_DATA || column_count <= 0)
1334 fprintf (
cas_error_fp,
"cursor error\nrequest id %d\n", req_id);
1339 fprintf (result_fp,
"---------- query result --------------\n");
1343 res = cci_fetch (req_id, &cci_error);
1346 fprintf (
cas_error_fp,
"fetch error\nrequest id %d\n", req_id);
1350 for (i = 0; i < column_count; i++)
1352 res = cci_get_data (req_id, i + 1, CCI_A_TYPE_STR, &buffer, &ind);
1355 fprintf (
cas_error_fp,
"get data error\nrequest id %d\n", req_id);
1359 if (ind < 0 || buffer ==
NULL)
1360 fprintf (result_fp,
"<NULL>|");
1362 fprintf (result_fp,
"%s|", buffer);
1364 fprintf (result_fp,
"\n");
1367 if (cmd_type == CUBRID_STMT_CALL_SP)
1373 res = cci_cursor (req_id, 1, CCI_CURSOR_CURRENT, &cci_error);
1374 if (res == CCI_ER_NO_MORE_DATA)
1378 fprintf (
cas_error_fp,
"cursor error\nrequest id %d\n", req_id);
1385 fprintf (result_fp,
"-- %d rows ----------------------------\n", num_tuple);
1400 info_str =
strdup (info_str);
1401 if (info_str ==
NULL)
1403 fprintf (stderr,
"malloc error\n");
1410 for (i = 1; i < 5; i++)
1412 p = strchr (str,
':');
1423 node->
port = atoi (token[2]);
1439 fprintf (stderr,
"invalid node format (%s)\n", info_str);
1462 if (strcasecmp (node_table[i].node_name, node_name) == 0)
1464 node = &node_table[
i];
1504 tmp = (
char *) malloc (
strlen (src) + 3);
1507 fprintf (stderr,
"malloc error\n");
1511 for (p = tmp; *
p; p++)
1519 query_len = (int)
strlen (tmp);
1524 query_len = (int)
strlen (tmp);
1527 if (tmp[query_len - 1] !=
';')
1529 tmp[query_len++] =
';';
1531 tmp[query_len++] =
'\n';
1532 tmp[query_len] =
'\0';
1544 query = (
char *) malloc ((query_len + 1) *
num_replica);
1547 fprintf (stderr,
"malloc error\n");
1553 strcpy (query + offset, tmp);
1554 offset += query_len;
static int make_node_info(T_NODE_INFO *node, char *node_name, char *info_str)
#define CAS_RUNNER_CONF_ENV
int getopt(int, char *const *, const char *)
static int process_close_req(char *linebuf, int *req_h, FILE *result_fp)
static int process_endtran(int con_h, int *req_h, FILE *result_fp)
static char * exec_script_file
static THREAD_FUNC thr_main(void *arg)
static int ign_srv_err_list[MAX_IGN_SRV_ERR]
void t_string_free(T_STRING *t_str)
static int set_args_with_node_info(char *node_name)
static int num_ign_srv_err
int t_string_len(T_STRING *t_str)
static void cas_runner(FILE *fp, FILE *result_fp, double *ret_exec_time, double *ret_prepare_time)
static double calc_avg(double *t, int count)
static int cubrid_manager_run
static int autocommit_mode
static FILE * cas_error_fp
int main(int argc, char *argv[])
char * t_string_str(T_STRING *t_str)
static double calc_stddev(double *t, double avg, int count)
static void calc_min_max(double *t, int count, double *min, double *max)
static int read_conf(void)
static int process_bind(char *msg, int *num_bind_p, T_BIND_INFO *bind_info)
static void print_result(int cci_res, int req_id, FILE *fp)
#define SERVER_HANDLE_ALLOC_SIZE
static double * run_time_exec
int count(int &result, const cub_regex_object ®, const std::string &src, const int position, const INTL_CODESET codeset)
static int process_execute(char *msg, int *req_h, int num_bind, T_BIND_INFO *bind_info, FILE *result_fp, double *sum_execute_time)
static void error(const char *msg)
#define STRDUP(TARGET, SOURCE)
static int dump_query_plan
static char * cas_err_file
static void free_node(T_NODE_INFO *node)
static char * broker_host
double ut_diff_time(struct timeval *begin, struct timeval *end)
static T_NODE_INFO node_table[MAX_NODE_INFO]
#define PRINT_CCI_ERROR(ERRCODE, CCI_ERROR, result_fp)
T_STRING * t_string_make(int init_size)
char * strdup(const char *str)
static int ignore_error(int code)
const char * cci_client_name
static char * result_file
static int get_args(int argc, char *argv[])
static char * make_sql_stmt(char *src)
int ut_get_line(FILE *fp, T_STRING *t_str, char **out_str, int *lineno)
#define FREE_BIND_INFO(NUM_BIND, BIND_INFO)
static int cas_error_flag