mysql_ubench.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <time.h>
  8. #include <mysql/mysql.h>
  9. using namespace std;
  10. class uBenchmark {
  11. public:
  12. uBenchmark(uint32_t numInterations);
  13. virtual void printInfo() = 0;
  14. virtual void connect() = 0;
  15. virtual void disconnect() = 0;
  16. virtual void createLease4Test() = 0;
  17. virtual void searchLease4Test() = 0;
  18. virtual void updateLease4Test() = 0;
  19. virtual void deleteLease4Test() = 0;
  20. virtual void failure(const char* operation);
  21. void print_clock(const std::string& operation, uint32_t num,
  22. const struct timespec& before,
  23. const struct timespec& after);
  24. int run();
  25. protected:
  26. uint32_t Num_; // number of operations (e.g. insert lease num times)
  27. const static uint32_t BASE_ADDR4 = 0x01000000; // let's start from 1.0.0.0 address
  28. // five timestamps (1 at the beginning and 4 after each step)
  29. struct timespec ts[5];
  30. };
  31. uBenchmark::uBenchmark(uint32_t iterations)
  32. :Num_(iterations) {
  33. }
  34. void uBenchmark::failure(const char* operation) {
  35. cout << "Error during " << operation << endl;
  36. throw string(operation);
  37. }
  38. void uBenchmark::print_clock(const std::string& operation, uint32_t num,
  39. const struct timespec& before,
  40. const struct timespec& after) {
  41. long int tv_sec = after.tv_sec - before.tv_sec;
  42. long int tv_nsec = after.tv_nsec - before.tv_nsec;
  43. if (tv_nsec < 0) {
  44. tv_sec++;
  45. tv_nsec += 1000000000; // 10^9
  46. }
  47. double oneoper = (tv_nsec/1000 + tv_sec*1000000)/num;
  48. cout << "Operation " << operation << " repeated " << num << " times took "
  49. << tv_sec << " seconds, " << tv_nsec/1000 << " us, 1 operation took "
  50. << oneoper << "us (or " << (1000000/oneoper) << " oper/sec)" << endl;
  51. }
  52. int uBenchmark::run() {
  53. try {
  54. connect();
  55. clock_gettime(CLOCK_REALTIME, &ts[0]);
  56. createLease4Test();
  57. clock_gettime(CLOCK_REALTIME, &ts[1]);
  58. searchLease4Test();
  59. clock_gettime(CLOCK_REALTIME, &ts[2]);
  60. updateLease4Test();
  61. clock_gettime(CLOCK_REALTIME, &ts[3]);
  62. deleteLease4Test();
  63. clock_gettime(CLOCK_REALTIME, &ts[4]);
  64. disconnect();
  65. } catch (const std::string& e) {
  66. cout << "Failed: " << e << endl;
  67. return (-1);
  68. }
  69. print_clock("Create leases4 ", Num_, ts[0], ts[1]);
  70. print_clock("Search leases4 ", Num_, ts[1], ts[2]);
  71. print_clock("Update leases4 ", Num_, ts[2], ts[3]);
  72. print_clock("Delete leases4 ", Num_, ts[3], ts[4]);
  73. return (0);
  74. }
  75. class MySQL_uBenchmark: public uBenchmark {
  76. public:
  77. MySQL_uBenchmark(const string& hostname, const string& user,
  78. const string& passwd, const string& db,
  79. uint32_t num_iterations);
  80. virtual void printInfo();
  81. virtual void connect();
  82. virtual void disconnect();
  83. virtual void createLease4Test();
  84. virtual void searchLease4Test();
  85. virtual void updateLease4Test();
  86. virtual void deleteLease4Test();
  87. protected:
  88. const static bool CONFIRM_UPDATE = false;
  89. const static bool CONFIRM_DELETE = false;
  90. void failure(const char* operation);
  91. std::string Hostname_;
  92. std::string User_;
  93. std::string Pass_;
  94. std::string DB_;
  95. MYSQL * Conn_;
  96. };
  97. MySQL_uBenchmark::MySQL_uBenchmark(const string& hostname, const string& user,
  98. const string& pass, const string& db,
  99. uint32_t num_iterations)
  100. :uBenchmark(num_iterations), Hostname_(hostname), User_(user),
  101. Pass_(pass), DB_(db), Conn_(NULL) {
  102. }
  103. void MySQL_uBenchmark::failure(const char* operation) {
  104. stringstream tmp;
  105. tmp << "Error " << mysql_errno(Conn_) << " during " << operation
  106. << ": " << mysql_error(Conn_);
  107. throw tmp.str();
  108. }
  109. void MySQL_uBenchmark::connect() {
  110. srandom(time(NULL));
  111. Conn_ = mysql_init(NULL);
  112. if (!Conn_) {
  113. failure("initializing MySQL library");
  114. } else {
  115. cout << "MySQL library init successful." << endl;
  116. }
  117. cout << "hostname=" << Hostname_ << ", user=" << User_
  118. << "pass=" << Pass_ << " db=" << DB_ << endl;
  119. if (!mysql_real_connect(Conn_, Hostname_.c_str(), User_.c_str(),
  120. Pass_.c_str(), DB_.c_str(), 0, NULL, 0)) {
  121. failure("connecting to MySQL server");
  122. } else {
  123. cout << "MySQL connection established." << endl;
  124. }
  125. string q = "delete from lease4;";
  126. if (mysql_real_query(Conn_, q.c_str(), strlen(q.c_str()))) {
  127. failure("dropping old lease4 entries.");
  128. }
  129. }
  130. void MySQL_uBenchmark::disconnect() {
  131. if (!Conn_) {
  132. throw "NULL MySQL connection pointer.";
  133. }
  134. mysql_close(Conn_);
  135. Conn_ = NULL;
  136. }
  137. void MySQL_uBenchmark::createLease4Test() {
  138. if (!Conn_) {
  139. throw "Not connected to MySQL server.";
  140. }
  141. uint32_t addr = BASE_ADDR4; // Let's start with 1.0.0.0 address
  142. char hwaddr[20];
  143. uint8_t hwaddr_len = 20; // not a real field
  144. char client_id[128];
  145. uint8_t client_id_len = 128;
  146. uint32_t valid_lft = 1000; // we can use the same value for all leases
  147. uint32_t recycle_time = 0; // not supported in any foresable future,
  148. // so keep this as 0
  149. string cltt = "now()"; // timestamp
  150. uint32_t pool_id = 0; // let's use pools 0-99
  151. bool fixed = false; //
  152. string hostname("foo"); // will generate it dynamically
  153. bool fqdn_fwd = true; // let's pretend to do AAAA update
  154. bool fqdn_rev = true; // let's pretend to do PTR update
  155. printf("CREATE: ");
  156. for (uint8_t i = 0; i < 20; i++) {
  157. hwaddr[i] = 65 + i;
  158. }
  159. for (uint8_t i = 0; i < 128; i++) {
  160. client_id[i] = 33 + i;
  161. }
  162. for (uint32_t i = 0; i < Num_; i++) {
  163. stringstream cltt;
  164. cltt << "2012-07-11 15:43:" << (i % 60);
  165. addr++;
  166. // the first address is 1.0.0.0.
  167. char query[2000], * end;
  168. strcpy(query, "INSERT INTO lease4(addr,hwaddr,client_id,"
  169. "valid_lft,recycle_time,cltt,pool_id,fixed,hostname,"
  170. "fqdn_fwd,fqdn_rev) VALUES(");
  171. end = query + strlen(query);
  172. end += sprintf(end, "%u,\'", addr);
  173. end += mysql_real_escape_string(Conn_, end, hwaddr, hwaddr_len);
  174. end += sprintf(end,"\',\'");
  175. end += mysql_real_escape_string(Conn_, end, client_id, client_id_len);
  176. end += sprintf(end, "\',%d,%d,'%s',%d,%s,\'%s\',%s,%s);",
  177. valid_lft, recycle_time, cltt.str().c_str(),
  178. pool_id, (fixed?"true":"false"), hostname.c_str(),
  179. (fqdn_fwd?"true":"false"), (fqdn_rev?"true":"false"));
  180. // lease_id field is set automatically
  181. // options and comments fields are not set
  182. unsigned int len = end - query;
  183. if (mysql_real_query(Conn_, query, len)) {
  184. // something failed.
  185. failure("INSERT query");
  186. } else {
  187. printf(".");
  188. };
  189. }
  190. printf("\n");
  191. }
  192. void MySQL_uBenchmark::searchLease4Test() {
  193. if (!Conn_) {
  194. throw "Not connected to MySQL server.";
  195. }
  196. // this formula should roughly find something a lease in 90% cases
  197. float hitRatio = 0.9;
  198. /* cout << "range=" << int(Num_ / hitRatio) << " minAddr=" << hex
  199. << BASE_ADDR4 << " maxAddr=" << BASE_ADDR4 + int(Num_ / hitRatio)
  200. << dec << endl; */
  201. printf("RETRIEVE: ");
  202. for (uint32_t i = 0; i < Num_; i++) {
  203. uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
  204. char query[2000];
  205. sprintf(query, "SELECT lease_id,addr,hwaddr,client_id,valid_lft,"
  206. "cltt,pool_id,fixed,hostname,fqdn_fwd,fqdn_rev "
  207. "FROM lease4 where addr=%d", x);
  208. mysql_real_query(Conn_, query, strlen(query));
  209. MYSQL_RES * result = mysql_store_result(Conn_);
  210. int num_rows = mysql_num_rows(result);
  211. int num_fields = mysql_num_fields(result);
  212. if ( (num_rows != 0) && (num_rows != 1) ) {
  213. stringstream tmp;
  214. tmp << "Search: DB returned " << num_rows << " leases for address "
  215. << hex << x << dec;
  216. failure(tmp.str().c_str());
  217. }
  218. if (num_rows) {
  219. MYSQL_ROW row = mysql_fetch_row(result);
  220. // pretend to do something with it
  221. printf(".");
  222. /* printf("lease_id=%s addr=%s valid_lft=%s cltt=%s\n",
  223. (row[0]?row[0]:"NULL"),
  224. (row[1]?row[1]:"NULL"),
  225. (row[4]?row[4]:"NULL"),
  226. (row[5]?row[5]:"NULL")); */
  227. mysql_free_result(result);
  228. } else {
  229. // printf("Address %x not found.\n", x);
  230. printf("x");
  231. }
  232. }
  233. printf("\n");
  234. }
  235. void MySQL_uBenchmark::updateLease4Test() {
  236. if (!Conn_) {
  237. throw "Not connected to MySQL server.";
  238. }
  239. printf("UPDATE: ");
  240. for (uint32_t i = 0; i < Num_; i++) {
  241. uint32_t x = BASE_ADDR4 + random() % Num_;
  242. char query[2000];
  243. sprintf(query, "UPDATE lease4 SET valid_lft=1002, cltt=now() WHERE addr=%d", x);
  244. mysql_real_query(Conn_, query, strlen(query));
  245. MYSQL_RES * result = NULL;
  246. if (CONFIRM_UPDATE) {
  247. mysql_store_result(Conn_);
  248. int num_rows = mysql_num_rows(result);
  249. int num_fields = mysql_num_fields(result);
  250. if ( (num_rows != 1) ) {
  251. stringstream tmp;
  252. tmp << "Search: DB returned " << num_rows << " leases for address "
  253. << hex << x << dec;
  254. failure(tmp.str().c_str());
  255. }
  256. MYSQL_ROW row = mysql_fetch_row(result);
  257. // pretend to do something with it
  258. printf("lease_id=%s addr=%s valid_lft=%s cltt=%s\n",
  259. (row[0]?row[0]:"NULL"),
  260. (row[1]?row[1]:"NULL"),
  261. (row[4]?row[4]:"NULL"),
  262. (row[5]?row[5]:"NULL"));
  263. mysql_free_result(result);
  264. }
  265. printf(".");
  266. }
  267. printf("\n");
  268. }
  269. void MySQL_uBenchmark::deleteLease4Test() {
  270. if (!Conn_) {
  271. throw "Not connected to MySQL server.";
  272. }
  273. printf("DELETE: ");
  274. for (uint32_t i = 0; i < Num_; i++) {
  275. uint32_t x = BASE_ADDR4 + i;
  276. char query[2000];
  277. sprintf(query, "DELETE FROM lease4 WHERE addr=%d", x);
  278. mysql_real_query(Conn_, query, strlen(query));
  279. MYSQL_RES * result = NULL;
  280. if (CONFIRM_DELETE) {
  281. mysql_store_result(Conn_);
  282. int num_rows = mysql_num_rows(result);
  283. int num_fields = mysql_num_fields(result);
  284. if ( (num_rows != 1) ) {
  285. stringstream tmp;
  286. tmp << "Search: DB returned " << num_rows << " leases for address "
  287. << hex << x << dec;
  288. failure(tmp.str().c_str());
  289. }
  290. MYSQL_ROW row = mysql_fetch_row(result);
  291. // pretend to do something with it
  292. printf("lease_id=%s addr=%s valid_lft=%s cltt=%s\n",
  293. (row[0]?row[0]:"NULL"),
  294. (row[1]?row[1]:"NULL"),
  295. (row[4]?row[4]:"NULL"),
  296. (row[5]?row[5]:"NULL"));
  297. mysql_free_result(result);
  298. }
  299. printf(".");
  300. }
  301. printf("\n");
  302. }
  303. void MySQL_uBenchmark::printInfo() {
  304. cout << "MySQL client version is " << mysql_get_client_info() << endl;
  305. }
  306. int main(int argc, const char * argv[]) {
  307. const char * hostname ="localhost";
  308. const char * user = "root";
  309. const char * passwd = "secret";
  310. const char * dbname = "kea";
  311. uint32_t num = 100;
  312. MySQL_uBenchmark bench(hostname, user, passwd, dbname, num);
  313. int result = bench.run();
  314. return (result);
  315. }