mysql_ubench.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. srandom(time(NULL));
  38. Conn_ = mysql_init(NULL);
  39. if (!Conn_) {
  40. failure("initializing MySQL library");
  41. } else {
  42. cout << "MySQL library init successful." << endl;
  43. }
  44. cout << "hostname=" << Hostname_ << ", user=" << User_
  45. << "pass=" << Pass_ << " db=" << DB_ << endl;
  46. if (!mysql_real_connect(Conn_, Hostname_.c_str(), User_.c_str(),
  47. Pass_.c_str(), DB_.c_str(), 0, NULL, 0)) {
  48. failure("connecting to MySQL server");
  49. } else {
  50. cout << "MySQL connection established." << endl;
  51. }
  52. string q = "delete from lease4;";
  53. if (mysql_real_query(Conn_, q.c_str(), strlen(q.c_str()))) {
  54. failure("dropping old lease4 entries.");
  55. }
  56. }
  57. void MySQL_uBenchmark::disconnect() {
  58. if (!Conn_) {
  59. throw "NULL MySQL connection pointer.";
  60. }
  61. mysql_close(Conn_);
  62. Conn_ = NULL;
  63. }
  64. void MySQL_uBenchmark::createLease4Test() {
  65. if (!Conn_) {
  66. throw "Not connected to MySQL server.";
  67. }
  68. uint32_t addr = BASE_ADDR4; // Let's start with 1.0.0.0 address
  69. char hwaddr[20];
  70. uint8_t hwaddr_len = 20; // not a real field
  71. char client_id[128];
  72. uint8_t client_id_len = 128;
  73. uint32_t valid_lft = 1000; // we can use the same value for all leases
  74. uint32_t recycle_time = 0; // not supported in any foresable future,
  75. // so keep this as 0
  76. string cltt = "now()"; // timestamp
  77. uint32_t pool_id = 0; // let's use pools 0-99
  78. bool fixed = false; //
  79. string hostname("foo"); // will generate it dynamically
  80. bool fqdn_fwd = true; // let's pretend to do AAAA update
  81. bool fqdn_rev = true; // let's pretend to do PTR update
  82. printf("CREATE: ");
  83. for (uint8_t i = 0; i < 20; i++) {
  84. hwaddr[i] = 65 + i;
  85. }
  86. for (uint8_t i = 0; i < 128; i++) {
  87. client_id[i] = 33 + i;
  88. }
  89. for (uint32_t i = 0; i < Num_; i++) {
  90. stringstream cltt;
  91. cltt << "2012-07-11 15:43:" << (i % 60);
  92. addr++;
  93. // the first address is 1.0.0.0.
  94. char query[2000], * end;
  95. strcpy(query, "INSERT INTO lease4(addr,hwaddr,client_id,"
  96. "valid_lft,recycle_time,cltt,pool_id,fixed,hostname,"
  97. "fqdn_fwd,fqdn_rev) VALUES(");
  98. end = query + strlen(query);
  99. end += sprintf(end, "%u,\'", addr);
  100. end += mysql_real_escape_string(Conn_, end, hwaddr, hwaddr_len);
  101. end += sprintf(end,"\',\'");
  102. end += mysql_real_escape_string(Conn_, end, client_id, client_id_len);
  103. end += sprintf(end, "\',%d,%d,'%s',%d,%s,\'%s\',%s,%s);",
  104. valid_lft, recycle_time, cltt.str().c_str(),
  105. pool_id, (fixed?"true":"false"), hostname.c_str(),
  106. (fqdn_fwd?"true":"false"), (fqdn_rev?"true":"false"));
  107. // lease_id field is set automatically
  108. // options and comments fields are not set
  109. unsigned int len = end - query;
  110. if (mysql_real_query(Conn_, query, len)) {
  111. // something failed.
  112. failure("INSERT query");
  113. } else {
  114. printf(".");
  115. };
  116. }
  117. printf("\n");
  118. }
  119. void MySQL_uBenchmark::searchLease4Test() {
  120. if (!Conn_) {
  121. throw "Not connected to MySQL server.";
  122. }
  123. // this formula should roughly find something a lease in 90% cases
  124. float hitRatio = 0.9;
  125. /* cout << "range=" << int(Num_ / hitRatio) << " minAddr=" << hex
  126. << BASE_ADDR4 << " maxAddr=" << BASE_ADDR4 + int(Num_ / hitRatio)
  127. << dec << endl; */
  128. printf("RETRIEVE: ");
  129. for (uint32_t i = 0; i < Num_; i++) {
  130. uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
  131. char query[2000];
  132. sprintf(query, "SELECT lease_id,addr,hwaddr,client_id,valid_lft,"
  133. "cltt,pool_id,fixed,hostname,fqdn_fwd,fqdn_rev "
  134. "FROM lease4 where addr=%d", x);
  135. mysql_real_query(Conn_, query, strlen(query));
  136. MYSQL_RES * result = mysql_store_result(Conn_);
  137. int num_rows = mysql_num_rows(result);
  138. int num_fields = mysql_num_fields(result);
  139. if ( (num_rows != 0) && (num_rows != 1) ) {
  140. stringstream tmp;
  141. tmp << "Search: DB returned " << num_rows << " leases for address "
  142. << hex << x << dec;
  143. failure(tmp.str().c_str());
  144. }
  145. if (num_rows) {
  146. MYSQL_ROW row = mysql_fetch_row(result);
  147. // pretend to do something with it
  148. printf(".");
  149. /* printf("lease_id=%s addr=%s valid_lft=%s cltt=%s\n",
  150. (row[0]?row[0]:"NULL"),
  151. (row[1]?row[1]:"NULL"),
  152. (row[4]?row[4]:"NULL"),
  153. (row[5]?row[5]:"NULL")); */
  154. mysql_free_result(result);
  155. } else {
  156. // printf("Address %x not found.\n", x);
  157. printf("x");
  158. }
  159. }
  160. printf("\n");
  161. }
  162. void MySQL_uBenchmark::updateLease4Test() {
  163. if (!Conn_) {
  164. throw "Not connected to MySQL server.";
  165. }
  166. printf("UPDATE: ");
  167. for (uint32_t i = 0; i < Num_; i++) {
  168. uint32_t x = BASE_ADDR4 + random() % Num_;
  169. char query[2000];
  170. sprintf(query, "UPDATE lease4 SET valid_lft=1002, cltt=now() WHERE addr=%d", x);
  171. mysql_real_query(Conn_, query, strlen(query));
  172. MYSQL_RES * result = NULL;
  173. printf(".");
  174. }
  175. printf("\n");
  176. }
  177. void MySQL_uBenchmark::deleteLease4Test() {
  178. if (!Conn_) {
  179. throw "Not connected to MySQL server.";
  180. }
  181. printf("DELETE: ");
  182. for (uint32_t i = 0; i < Num_; i++) {
  183. uint32_t x = BASE_ADDR4 + i;
  184. char query[2000];
  185. sprintf(query, "DELETE FROM lease4 WHERE addr=%d", x);
  186. mysql_real_query(Conn_, query, strlen(query));
  187. MYSQL_RES * result = NULL;
  188. printf(".");
  189. }
  190. printf("\n");
  191. }
  192. void MySQL_uBenchmark::printInfo() {
  193. cout << "MySQL client version is " << mysql_get_client_info() << endl;
  194. }
  195. int main(int argc, const char * argv[]) {
  196. const char * hostname ="localhost";
  197. const char * user = "root";
  198. const char * passwd = "secret";
  199. const char * dbname = "kea";
  200. uint32_t num = 10000;
  201. MySQL_uBenchmark bench(hostname, user, passwd, dbname, num);
  202. int result = bench.run();
  203. return (result);
  204. }