mysql_ubench.cc 8.0 KB

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