From: Tomek Mrugalski Date: Thu, 12 Jul 2012 12:10:30 +0000 (+0200) Subject: [2042] MySQL benchmark is now nicely wrapped in classes. X-Git-Tag: trac2351_base~117^2~30 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e9d858f634bc30565367ea29becdbaa6bf7d5132;p=thirdparty%2Fkea.git [2042] MySQL benchmark is now nicely wrapped in classes. --- diff --git a/tests/tools/dhcp-ubench/mysql_ubench.cc b/tests/tools/dhcp-ubench/mysql_ubench.cc index c6f9c0215e..5283832a53 100644 --- a/tests/tools/dhcp-ubench/mysql_ubench.cc +++ b/tests/tools/dhcp-ubench/mysql_ubench.cc @@ -8,54 +8,176 @@ using namespace std; -const char * hostname ="localhost"; -const char * user = "root"; -const char * passwd = "foobletch"; -const char * dbname = "kea"; +class uBenchmark { +public: + uBenchmark(uint32_t numInterations); -void failure(MYSQL* conn, const char* operation) { + virtual void printInfo() = 0; + virtual void connect() = 0; + virtual void disconnect() = 0; + virtual void createLease4Test() = 0; + virtual void searchLease4Test() = 0; + virtual void updateLease4Test() = 0; + virtual void deleteLease4Test() = 0; + + virtual void failure(const char* operation); + + void print_clock(const std::string& operation, uint32_t num, + const struct timespec& before, + const struct timespec& after); + + int run(); + +protected: + uint32_t Num_; // number of operations (e.g. insert lease num times) + + // five timestamps (1 at the beginning and 4 after each step) + struct timespec ts[5]; +}; + +uBenchmark::uBenchmark(uint32_t iterations) + :Num_(iterations) { + +} + +void uBenchmark::failure(const char* operation) { + cout << "Error during " << operation << endl; + throw string(operation); +} + +void uBenchmark::print_clock(const std::string& operation, uint32_t num, + const struct timespec& before, + const struct timespec& after) { + long int tv_sec = after.tv_sec - before.tv_sec; + + long int tv_nsec = after.tv_nsec - before.tv_nsec; + + cout << "after.tv_nsec=" << after.tv_nsec + << " before.tv_nsec=" << before.tv_nsec << endl; + + if (tv_nsec < 0) { + tv_sec++; + tv_nsec += 1000000000; // 10^9 + } + + double oneoper = (tv_nsec/1000 + tv_sec*1000000)/num; + + cout << "Operation " << operation << " repeated " << num << " times took " + << tv_sec << " seconds, " << tv_nsec/1000 << " us, 1 operation took " + << oneoper << "us (or " << (1000000/oneoper) << " oper/sec)" << endl; + +} + +int uBenchmark::run() { + + try { + connect(); + + clock_gettime(CLOCK_REALTIME, &ts[0]); + + createLease4Test(); + clock_gettime(CLOCK_REALTIME, &ts[1]); + + searchLease4Test(); + clock_gettime(CLOCK_REALTIME, &ts[2]); + + updateLease4Test(); + clock_gettime(CLOCK_REALTIME, &ts[3]); + + deleteLease4Test(); + clock_gettime(CLOCK_REALTIME, &ts[4]); + + disconnect(); + + } catch (const std::string& e) { + cout << "Failed: " << e << endl; + return (-1); + } + + print_clock("Create leases4 ", Num_, ts[0], ts[1]); + // print_clock("Search leases4 ", num, ts[1], ts[2]); + // print_clock("Update leases4 ", num, ts[2], ts[3]); + // print_clock("Delete leases4 ", num, ts[3], ts[4]); + + return (0); +} + +class MySQL_uBenchmark: public uBenchmark { +public: + MySQL_uBenchmark(const string& hostname, const string& user, + const string& passwd, const string& db, + uint32_t num_iterations); + + virtual void printInfo(); + virtual void connect(); + virtual void disconnect(); + virtual void createLease4Test(); + virtual void searchLease4Test(); + virtual void updateLease4Test(); + virtual void deleteLease4Test(); + +protected: + void failure(const char* operation); + + std::string Hostname_; + std::string User_; + std::string Pass_; + std::string DB_; + MYSQL * Conn_; +}; + + +MySQL_uBenchmark::MySQL_uBenchmark(const string& hostname, const string& user, + const string& pass, const string& db, + uint32_t num_iterations) + :uBenchmark(num_iterations), Hostname_(hostname), User_(user), + Pass_(pass), DB_(db), Conn_(NULL) { + +} + +void MySQL_uBenchmark::failure(const char* operation) { stringstream tmp; - tmp << "Error " << mysql_errno(conn) << " during " << operation - << ": " << mysql_error(conn); + tmp << "Error " << mysql_errno(Conn_) << " during " << operation + << ": " << mysql_error(Conn_); throw tmp.str(); } -MYSQL * connect() { - MYSQL *conn = NULL; +void MySQL_uBenchmark::connect() { - conn = mysql_init(NULL); - if (!conn) { - failure(conn, "initializing MySQL library"); + Conn_ = mysql_init(NULL); + if (!Conn_) { + failure("initializing MySQL library"); } else { cout << "MySQL library init successful." << endl; } - if (!mysql_real_connect(conn, hostname, user, passwd, dbname, 0, NULL, 0)) { - failure(conn, "connecting to MySQL server"); + cout << "hostname=" << Hostname_ << ", user=" << User_ + << "pass=" << Pass_ << " db=" << DB_ << endl; + + if (!mysql_real_connect(Conn_, Hostname_.c_str(), User_.c_str(), + Pass_.c_str(), DB_.c_str(), 0, NULL, 0)) { + failure("connecting to MySQL server"); } else { cout << "MySQL connection established." << endl; } - string q = "delete from lease4"; - if (mysql_real_query(conn, q.c_str(), strlen(q.c_str()))) { - failure(conn, "dropping old lease4 entries."); + string q = "delete from lease4;"; + if (mysql_real_query(Conn_, q.c_str(), strlen(q.c_str()))) { + failure("dropping old lease4 entries."); } - - return (conn); } -bool disconnect(MYSQL * conn) { - if (!conn) { +void MySQL_uBenchmark::disconnect() { + if (!Conn_) { throw "NULL MySQL connection pointer."; } - mysql_close(conn); + mysql_close(Conn_); + Conn_ = NULL; } - - -bool createLease4Test(MYSQL * conn, uint32_t num) { - if (!conn) { - throw "NULL MySQL connection pointer."; +void MySQL_uBenchmark::createLease4Test() { + if (!Conn_) { + throw "Not connected to MySQL server."; } uint32_t addr = 0x01000000; // Let's start with 1.0.0.0 address @@ -81,7 +203,7 @@ bool createLease4Test(MYSQL * conn, uint32_t num) { client_id[i] = 33 + i; } - for (uint32_t i = 0; i < num; i++) { + for (uint32_t i = 0; i < Num_; i++) { stringstream cltt; cltt << "2012-07-11 15:43:" << (i % 60); @@ -95,9 +217,9 @@ bool createLease4Test(MYSQL * conn, uint32_t num) { "fqdn_fwd,fqdn_rev) VALUES("); end = query + strlen(query); end += sprintf(end, "%u,\'", addr); - end += mysql_real_escape_string(conn, end, hwaddr, hwaddr_len); + end += mysql_real_escape_string(Conn_, end, hwaddr, hwaddr_len); end += sprintf(end,"\',\'"); - end += mysql_real_escape_string(conn, end, client_id, client_id_len); + end += mysql_real_escape_string(Conn_, end, client_id, client_id_len); end += sprintf(end, "\',%d,%d,'%s',%d,%s,\'%s\',%s,%s);", valid_lft, recycle_time, cltt.str().c_str(), pool_id, (fixed?"true":"false"), hostname.c_str(), @@ -106,91 +228,50 @@ bool createLease4Test(MYSQL * conn, uint32_t num) { // options and comments fields are not set unsigned int len = end - query; - if (mysql_real_query(conn, query, len)) { + if (mysql_real_query(Conn_, query, len)) { // something failed. - failure(conn, "INSERT query"); + failure("INSERT query"); } else { printf("."); - fflush(stdout); }; } printf("\n"); - fflush(stdout); } -bool searchLease4Test(MYSQL * conn, uint32_t num) { - if (!conn) { - throw "NULL MySQL connection pointer."; +void MySQL_uBenchmark::searchLease4Test() { + if (!Conn_) { + throw "Not connected to MySQL server."; } } -bool updateLease4Test(MYSQL* conn, uint32_t num) { - if (!conn) { - throw "NULL MySQL connection pointer."; +void MySQL_uBenchmark::updateLease4Test() { + if (!Conn_) { + throw "Not connected to MySQL server."; } } -bool deleteLease4Test(MYSQL* conn, uint32_t num) { - if (!conn) { - throw "NULL MySQL connection pointer."; +void MySQL_uBenchmark::deleteLease4Test() { + if (!Conn_) { + throw "Not connected to MySQL server."; } } -void print_clock(const std::string& operation, uint32_t num, - const struct timespec& before, - const struct timespec& after) { - long int tv_sec = after.tv_sec - before.tv_sec; - - long int tv_nsec = after.tv_nsec - before.tv_nsec; - - cout << "after.tv_nsec=" << after.tv_nsec - << " before.tv_nsec=" << before.tv_nsec << endl; - - if (tv_nsec < 0) { - tv_sec++; - tv_nsec += 1000000000; // 10^9 - } - - double oneoper = (tv_nsec/1000 + tv_sec*1000000)/num; - - cout << "Operation " << operation << " repeated " << num << " times took " - << tv_sec << " seconds, " << tv_nsec/1000 << " us, 1 operation took " - << oneoper << "us" << endl; - +void MySQL_uBenchmark::printInfo() { + cout << "MySQL client version is " << mysql_get_client_info() << endl; } -int main(int argc, const char * argv[]) { - cout << "MySQL client version is " << mysql_get_client_info() << endl; +int main(int argc, const char * argv[]) { + const char * hostname ="localhost"; + const char * user = "root"; + const char * passwd = "foobletch"; + const char * dbname = "kea"; uint32_t num = 100; - struct timespec ts[5]; - try { - MYSQL *conn = connect(); + MySQL_uBenchmark bench(hostname, user, passwd, dbname, num); - clock_gettime(CLOCK_REALTIME, &ts[0]); + int result = bench.run(); - createLease4Test(conn, num); - clock_gettime(CLOCK_REALTIME, &ts[1]); - - searchLease4Test(conn, num); - clock_gettime(CLOCK_REALTIME, &ts[2]); - - updateLease4Test(conn, num); - clock_gettime(CLOCK_REALTIME, &ts[3]); - - deleteLease4Test(conn, num); - clock_gettime(CLOCK_REALTIME, &ts[4]); - - disconnect(conn); - - } catch (const std::string& e) { - cout << "Failed: " << e << endl; - return (-1); - } - - print_clock("Create leases4 ", num, ts[0], ts[1]); - - return (0); + return (result); }