69 #define MAX_THREADS 1024 71 static void help(
void)
73 info(0,
"Usage: test_dbpool [options] ...");
74 info(0,
"where options are:");
76 info(0,
" set log level for stderr logging");
77 info(0,
"-h hostname");
78 info(0,
" hostname to connect to");
79 info(0,
"-u username");
80 info(0,
" username to use for the login credentials");
81 info(0,
"-p password");
82 info(0,
" password to use for the login credentials");
83 info(0,
"-d database");
84 info(0,
" database to connect to (for oracle tnsname) or file to open (for sqlite)");
86 info(0,
" size of the database connection pool (default: 5)");
88 info(0,
" run a set of queries on the database connection pool (default: 100)");
90 info(0,
" how many query client threads should be used (default: 1)");
92 info(0,
" the SQL string that is performed while the queries (default: SHOW STATUS)");
94 info(0,
" the type of database to use [mysql|oracle|sqlite|cassandra]");
98 static unsigned long queries = 100;
100 static unsigned int pool_size = 5;
108 static void mysql_client_thread(
void *arg)
110 unsigned long i, succeeded, failed;
115 succeeded = failed = 0;
117 info(0,
"Client thread started with %ld queries to perform on pool", queries);
119 for (i = 1; i <= queries; i++) {
144 info(0,
"This thread: %ld succeeded, %ld failed.", succeeded, failed);
150 conf = gw_malloc(
sizeof(
DBConf));
169 conf = gw_malloc(
sizeof(
DBConf));
188 static void oracle_client_thread(
void *arg)
195 for (i = 1; i <= queries; i++) {
226 conf = gw_malloc(
sizeof(
DBConf));
234 static int callback(
void *not_used,
int argc,
char **argv,
char **col_name)
238 for (i = 0; i < argc; i++) {
239 debug(
"",0,
"SQLite: result: %s = %s", col_name[i], argv[i]);
245 static void sqlite_client_thread(
void *arg)
247 unsigned long i, succeeded, failed;
251 succeeded = failed = 0;
253 info(0,
"Client thread started with %ld queries to perform on pool", queries);
256 for (i = 1; i <= queries; i++) {
262 debug(
"",0,
"Query %ld/%ld: sqlite conn obj at %p",
263 i, queries, (
void*) pconn->
conn);
266 if (state != SQLITE_OK) {
267 error(0,
"SQLite: %s", errmsg);
276 info(0,
"This thread: %ld succeeded, %ld failed.", succeeded, failed);
286 conf = gw_malloc(
sizeof(
DBConf));
294 static int callback3(
void *not_used,
int argc,
char **argv,
char **col_name)
298 for (i = 0; i < argc; i++) {
299 debug(
"",0,
"SQLite3: result: %s = %s", col_name[i], argv[i]);
305 static void sqlite3_client_thread(
void *arg)
307 unsigned long i, succeeded, failed;
311 succeeded = failed = 0;
313 info(0,
"Client thread started with %ld queries to perform on pool", queries);
316 for (i = 1; i <= queries; i++) {
322 debug(
"",0,
"Query %ld/%ld: sqlite conn obj at %p",
323 i, queries, (
void*) pconn->
conn);
326 if (state != SQLITE_OK) {
327 error(0,
"SQLite3: %s", errmsg);
336 info(0,
"This thread: %ld succeeded, %ld failed.", succeeded, failed);
342 static void cass_client_thread(
void *arg)
344 unsigned long i, succeeded, failed;
349 succeeded = failed = 0;
351 info(0,
"Client thread started with %ld queries to perform on pool", queries);
353 for (i = 1; i <= queries; i++) {
378 info(0,
"This thread: %ld succeeded, %ld failed.", succeeded, failed);
384 conf = gw_malloc(
sizeof(
DBConf));
396 static void inc_dec_thread(
void *arg)
402 info(0,
"Decreasing pool by half of size, which is %d connections", abs(pool_size/2));
404 debug(
"",0,
"Decreased by %d connections", ret);
408 info(0,
"Increasing pool again by %d connections", pool_size);
410 debug(
"",0,
"Increased by %d connections", ret);
414 int main(
int argc,
char **argv)
418 unsigned int num_threads = 1;
421 time_t
start = 0, end = 0;
432 while ((opt =
getopt(argc, argv,
"v:h:u:p:d:s:q:t:S:T:")) != EOF) {
468 num_threads = atoi(
optarg);
477 error(0,
"Invalid option %c", opt);
479 panic(0,
"Stopping.");
489 info(0,
"No database type given assuming MySQL.");
492 info(0,
"Do tests for mysql database.");
496 info(0,
"Do tests for oracle database.");
500 info(0,
"Do tests for sqlite database.");
504 info(0,
"Do tests for sqlite3 database.");
508 info(0,
"Do tests for cassandra database.");
516 switch (database_type) {
518 bail_out = (!user || !pass || !db) ? 1 : 0;
522 bail_out = (!db) ? 1 : 0;
525 bail_out = (!
host || !db) ? 1 : 0;
528 bail_out = (!
host || !user || !pass || !db) ? 1 : 0;
533 panic(0,
"Database connection details are not fully provided!");
536 for (j = 0; j < 1; j++) {
539 switch (database_type) {
542 conf = mysql_create_conf(user,pass,db,
host);
548 conf = oracle_create_conf(user, pass, db);
554 conf = sqlite_create_conf(db);
560 conf = sqlite3_create_conf(db);
566 conf = cass_create_conf(user,pass,db,
host);
571 panic(0,
"ooops ....");
575 info(0,
"Creating database pool to `%s' with %d connections type '%s'.",
580 panic(0,
"Unable to start without DBConns...");
584 for (i = 0; i < num_threads; ++i) {
586 panic(0,
"Could not create thread %ld", i);
591 info(0,
"Checked pool, %d connections still active and ok",
dbpool_check(pool));
596 for (i = 0; i < num_threads; ++i) {
599 panic(0,
"Couldnot create thread %ld", i);
602 panic(0,
"Couldnot create thread %ld", i);
608 run_time = difftime(end,
start);
609 info(0,
"%ld requests in %.2f seconds, %.2f requests/s.",
610 (queries * num_threads), run_time, (
float) (queries * num_threads) / (run_time==0?1:run_time));
614 info(0,
"Checked pool, %d connections still active and ok",
dbpool_check(pool));
616 info(0,
"Destroying pool");
633 int main(
int argc,
char **argv)
void error(int err, const char *fmt,...)
void info(int err, const char *fmt,...)
void gwthread_join_all(void)
unsigned int dbpool_check(DBPool *p)
long dbpool_conn_count(DBPool *p)
DBPool * dbpool_create(enum db_type db_type, DBConf *conf, unsigned int connections)
long gwlist_len(List *list)
static void client_thread(void *arg)
void * gwlist_get(List *list, long pos)
unsigned int dbpool_decrease(DBPool *p, unsigned int conn)
unsigned int dbpool_increase(DBPool *p, unsigned int conn)
#define octstr_get_cstr(ostr)
int main(int argc, char **argv)
void dbpool_conn_produce(DBPoolConn *conn)
int getopt(int argc, char **argv, char *opts)
Octstr * octstr_imm(const char *cstr)
void log_set_output_level(enum output_level level)
#define octstr_duplicate(ostr)
int octstr_case_compare(const Octstr *os1, const Octstr *os2)
void octstr_destroy(Octstr *ostr)
#define gwthread_create(func, arg)
#define octstr_create(cstr)
void octstr_destroy_item(void *os)
void dbpool_destroy(DBPool *p)
int dbpool_conn_update(DBPoolConn *conn, const Octstr *sql, List *binds)
void debug(const char *place, int err, const char *fmt,...)
void gwlib_shutdown(void)
int dbpool_conn_select(DBPoolConn *conn, const Octstr *sql, List *binds, List **result)
DBPoolConn * dbpool_conn_consume(DBPool *p)
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)