#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "tap.h" #include "utils.h" #include "command_line.h" unsigned long long monotonic_time() { struct timespec ts; //clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); // this is faster, but not precise clock_gettime(CLOCK_MONOTONIC, &ts); return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000); } struct cpu_timer { cpu_timer() { begin = monotonic_time(); } ~cpu_timer() { unsigned long long end = monotonic_time(); std::cerr << double( end - begin ) / 1000000 << " secs.\n" ; begin=end-begin; }; unsigned long long begin; }; bool debug_diag = true; unsigned int num_threads=5; int count=10; char *username=NULL; char *password=NULL; char *host=(char *)"localhost"; int port=3306; char *schema=(char *)"information_schema"; int silent = 0; int sysbench = 0; int local=0; int transactions=200; int uniquequeries=0; int histograms=-1; unsigned int g_passed=0; unsigned int g_failed=0; std::atomic cnt_transactions; std::atomic cnt_SELECT_outside_transactions; std::atomic cnt_expected_errors; unsigned int status_connections = 0; unsigned int connect_phase_completed = 0; unsigned int query_phase_completed = 0; __thread int g_seed; std::mutex mtx_; inline int fastrand() { g_seed = (214013*g_seed+2531011); return (g_seed>>16)&0x7FFF; } void gen_random(char *s, const int len) { static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < len; ++i) { s[i] = alphanum[fastrand() % (sizeof(alphanum) - 1)]; } s[len] = 0; } void * my_conn_thread(void *arg) { g_seed = time(NULL) ^ getpid() ^ pthread_self(); unsigned int select_OK=0; unsigned int select_ERR=0; int i, j; MYSQL **mysqlconns=(MYSQL **)malloc(sizeof(MYSQL *)*count); if (mysqlconns==NULL) { exit(EXIT_FAILURE); } for (i=0; i 7) { q = "RELEASE SAVEPOINT "; q += buf; if (debug_diag==true) diag("Thread %lu , connection %p , query: %s", pthread_self(), mysql, q.c_str()); if (mysql_query(mysql, q.c_str())) { fprintf(stderr,"Error running query: %s. Error: %s\n", q.c_str(), mysql_error(mysql)); exit(EXIT_FAILURE); } sleepDelay = fastrand()%100; usleep(sleepDelay * 100); } } { std::string q; int f = fr%3; if (f==0) { q = "COMMIT"; } else { q = "ROLLBACK"; /* // FIXME: this code is currently commented because of another bug if (explicit_transaction==false) { if (f!=1) { q = "SET AUTOCOMMIT=1"; } } */ } if (debug_diag==true) diag("Thread %lu , connection %p , query: %s", pthread_self(), mysql, q.c_str()); if (mysql_query(mysql, q.c_str())) { fprintf(stderr,"Error running query: %s. Error: %s\n", q.c_str(), mysql_error(mysql)); exit(EXIT_FAILURE); } cnt_transactions++; sleepDelay = fastrand()%100; usleep(sleepDelay * 100); } /* // we do not log every single transaction, too verbose bool testPassed = true; { std::lock_guard lock(mtx_); ok(testPassed, "mysql connection [%p], thread_id [%lu], transaction completed", mysql, mysql->thread_id); } */ if (fr%7 == 0) { std::string sel; sel = sel1; if (debug_diag==true) diag("Thread %lu , connection %p , query: %s", pthread_self(), mysql, sel.c_str()); if (mysql_query(mysql, sel.c_str())) { fprintf(stderr,"Error running query: %s. Error: %s\n", sel.c_str(), mysql_error(mysql)); } else { MYSQL_RES *result = mysql_store_result(mysql); mysql_free_result(result); select_OK++; cnt_SELECT_outside_transactions++; } sleepDelay = fastrand()%100; usleep(sleepDelay * 100); } } for (i=0; i= cnt_transactions+cnt_SELECT_outside_transactions-10) , "Number of transactions [%d] , Queries outside transaction [%d] , total connections returned [%d]", cnt_transactions.load(std::memory_order_relaxed), cnt_SELECT_outside_transactions.load(std::memory_order_relaxed), MyHGM_myconnpoll_get); ok((MyHGM_myconnpoll_get == cnt_transactions+cnt_SELECT_outside_transactions) , "Number of transactions [%d] , Queries outside transaction [%d] , total connections returned [%d]", cnt_transactions.load(std::memory_order_relaxed), cnt_SELECT_outside_transactions.load(std::memory_order_relaxed), MyHGM_myconnpoll_get); MYSQL_QUERY(mysqladmin, "DELETE FROM mysql_query_rules"); MYSQL_QUERY(mysqladmin, "INSERT INTO mysql_query_rules SELECT * FROM mysql_query_rules_948"); MYSQL_QUERY(mysqladmin, "LOAD MYSQL QUERY RULES TO RUNTIME"); return exit_status(); }