mysql_ubench.cc 8.0 KB

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