mysql_ubench.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include <time.h>
  7. #include <mysql/mysql.h>
  8. using namespace std;
  9. const char * hostname ="localhost";
  10. const char * user = "root";
  11. const char * passwd = "foobletch";
  12. const char * dbname = "kea";
  13. void failure(MYSQL* conn, const char* operation) {
  14. stringstream tmp;
  15. tmp << "Error " << mysql_errno(conn) << " during " << operation
  16. << ": " << mysql_error(conn);
  17. throw tmp.str();
  18. }
  19. MYSQL * connect() {
  20. MYSQL *conn = NULL;
  21. conn = mysql_init(NULL);
  22. if (!conn) {
  23. failure(conn, "initializing MySQL library");
  24. } else {
  25. cout << "MySQL library init successful." << endl;
  26. }
  27. if (!mysql_real_connect(conn, hostname, user, passwd, dbname, 0, NULL, 0)) {
  28. failure(conn, "connecting to MySQL server");
  29. } else {
  30. cout << "MySQL connection established." << endl;
  31. }
  32. string q = "delete from lease4";
  33. if (mysql_real_query(conn, q.c_str(), strlen(q.c_str()))) {
  34. failure(conn, "dropping old lease4 entries.");
  35. }
  36. return (conn);
  37. }
  38. bool disconnect(MYSQL * conn) {
  39. if (!conn) {
  40. throw "NULL MySQL connection pointer.";
  41. }
  42. mysql_close(conn);
  43. }
  44. bool createLease4Test(MYSQL * conn, uint32_t num) {
  45. if (!conn) {
  46. throw "NULL MySQL connection pointer.";
  47. }
  48. uint32_t addr = 0x01000000; // Let's start with 1.0.0.0 address
  49. char hwaddr[20];
  50. uint8_t hwaddr_len = 20; // not a real field
  51. char client_id[128];
  52. uint8_t client_id_len = 128;
  53. uint32_t valid_lft = 1000; // we can use the same value for all leases
  54. uint32_t recycle_time = 0; // not supported in any foresable future,
  55. // so keep this as 0
  56. string cltt = "now()"; // timestamp
  57. uint32_t pool_id = 0; // let's use pools 0-99
  58. bool fixed = false; //
  59. string hostname("foo"); // will generate it dynamically
  60. bool fqdn_fwd = true; // let's pretend to do AAAA update
  61. bool fqdn_rev = true; // let's pretend to do PTR update
  62. for (uint8_t i = 0; i < 20; i++) {
  63. hwaddr[i] = 65 + i;
  64. }
  65. for (uint8_t i = 0; i < 128; i++) {
  66. client_id[i] = 33 + i;
  67. }
  68. for (uint32_t i = 0; i < num; i++) {
  69. stringstream cltt;
  70. cltt << "2012-07-11 15:43:" << (i % 60);
  71. addr++;
  72. // the first address is 1.0.0.0.
  73. char query[2000], * end;
  74. strcpy(query, "INSERT INTO lease4(addr,hwaddr,client_id,"
  75. "valid_lft,recycle_time,cltt,pool_id,fixed,hostname,"
  76. "fqdn_fwd,fqdn_rev) VALUES(");
  77. end = query + strlen(query);
  78. end += sprintf(end, "%u,\'", addr);
  79. end += mysql_real_escape_string(conn, end, hwaddr, hwaddr_len);
  80. end += sprintf(end,"\',\'");
  81. end += mysql_real_escape_string(conn, end, client_id, client_id_len);
  82. end += sprintf(end, "\',%d,%d,'%s',%d,%s,\'%s\',%s,%s);",
  83. valid_lft, recycle_time, cltt.str().c_str(),
  84. pool_id, (fixed?"true":"false"), hostname.c_str(),
  85. (fqdn_fwd?"true":"false"), (fqdn_rev?"true":"false"));
  86. // lease_id field is set automatically
  87. // options and comments fields are not set
  88. unsigned int len = end - query;
  89. if (mysql_real_query(conn, query, len)) {
  90. // something failed.
  91. failure(conn, "INSERT query");
  92. } else {
  93. printf(".");
  94. fflush(stdout);
  95. };
  96. }
  97. printf("\n");
  98. fflush(stdout);
  99. }
  100. bool searchLease4Test(MYSQL * conn, uint32_t num) {
  101. if (!conn) {
  102. throw "NULL MySQL connection pointer.";
  103. }
  104. }
  105. bool updateLease4Test(MYSQL* conn, uint32_t num) {
  106. if (!conn) {
  107. throw "NULL MySQL connection pointer.";
  108. }
  109. }
  110. bool deleteLease4Test(MYSQL* conn, uint32_t num) {
  111. if (!conn) {
  112. throw "NULL MySQL connection pointer.";
  113. }
  114. }
  115. void print_clock(const std::string& operation, uint32_t num,
  116. const struct timespec& before,
  117. const struct timespec& after) {
  118. long int tv_sec = after.tv_sec - before.tv_sec;
  119. long int tv_nsec = after.tv_nsec - before.tv_nsec;
  120. cout << "after.tv_nsec=" << after.tv_nsec
  121. << " before.tv_nsec=" << before.tv_nsec << endl;
  122. if (tv_nsec < 0) {
  123. tv_sec++;
  124. tv_nsec += 1000000000; // 10^9
  125. }
  126. double oneoper = (tv_nsec/1000 + tv_sec*1000000)/num;
  127. cout << "Operation " << operation << " repeated " << num << " times took "
  128. << tv_sec << " seconds, " << tv_nsec/1000 << " us, 1 operation took "
  129. << oneoper << "us" << endl;
  130. }
  131. int main(int argc, const char * argv[]) {
  132. cout << "MySQL client version is " << mysql_get_client_info() << endl;
  133. uint32_t num = 100;
  134. struct timespec ts[5];
  135. try {
  136. MYSQL *conn = connect();
  137. clock_gettime(CLOCK_REALTIME, &ts[0]);
  138. createLease4Test(conn, num);
  139. clock_gettime(CLOCK_REALTIME, &ts[1]);
  140. searchLease4Test(conn, num);
  141. clock_gettime(CLOCK_REALTIME, &ts[2]);
  142. updateLease4Test(conn, num);
  143. clock_gettime(CLOCK_REALTIME, &ts[3]);
  144. deleteLease4Test(conn, num);
  145. clock_gettime(CLOCK_REALTIME, &ts[4]);
  146. disconnect(conn);
  147. } catch (const std::string& e) {
  148. cout << "Failed: " << e << endl;
  149. return (-1);
  150. }
  151. print_clock("Create leases4 ", num, ts[0], ts[1]);
  152. return (0);
  153. }