sqlite_ubench.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. char hwaddr[20];
  61. uint8_t hwaddr_len = 20; // not a real field
  62. char client_id[128];
  63. uint8_t client_id_len = 128;
  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 < 20; i++) {
  75. hwaddr[i] = 65 + i;
  76. }
  77. hwaddr[19] = 0; // workaround
  78. for (uint8_t i = 0; i < 128; i++) {
  79. client_id[i] = 33 + i;
  80. }
  81. client_id[6] = 'X';
  82. client_id[127] = 0; // workaround
  83. for (uint32_t i = 0; i < Num_; i++) {
  84. stringstream cltt;
  85. cltt << "2012-07-11 15:43:" << (i % 60);
  86. addr++;
  87. char * errorMsg = NULL;
  88. // the first address is 1.0.0.0.
  89. char query[2000], * end;
  90. /// @todo: Encode HWADDR and CLIENT-ID properly
  91. sprintf(query, "INSERT INTO lease4(addr,hwaddr,client_id,"
  92. "valid_lft,recycle_time,cltt,pool_id,fixed,hostname,"
  93. "fqdn_fwd,fqdn_rev) VALUES(%u,'%s','%s',%d,%d,'%s',%d,'%s','%s','%s','%s');",
  94. addr, hwaddr, client_id, valid_lft, recycle_time,
  95. cltt.str().c_str(), pool_id, (fixed?"true":"false"),
  96. hostname.c_str(), (fqdn_fwd?"true":"false"), (fqdn_rev?"true":"false"));
  97. // printf("QUERY=[%s]\n", query);
  98. int result = sqlite3_exec(DB_, query, NULL, 0, &errorMsg);
  99. if (result != SQLITE_OK) {
  100. stringstream tmp;
  101. tmp << "INSERT error:" << errorMsg;
  102. failure(tmp.str().c_str());
  103. } else {
  104. if (Verbose_) {
  105. printf(".");
  106. }
  107. };
  108. }
  109. printf("\n");
  110. }
  111. static int search_callback(void *counter, int argc, char **argv, char **azColName){
  112. int * cnt = static_cast<int*>(counter);
  113. (*cnt)++;
  114. #if 0
  115. int i;
  116. for(i=0; i<argc; i++){
  117. printf("%s=%s ", azColName[i], argv[i] ? argv[i] : "NULL");
  118. }
  119. printf("\n");
  120. #endif
  121. return 0;
  122. }
  123. void SQLite_uBenchmark::searchLease4Test() {
  124. if (!DB_) {
  125. throw "SQLite connection is closed.";
  126. }
  127. // this formula should roughly find something a lease in 90% cases
  128. float hitRatio = 0.5;
  129. printf("RETRIEVE: ");
  130. for (uint32_t i = 0; i < Num_; i++) {
  131. uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
  132. char * errorMsg = NULL;
  133. int cnt = 0;
  134. char query[2000];
  135. sprintf(query, "SELECT lease_id,addr,hwaddr,client_id,valid_lft,"
  136. "cltt,pool_id,fixed,hostname,fqdn_fwd,fqdn_rev "
  137. "FROM lease4 where addr=%d", x);
  138. int result = sqlite3_exec(DB_, query, search_callback, &cnt, &errorMsg);
  139. if (result != SQLITE_OK) {
  140. stringstream tmp;
  141. tmp << "SELECT failed: " << errorMsg;
  142. failure(tmp.str().c_str());
  143. }
  144. if (cnt) {
  145. if (Verbose_) {
  146. printf(".");
  147. }
  148. } else {
  149. if (Verbose_) {
  150. printf("X");
  151. }
  152. }
  153. }
  154. printf("\n");
  155. }
  156. void SQLite_uBenchmark::updateLease4Test() {
  157. if (!DB_) {
  158. throw "SQLite connection is closed.";
  159. }
  160. printf("UPDATE: ");
  161. for (uint32_t i = 0; i < Num_; i++) {
  162. uint32_t x = BASE_ADDR4 + random() % Num_;
  163. char * errorMsg = NULL;
  164. char query[2000];
  165. sprintf(query, "UPDATE lease4 SET valid_lft=1002, cltt='now' WHERE addr=%d", x);
  166. int result = sqlite3_exec(DB_, query, NULL /* no callback here*/, 0, &errorMsg);
  167. if (result != SQLITE_OK) {
  168. stringstream tmp;
  169. tmp << "UPDATE error:" << errorMsg;
  170. failure(tmp.str().c_str());
  171. }
  172. if (Verbose_) {
  173. printf(".");
  174. }
  175. }
  176. printf("\n");
  177. }
  178. void SQLite_uBenchmark::deleteLease4Test() {
  179. if (!DB_) {
  180. throw "SQLite connection is closed.";
  181. }
  182. printf("DELETE: ");
  183. char * errorMsg = NULL;
  184. for (uint32_t i = 0; i < Num_; i++) {
  185. uint32_t x = BASE_ADDR4 + i;
  186. char * errorMsg = NULL;
  187. char query[2000];
  188. sprintf(query, "DELETE FROM lease4 WHERE addr=%d", x);
  189. int result = sqlite3_exec(DB_, query, NULL /* no callback here*/, 0, &errorMsg);
  190. if (result != SQLITE_OK) {
  191. stringstream tmp;
  192. tmp << "DELETE error:" << errorMsg;
  193. failure(tmp.str().c_str());
  194. }
  195. if (Verbose_) {
  196. printf(".");
  197. }
  198. }
  199. printf("\n");
  200. }
  201. void SQLite_uBenchmark::printInfo() {
  202. cout << "SQLite version is " << sqlite3_libversion()
  203. << "sourceid version is " << sqlite3_sourceid() << endl;
  204. }
  205. int main(int argc, const char * argv[]) {
  206. const char * filename = "sqlite.db";
  207. uint32_t num = 100;
  208. bool sync = true;
  209. bool verbose = true;
  210. SQLite_uBenchmark bench(filename, num, sync, verbose);
  211. int result = bench.run();
  212. return (result);
  213. }