sqlite_ubench.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <sstream>
  19. #include <iostream>
  20. #include <sqlite3.h>
  21. #include "sqlite_ubench.h"
  22. using namespace std;
  23. SQLite_uBenchmark::SQLite_uBenchmark(const string& filename,
  24. uint32_t num_iterations,
  25. bool sync, bool verbose)
  26. :uBenchmark(num_iterations, filename, sync, verbose),
  27. DB_(NULL) {
  28. }
  29. void SQLite_uBenchmark::connect() {
  30. int result = sqlite3_open(DBName_.c_str(), &DB_);
  31. if (result) {
  32. sqlite3_open(DBName_.c_str(), &DB_);
  33. failure("Failed to open DB file");
  34. }
  35. sqlite3_exec(DB_, "DELETE FROM lease4", NULL, NULL, NULL);
  36. if (Sync_) {
  37. sqlite3_exec(DB_, "PRAGMA synchronous = ON", NULL, NULL, NULL);
  38. } else {
  39. sqlite3_exec(DB_, "PRAGMA synchronous = OFF", NULL, NULL, NULL);
  40. }
  41. // see http://www.sqlite.org/pragma.html#pragma_journal_mode
  42. // for detailed explanation. Available modes: DELETE, TRUNCATE,
  43. // PERSIST, MEMORY, WAL, OFF
  44. sqlite3_exec(DB_, "PRAGMA journal_mode = OFF", NULL, NULL, NULL);
  45. }
  46. void SQLite_uBenchmark::disconnect() {
  47. if (DB_) {
  48. sqlite3_close(DB_);
  49. DB_ = NULL;
  50. }
  51. }
  52. void SQLite_uBenchmark::createLease4Test() {
  53. if (!DB_) {
  54. throw "SQLite connection is closed.";
  55. }
  56. uint32_t addr = BASE_ADDR4; // Let's start with 1.0.0.0 address
  57. const uint8_t hwaddr_len = 20; // Not a real field
  58. char hwaddr[hwaddr_len];
  59. const uint8_t client_id_len = 128;
  60. char client_id[client_id_len];
  61. uint32_t valid_lft = 1000; // We can use the same value for all leases
  62. uint32_t recycle_time = 0; // Not supported in any foresable future,
  63. // so keep this as 0
  64. string cltt = "now"; // Timestamp
  65. uint32_t pool_id = 0; // Let's use pools 0-99
  66. bool fixed = false;
  67. string hostname("foo"); // Will generate it dynamically
  68. bool fqdn_fwd = true; // Let's pretend to do AAAA update
  69. bool fqdn_rev = true; // Let's pretend to do PTR update
  70. printf("CREATE: ");
  71. for (uint8_t i = 0; i < hwaddr_len; i++) {
  72. hwaddr[i] = 65 + i;
  73. }
  74. hwaddr[19] = 0; // workaround
  75. for (uint8_t i = 0; i < client_id_len; i++) {
  76. client_id[i] = 33 + i;
  77. }
  78. client_id[6] = 'X'; // there's apostrophe here. It would confuse
  79. // query formatting, let's get rid of it
  80. client_id[127] = 0; // workaround
  81. for (uint32_t i = 0; i < Num_; i++) {
  82. stringstream cltt;
  83. cltt << "2012-07-11 15:43:" << (i % 60);
  84. addr++;
  85. char* errorMsg = NULL;
  86. // the first address is 1.0.0.0.
  87. char query[2000];
  88. /// @todo: Encode HWADDR and CLIENT-ID properly
  89. sprintf(query, "INSERT INTO lease4(addr,hwaddr,client_id,"
  90. "valid_lft,recycle_time,cltt,pool_id,fixed,hostname,"
  91. "fqdn_fwd,fqdn_rev) VALUES(%u,'%s','%s',%d,%d,'%s',%d,'%s','%s','%s','%s');",
  92. addr, hwaddr, client_id, valid_lft, recycle_time,
  93. cltt.str().c_str(), pool_id, (fixed?"true":"false"),
  94. hostname.c_str(), (fqdn_fwd?"true":"false"), (fqdn_rev?"true":"false"));
  95. // printf("QUERY=[%s]\n", query);
  96. int result = sqlite3_exec(DB_, query, NULL, 0, &errorMsg);
  97. if (result != SQLITE_OK) {
  98. stringstream tmp;
  99. tmp << "INSERT error:" << errorMsg;
  100. failure(tmp.str().c_str());
  101. } else {
  102. if (Verbose_) {
  103. printf(".");
  104. }
  105. };
  106. }
  107. printf("\n");
  108. }
  109. static int search_callback(void *counter, int /*argc*/, char** /*argv*/,
  110. char** /*azColName*/){
  111. int* cnt = static_cast<int*>(counter);
  112. (*cnt)++;
  113. #if 0
  114. int i;
  115. for(i=0; i<argc; i++){
  116. printf("%s=%s ", azColName[i], argv[i] ? argv[i] : "NULL");
  117. }
  118. printf("\n");
  119. #endif
  120. return 0;
  121. }
  122. void SQLite_uBenchmark::searchLease4Test() {
  123. if (!DB_) {
  124. throw "SQLite connection is closed.";
  125. }
  126. // this formula should roughly find something a lease in 90% cases
  127. float hitRatio = 0.5;
  128. printf("RETRIEVE: ");
  129. for (uint32_t i = 0; i < Num_; i++) {
  130. uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
  131. char* errorMsg = NULL;
  132. int cnt = 0;
  133. char query[2000];
  134. sprintf(query, "SELECT lease_id,addr,hwaddr,client_id,valid_lft,"
  135. "cltt,pool_id,fixed,hostname,fqdn_fwd,fqdn_rev "
  136. "FROM lease4 where addr=%d", x);
  137. int result = sqlite3_exec(DB_, query, search_callback, &cnt, &errorMsg);
  138. if (result != SQLITE_OK) {
  139. stringstream tmp;
  140. tmp << "SELECT failed: " << errorMsg;
  141. failure(tmp.str().c_str());
  142. }
  143. if (cnt) {
  144. if (Verbose_) {
  145. printf(".");
  146. }
  147. } else {
  148. if (Verbose_) {
  149. printf("X");
  150. }
  151. }
  152. }
  153. printf("\n");
  154. }
  155. void SQLite_uBenchmark::updateLease4Test() {
  156. if (!DB_) {
  157. throw "SQLite connection is closed.";
  158. }
  159. printf("UPDATE: ");
  160. for (uint32_t i = 0; i < Num_; i++) {
  161. uint32_t x = BASE_ADDR4 + random() % Num_;
  162. char* errorMsg = NULL;
  163. char query[2000];
  164. sprintf(query, "UPDATE lease4 SET valid_lft=1002, cltt='now' WHERE addr=%d", x);
  165. int result = sqlite3_exec(DB_, query, NULL /* no callback here*/, 0, &errorMsg);
  166. if (result != SQLITE_OK) {
  167. stringstream tmp;
  168. tmp << "UPDATE error:" << errorMsg;
  169. failure(tmp.str().c_str());
  170. }
  171. if (Verbose_) {
  172. printf(".");
  173. }
  174. }
  175. printf("\n");
  176. }
  177. void SQLite_uBenchmark::deleteLease4Test() {
  178. if (!DB_) {
  179. throw "SQLite connection is closed.";
  180. }
  181. printf("DELETE: ");
  182. for (uint32_t i = 0; i < Num_; i++) {
  183. uint32_t x = BASE_ADDR4 + i;
  184. char* errorMsg = NULL;
  185. char query[2000];
  186. sprintf(query, "DELETE FROM lease4 WHERE addr=%d", x);
  187. int result = sqlite3_exec(DB_, query, NULL /* no callback here*/, 0, &errorMsg);
  188. if (result != SQLITE_OK) {
  189. stringstream tmp;
  190. tmp << "DELETE error:" << errorMsg;
  191. failure(tmp.str().c_str());
  192. }
  193. if (Verbose_) {
  194. printf(".");
  195. }
  196. }
  197. printf("\n");
  198. }
  199. void SQLite_uBenchmark::printInfo() {
  200. cout << "SQLite version is " << sqlite3_libversion()
  201. << "sourceid version is " << sqlite3_sourceid() << endl;
  202. }
  203. int main(int argc, char* const argv[]) {
  204. const char* filename = "sqlite.db";
  205. uint32_t num = 100;
  206. bool sync = true;
  207. bool verbose = true;
  208. SQLite_uBenchmark bench(filename, num, sync, verbose);
  209. bench.parseCmdline(argc, argv);
  210. int result = bench.run();
  211. return (result);
  212. }