mysql_ubench.cc 4.0 KB

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