sqlite_ubench.cc 7.0 KB

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