mysql_ubench.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <iostream>
  15. #include <sstream>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <mysql/mysql.h>
  21. #include "benchmark.h"
  22. #include "mysql_ubench.h"
  23. using namespace std;
  24. MySQL_uBenchmark::MySQL_uBenchmark(const string& hostname, const string& user,
  25. const string& pass, const string& db,
  26. uint32_t num_iterations)
  27. :uBenchmark(num_iterations), Hostname_(hostname), User_(user),
  28. Pass_(pass), DB_(db), Conn_(NULL) {
  29. }
  30. void MySQL_uBenchmark::failure(const char* operation) {
  31. stringstream tmp;
  32. tmp << "Error " << mysql_errno(Conn_) << " during " << operation
  33. << ": " << mysql_error(Conn_);
  34. throw tmp.str();
  35. }
  36. void MySQL_uBenchmark::connect() {
  37. Conn_ = mysql_init(NULL);
  38. if (!Conn_) {
  39. failure("initializing MySQL library");
  40. } else {
  41. cout << "MySQL library init successful." << endl;
  42. }
  43. cout << "hostname=" << Hostname_ << ", user=" << User_
  44. << "pass=" << Pass_ << " db=" << DB_ << endl;
  45. if (!mysql_real_connect(Conn_, Hostname_.c_str(), User_.c_str(),
  46. Pass_.c_str(), DB_.c_str(), 0, NULL, 0)) {
  47. failure("connecting to MySQL server");
  48. } else {
  49. cout << "MySQL connection established." << endl;
  50. }
  51. string q = "delete from lease4;";
  52. if (mysql_real_query(Conn_, q.c_str(), strlen(q.c_str()))) {
  53. failure("dropping old lease4 entries.");
  54. }
  55. }
  56. void MySQL_uBenchmark::disconnect() {
  57. if (!Conn_) {
  58. throw "NULL MySQL connection pointer.";
  59. }
  60. mysql_close(Conn_);
  61. Conn_ = NULL;
  62. }
  63. void MySQL_uBenchmark::createLease4Test() {
  64. if (!Conn_) {
  65. throw "Not connected to MySQL server.";
  66. }
  67. uint32_t addr = BASE_ADDR4; // Let's start with 1.0.0.0 address
  68. char hwaddr[20];
  69. uint8_t hwaddr_len = 20; // not a real field
  70. char client_id[128];
  71. uint8_t client_id_len = 128;
  72. uint32_t valid_lft = 1000; // we can use the same value for all leases
  73. uint32_t recycle_time = 0; // not supported in any foresable future,
  74. // so keep this as 0
  75. string cltt = "now()"; // timestamp
  76. uint32_t pool_id = 0; // let's use pools 0-99
  77. bool fixed = false; //
  78. string hostname("foo"); // will generate it dynamically
  79. bool fqdn_fwd = true; // let's pretend to do AAAA update
  80. bool fqdn_rev = true; // let's pretend to do PTR update
  81. printf("CREATE: ");
  82. for (uint8_t i = 0; i < 20; i++) {
  83. hwaddr[i] = 65 + i;
  84. }
  85. for (uint8_t i = 0; i < 128; i++) {
  86. client_id[i] = 33 + i;
  87. }
  88. for (uint32_t i = 0; i < Num_; i++) {
  89. stringstream cltt;
  90. cltt << "2012-07-11 15:43:" << (i % 60);
  91. addr++;
  92. // the first address is 1.0.0.0.
  93. char query[2000], * end;
  94. strcpy(query, "INSERT INTO lease4(addr,hwaddr,client_id,"
  95. "valid_lft,recycle_time,cltt,pool_id,fixed,hostname,"
  96. "fqdn_fwd,fqdn_rev) VALUES(");
  97. end = query + strlen(query);
  98. end += sprintf(end, "%u,\'", addr);
  99. end += mysql_real_escape_string(Conn_, end, hwaddr, hwaddr_len);
  100. end += sprintf(end,"\',\'");
  101. end += mysql_real_escape_string(Conn_, end, client_id, client_id_len);
  102. end += sprintf(end, "\',%d,%d,'%s',%d,%s,\'%s\',%s,%s);",
  103. valid_lft, recycle_time, cltt.str().c_str(),
  104. pool_id, (fixed?"true":"false"), hostname.c_str(),
  105. (fqdn_fwd?"true":"false"), (fqdn_rev?"true":"false"));
  106. // lease_id field is set automatically
  107. // options and comments fields are not set
  108. unsigned int len = end - query;
  109. if (mysql_real_query(Conn_, query, len)) {
  110. // something failed.
  111. failure("INSERT query");
  112. } else {
  113. printf(".");
  114. };
  115. }
  116. printf("\n");
  117. }
  118. void MySQL_uBenchmark::searchLease4Test() {
  119. if (!Conn_) {
  120. throw "Not connected to MySQL server.";
  121. }
  122. // this formula should roughly find something a lease in 90% cases
  123. float hitRatio = 0.9;
  124. /* cout << "range=" << int(Num_ / hitRatio) << " minAddr=" << hex
  125. << BASE_ADDR4 << " maxAddr=" << BASE_ADDR4 + int(Num_ / hitRatio)
  126. << dec << endl; */
  127. printf("RETRIEVE: ");
  128. for (uint32_t i = 0; i < Num_; i++) {
  129. uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
  130. char query[2000];
  131. sprintf(query, "SELECT lease_id,addr,hwaddr,client_id,valid_lft,"
  132. "cltt,pool_id,fixed,hostname,fqdn_fwd,fqdn_rev "
  133. "FROM lease4 where addr=%d", x);
  134. mysql_real_query(Conn_, query, strlen(query));
  135. MYSQL_RES * result = mysql_store_result(Conn_);
  136. int num_rows = mysql_num_rows(result);
  137. int num_fields = mysql_num_fields(result);
  138. if ( (num_rows != 0) && (num_rows != 1) ) {
  139. stringstream tmp;
  140. tmp << "Search: DB returned " << num_rows << " leases for address "
  141. << hex << x << dec;
  142. failure(tmp.str().c_str());
  143. }
  144. if (num_rows) {
  145. MYSQL_ROW row = mysql_fetch_row(result);
  146. // pretend to do something with it
  147. printf(".");
  148. /* printf("lease_id=%s addr=%s valid_lft=%s cltt=%s\n",
  149. (row[0]?row[0]:"NULL"),
  150. (row[1]?row[1]:"NULL"),
  151. (row[4]?row[4]:"NULL"),
  152. (row[5]?row[5]:"NULL")); */
  153. mysql_free_result(result);
  154. } else {
  155. // printf("Address %x not found.\n", x);
  156. printf("x");
  157. }
  158. }
  159. printf("\n");
  160. }
  161. void MySQL_uBenchmark::updateLease4Test() {
  162. if (!Conn_) {
  163. throw "Not connected to MySQL server.";
  164. }
  165. printf("UPDATE: ");
  166. for (uint32_t i = 0; i < Num_; i++) {
  167. uint32_t x = BASE_ADDR4 + random() % Num_;
  168. char query[2000];
  169. sprintf(query, "UPDATE lease4 SET valid_lft=1002, cltt=now() WHERE addr=%d", x);
  170. mysql_real_query(Conn_, query, strlen(query));
  171. MYSQL_RES * result = NULL;
  172. printf(".");
  173. }
  174. printf("\n");
  175. }
  176. void MySQL_uBenchmark::deleteLease4Test() {
  177. if (!Conn_) {
  178. throw "Not connected to MySQL server.";
  179. }
  180. printf("DELETE: ");
  181. for (uint32_t i = 0; i < Num_; i++) {
  182. uint32_t x = BASE_ADDR4 + i;
  183. char query[2000];
  184. sprintf(query, "DELETE FROM lease4 WHERE addr=%d", x);
  185. mysql_real_query(Conn_, query, strlen(query));
  186. MYSQL_RES * result = NULL;
  187. printf(".");
  188. }
  189. printf("\n");
  190. }
  191. void MySQL_uBenchmark::printInfo() {
  192. cout << "MySQL client version is " << mysql_get_client_info() << endl;
  193. }
  194. int main(int argc, const char * argv[]) {
  195. const char * hostname ="localhost";
  196. const char * user = "root";
  197. const char * passwd = "secret";
  198. const char * dbname = "kea";
  199. uint32_t num = 10000;
  200. MySQL_uBenchmark bench(hostname, user, passwd, dbname, num);
  201. int result = bench.run();
  202. return (result);
  203. }