mysql_ubench.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. size_t hwaddr_len = 20; // Not a real field
  79. char client_id[128];
  80. size_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 = 7; // not supported in any foresable future,
  83. char cltt[48]; // timestamp (specified as text)
  84. size_t cltt_len;
  85. sprintf(cltt, "2012-07-11 15:43:00");
  86. cltt_len = strlen(cltt);
  87. uint32_t pool_id = 1000; // Let's use pools 0-99
  88. bool fixed = false;
  89. char hostname[] = "foo"; // Will generate it dynamically
  90. size_t hostname_len;
  91. hostname_len = strlen(hostname);
  92. bool fqdn_fwd = true; // Let's pretend to do AAAA update
  93. bool fqdn_rev = true; // Let's pretend to do PTR update
  94. printf("CREATE: ");
  95. for (uint8_t i = 0; i < 20; i++) {
  96. hwaddr[i] = 65 + i;
  97. }
  98. hwaddr[19] = 0;
  99. for (uint8_t i = 0; i < 128; i++) {
  100. client_id[i] = 33 + i;
  101. }
  102. client_id[127] = 0;
  103. MYSQL_STMT * stmt = NULL;
  104. MYSQL_BIND bind[11]; // 11 parameters in the insert statement
  105. if (compiled_stmt_) {
  106. // create a statement once
  107. stmt = mysql_stmt_init(conn_);
  108. if (!stmt) {
  109. failure("Unable to create compiled statement, mysql_stmt_init() failed");
  110. }
  111. const char * statement = "INSERT INTO lease4(addr,hwaddr,client_id,"
  112. "valid_lft,recycle_time,cltt,pool_id,fixed,hostname,"
  113. "fqdn_fwd,fqdn_rev) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
  114. if (mysql_stmt_prepare(stmt, statement, strlen(statement) )) {
  115. failure("Failed to prepare statement, mysql_stmt_prepare() returned non-zero");
  116. }
  117. int param_cnt = mysql_stmt_param_count(stmt);
  118. if (param_cnt != 11) {
  119. failure("Parameter count sanity check failed.");
  120. }
  121. memset(bind, 0, sizeof(bind));
  122. // 1st parameter: IPv4 address
  123. bind[0].buffer_type = MYSQL_TYPE_LONG;
  124. bind[0].buffer = (&addr);
  125. bind[0].is_null = 0;
  126. bind[0].length = 0;
  127. // 2nd parameter: Hardware address
  128. bind[1].buffer_type = MYSQL_TYPE_VARCHAR;
  129. bind[1].buffer = hwaddr;
  130. bind[1].buffer_length = hwaddr_len;
  131. bind[1].is_null = 0;
  132. bind[1].length = &hwaddr_len;
  133. // 3rd parameter: Client-id
  134. bind[2].buffer_type = MYSQL_TYPE_VARCHAR;
  135. bind[2].buffer = client_id;
  136. bind[2].buffer_length = client_id_len;
  137. bind[2].is_null = 0;
  138. bind[2].length = &client_id_len;
  139. // 4th parameter: valid-lifetime
  140. bind[3].buffer_type = MYSQL_TYPE_LONG;
  141. bind[3].buffer = (&valid_lft);
  142. bind[3].is_null = 0;
  143. bind[3].length = 0;
  144. // 5th parameter: recycle-time
  145. bind[4].buffer_type = MYSQL_TYPE_LONG;
  146. bind[4].buffer = (&recycle_time);
  147. bind[4].is_null = 0;
  148. bind[4].length = 0;
  149. // 6th parameter: cltt
  150. bind[5].buffer_type = MYSQL_TYPE_TIMESTAMP;
  151. bind[5].buffer = cltt;
  152. bind[2].buffer_length = cltt_len;
  153. bind[5].is_null = 0;
  154. bind[5].length = &cltt_len;
  155. // 7th parameter: pool-id
  156. bind[6].buffer_type = MYSQL_TYPE_LONG;
  157. bind[6].buffer = &pool_id;
  158. bind[6].is_null = 0;
  159. bind[6].length = 0;
  160. // 8th parameter: fixed
  161. bind[7].buffer_type = MYSQL_TYPE_TINY;
  162. bind[7].buffer = &fixed;
  163. bind[7].is_null = 0;
  164. bind[7].length = 0;
  165. // 9th parameter: hostname
  166. bind[8].buffer_type = MYSQL_TYPE_VARCHAR;
  167. bind[8].buffer = hostname;
  168. bind[8].buffer_length = strlen(hostname);
  169. bind[8].is_null = 0;
  170. bind[8].length = &hostname_len;
  171. // 10th parameter: fqdn_fwd
  172. bind[9].buffer_type = MYSQL_TYPE_TINY;
  173. bind[9].buffer = &fqdn_fwd;
  174. bind[9].is_null = 0;
  175. bind[9].length = 0;
  176. // 11th parameter: fqdn_rev
  177. bind[10].buffer_type = MYSQL_TYPE_TINY;
  178. bind[10].buffer = &fqdn_rev;
  179. bind[10].is_null = 0;
  180. bind[10].length = 0;
  181. }
  182. for (uint32_t i = 0; i < num_; i++) {
  183. sprintf(cltt, "2012-07-11 15:43:%02d", i % 60);
  184. addr++;
  185. if (!compiled_stmt_) {
  186. // the first address is 1.0.0.0.
  187. char query[2000], * end;
  188. strcpy(query, "INSERT INTO lease4(addr,hwaddr,client_id,"
  189. "valid_lft,recycle_time,cltt,pool_id,fixed,hostname,"
  190. "fqdn_fwd,fqdn_rev) VALUES(");
  191. end = query + strlen(query);
  192. end += sprintf(end, "%u,\'", addr);
  193. end += mysql_real_escape_string(conn_, end, hwaddr, hwaddr_len);
  194. end += sprintf(end,"\',\'");
  195. end += mysql_real_escape_string(conn_, end, client_id, client_id_len);
  196. end += sprintf(end, "\',%d,%d,'%s',%d,%s,\'%s\',%s,%s);",
  197. valid_lft, recycle_time, cltt,
  198. pool_id, (fixed?"true":"false"), hostname,
  199. (fqdn_fwd?"true":"false"), (fqdn_rev?"true":"false"));
  200. // lease_id field is set automatically
  201. // options and comments fields are not set
  202. unsigned int len = end - query;
  203. if (mysql_real_query(conn_, query, len)) {
  204. // something failed.
  205. failure("INSERT query");
  206. }
  207. } else {
  208. // compiled statement
  209. if (mysql_stmt_bind_param(stmt, bind)) {
  210. failure("Failed to bind parameters: mysql_stmt_bind_param() returned non-zero");
  211. }
  212. if (mysql_stmt_execute(stmt)) {
  213. failure("Failed to execute statement: mysql_stmt_execute() returned non-zero");
  214. }
  215. }
  216. if (verbose_) {
  217. printf(".");
  218. }
  219. }
  220. if (compiled_stmt_) {
  221. if (mysql_stmt_close(stmt)) {
  222. failure("Failed to close compiled statement, mysql_stmt_close returned non-zero");
  223. }
  224. }
  225. printf("\n");
  226. }
  227. void MySQL_uBenchmark::searchLease4Test() {
  228. if (!conn_) {
  229. throw "Not connected to MySQL server.";
  230. }
  231. printf("RETRIEVE: ");
  232. uint32_t addr = 0;
  233. MYSQL_STMT * stmt = NULL;
  234. MYSQL_BIND bind[1]; // just a single element
  235. if (compiled_stmt_) {
  236. stmt = mysql_stmt_init(conn_);
  237. if (!stmt) {
  238. failure("Unable to create compiled statement");
  239. }
  240. const char * statement = "SELECT lease_id,addr,hwaddr,client_id,"
  241. "valid_lft, cltt,pool_id,fixed,hostname,fqdn_fwd,fqdn_rev "
  242. "FROM lease4 where addr=?";
  243. if (mysql_stmt_prepare(stmt, statement, strlen(statement))) {
  244. failure("Failed to prepare statement, mysql_stmt_prepare() returned non-zero");
  245. }
  246. int param_cnt = mysql_stmt_param_count(stmt);
  247. if (param_cnt != 1) {
  248. failure("Parameter count sanity check failed.");
  249. }
  250. memset(bind, 0, sizeof(bind));
  251. // 1st parameter: IPv4 address
  252. bind[0].buffer_type = MYSQL_TYPE_LONG;
  253. bind[0].buffer = (&addr);
  254. bind[0].is_null = 0;
  255. bind[0].length = 0;
  256. }
  257. for (uint32_t i = 0; i < num_; i++) {
  258. addr = BASE_ADDR4 + random() % int(num_ / hitratio_);
  259. if (!compiled_stmt_) {
  260. char query[512];
  261. sprintf(query, "SELECT lease_id,addr,hwaddr,client_id,valid_lft,"
  262. "cltt,pool_id,fixed,hostname,fqdn_fwd,fqdn_rev "
  263. "FROM lease4 where addr=%d", addr);
  264. mysql_real_query(conn_, query, strlen(query));
  265. MYSQL_RES * result = mysql_store_result(conn_);
  266. int num_rows = mysql_num_rows(result);
  267. int num_fields = mysql_num_fields(result);
  268. if ( (num_rows > 1) ) {
  269. stringstream tmp;
  270. tmp << "Search: DB returned " << num_rows << " leases for address "
  271. << hex << addr << dec;
  272. failure(tmp.str().c_str());
  273. }
  274. if (num_rows) {
  275. if (num_fields == 0) {
  276. failure("Query returned empty set");
  277. }
  278. MYSQL_ROW row = mysql_fetch_row(result);
  279. // pretend to do something with it
  280. if (row[0] == NULL) {
  281. failure("SELECT returned NULL data.");
  282. }
  283. mysql_free_result(result);
  284. if (verbose_) {
  285. printf("."); // hit
  286. }
  287. } else {
  288. if (verbose_) {
  289. printf("x"); // miss
  290. }
  291. }
  292. } else {
  293. // compiled statement
  294. if (mysql_stmt_bind_param(stmt, bind)) {
  295. failure("Failed to bind parameters: mysql_stmt_bind_param() returned non-zero");
  296. }
  297. if (mysql_stmt_execute(stmt)) {
  298. failure("Failed to execute statement: mysql_stmt_execute() returned non-zero");
  299. }
  300. MYSQL_BIND response[11];
  301. size_t length[11];
  302. my_bool is_null[11];
  303. my_bool error[11];
  304. uint32_t lease_id;
  305. uint32_t lease_addr;
  306. char hwaddr[20];
  307. char client_id[128];
  308. uint32_t valid_lft; // We can use the same value for all leases
  309. MYSQL_TIME cltt;
  310. uint32_t pool_id;
  311. my_bool fixed;
  312. char hostname[255];
  313. my_bool fqdn_fwd;
  314. my_bool fqdn_rev;
  315. for (int j = 0; j < 11; j++) {
  316. response[j].is_null = &is_null[j];
  317. response[j].length = &length[j];
  318. response[j].error = &error[j];
  319. }
  320. // 1th parameter: lease_id
  321. response[0].buffer_type = MYSQL_TYPE_LONG;
  322. response[0].buffer = (&lease_id);
  323. // 2nd parameter: IPv4 address
  324. response[1].buffer_type = MYSQL_TYPE_LONG;
  325. response[1].buffer = (&lease_addr);
  326. // 3rd parameter: Hardware address
  327. response[2].buffer_type = MYSQL_TYPE_STRING;
  328. response[2].buffer = hwaddr;
  329. response[2].buffer_length = sizeof(hwaddr);
  330. // 4th parameter: Client-id
  331. response[3].buffer_type = MYSQL_TYPE_STRING;
  332. response[3].buffer = &client_id;
  333. // 5th parameter: valid-lifetime
  334. response[4].buffer_type = MYSQL_TYPE_LONG;
  335. response[4].buffer = &valid_lft;
  336. // 6th parameter: cltt
  337. response[5].buffer_type = MYSQL_TYPE_TIMESTAMP;
  338. response[5].buffer = &cltt;
  339. // 7th parameter: pool-id
  340. response[6].buffer_type = MYSQL_TYPE_LONG;
  341. response[6].buffer = &pool_id;
  342. // 8th parameter: fixed
  343. response[7].buffer_type = MYSQL_TYPE_TINY;
  344. response[7].buffer = &fixed;
  345. // 9th parameter: hostname
  346. response[8].buffer_type = MYSQL_TYPE_STRING;
  347. response[8].buffer = &hostname;
  348. // 10th parameter: fqdn_fwd
  349. response[9].buffer_type = MYSQL_TYPE_TINY;
  350. response[9].buffer = &fqdn_fwd;
  351. // 11th parameter: fqdn_rev
  352. response[10].buffer_type = MYSQL_TYPE_TINY;
  353. response[10].buffer = &fqdn_rev;
  354. if (mysql_stmt_bind_result(stmt, response))
  355. {
  356. printf("Error: %s\n", mysql_stmt_error(stmt));
  357. failure("mysql_stmt_bind_result() failed");
  358. }
  359. int num_rows = 0;
  360. if (!mysql_stmt_fetch(stmt)) {
  361. if (lease_addr != addr) {
  362. failure("Returned data is bogus!");
  363. }
  364. num_rows++;
  365. }
  366. // we could call mysql_stmt_fetch again to check that there are no
  367. // other data for us. But there should be exactly one row of data
  368. // with specified address.
  369. if (num_rows) {
  370. if (verbose_) {
  371. printf("."); // hit
  372. }
  373. } else {
  374. if (verbose_) {
  375. printf("x"); // miss
  376. }
  377. }
  378. }
  379. }
  380. if (compiled_stmt_) {
  381. if (mysql_stmt_close(stmt)) {
  382. failure("Failed to close compiled statement, mysql_stmt_close returned non-zero");
  383. }
  384. }
  385. printf("\n");
  386. }
  387. void MySQL_uBenchmark::updateLease4Test() {
  388. if (!conn_) {
  389. throw "Not connected to MySQL server.";
  390. }
  391. printf("UPDATE: ");
  392. uint32_t valid_lft = 1002; // just some dummy value
  393. char cltt[] = "now()";
  394. size_t cltt_len = strlen(cltt);
  395. uint32_t addr = 0;
  396. MYSQL_STMT * stmt = NULL;
  397. MYSQL_BIND bind[3];
  398. if (compiled_stmt_) {
  399. stmt = mysql_stmt_init(conn_);
  400. if (!stmt) {
  401. failure("Unable to create compiled statement");
  402. }
  403. const char * statement = "UPDATE lease4 SET valid_lft=?, cltt=? WHERE addr=?";
  404. if (mysql_stmt_prepare(stmt, statement, strlen(statement))) {
  405. failure("Failed to prepare statement, mysql_stmt_prepare() returned non-zero");
  406. }
  407. int param_cnt = mysql_stmt_param_count(stmt);
  408. if (param_cnt != 3) {
  409. failure("Parameter count sanity check failed.");
  410. }
  411. memset(bind, 0, sizeof(bind));
  412. // 1st parameter: valid lifetime
  413. bind[0].buffer_type = MYSQL_TYPE_LONG;
  414. bind[0].buffer = &valid_lft;
  415. // 2nd parameter: cltt
  416. bind[1].buffer_type = MYSQL_TYPE_STRING;
  417. bind[1].buffer = &cltt;
  418. bind[1].buffer_length = cltt_len;
  419. bind[2].buffer_type = MYSQL_TYPE_LONG;
  420. bind[2].buffer = &addr;
  421. }
  422. for (uint32_t i = 0; i < num_; i++) {
  423. addr = BASE_ADDR4 + random() % num_;
  424. if (!compiled_stmt_) {
  425. char query[128];
  426. sprintf(query, "UPDATE lease4 SET valid_lft=1002, cltt=now() WHERE addr=%d", addr);
  427. mysql_real_query(conn_, query, strlen(query));
  428. } else {
  429. // compiled statement
  430. if (mysql_stmt_bind_param(stmt, bind)) {
  431. failure("Failed to bind parameters: mysql_stmt_bind_param() returned non-zero");
  432. }
  433. if (mysql_stmt_execute(stmt)) {
  434. failure("Failed to execute statement: mysql_stmt_execute() returned non-zero");
  435. }
  436. }
  437. if (verbose_) {
  438. printf(".");
  439. }
  440. }
  441. if (compiled_stmt_) {
  442. if (mysql_stmt_close(stmt)) {
  443. failure("Failed to close compiled statement, mysql_stmt_close returned non-zero");
  444. }
  445. }
  446. printf("\n");
  447. }
  448. void MySQL_uBenchmark::deleteLease4Test() {
  449. if (!conn_) {
  450. throw "Not connected to MySQL server.";
  451. }
  452. printf("DELETE: ");
  453. uint32_t addr = 0;
  454. MYSQL_STMT * stmt = NULL;
  455. MYSQL_BIND bind[1]; // just a single element
  456. if (compiled_stmt_) {
  457. stmt = mysql_stmt_init(conn_);
  458. if (!stmt) {
  459. failure("Unable to create compiled statement, mysql_stmt_init() failed");
  460. }
  461. const char * statement = "DELETE FROM lease4 WHERE addr=?";
  462. if (mysql_stmt_prepare(stmt, statement, strlen(statement) )) {
  463. failure("Failed to prepare statement, mysql_stmt_prepare() returned non-zero");
  464. }
  465. int param_cnt = mysql_stmt_param_count(stmt);
  466. if (param_cnt != 1) {
  467. failure("Parameter count sanity check failed.");
  468. }
  469. memset(bind, 0, sizeof(bind));
  470. // 1st parameter: IPv4 address
  471. bind[0].buffer_type = MYSQL_TYPE_LONG;
  472. bind[0].buffer = (&addr);
  473. bind[0].is_null = 0;
  474. bind[0].length = 0;
  475. }
  476. for (uint32_t i = 0; i < num_; i++) {
  477. addr = BASE_ADDR4 + i;
  478. if (!compiled_stmt_) {
  479. char query[128];
  480. sprintf(query, "DELETE FROM lease4 WHERE addr=%d", addr);
  481. mysql_real_query(conn_, query, strlen(query));
  482. } else {
  483. // compiled statement
  484. if (mysql_stmt_bind_param(stmt, bind)) {
  485. failure("Failed to bind parameters: mysql_stmt_bind_param() returned non-zero");
  486. }
  487. if (mysql_stmt_execute(stmt)) {
  488. failure("Failed to execute statement: mysql_stmt_execute() returned non-zero");
  489. }
  490. }
  491. if (verbose_) {
  492. printf(".");
  493. }
  494. }
  495. if (compiled_stmt_) {
  496. if (mysql_stmt_close(stmt)) {
  497. failure("Failed to close compiled statement, mysql_stmt_close returned non-zero");
  498. }
  499. }
  500. printf("\n");
  501. }
  502. void MySQL_uBenchmark::printInfo() {
  503. cout << "MySQL client version is " << mysql_get_client_info() << endl;
  504. }
  505. int main(int argc, char * const argv[]) {
  506. const char* hostname ="localhost"; // -m (MySQL server)
  507. const char* user = "root"; // -u
  508. const char* passwd = "secret"; // -p
  509. const char* dbname = "kea"; // -f
  510. uint32_t num = 100; // -n
  511. bool sync = true; // -s
  512. bool verbose = true; // -v
  513. MySQL_uBenchmark bench(hostname, user, passwd, dbname, num, sync, verbose);
  514. bench.parseCmdline(argc, argv);
  515. int result = bench.run();
  516. return (result);
  517. }