sqlite_ubench.cc 6.7 KB

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