mysql_ubench.cc 4.3 KB

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