sqlite_ubench.cc 7.7 KB

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