memfile_ubench.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <sstream>
  15. #include <iostream>
  16. #include <map>
  17. #include "memfile_ubench.h"
  18. using namespace std;
  19. class memfile_LeaseMgr {
  20. public:
  21. typedef std::map<uint32_t /* addr */, Lease4Ptr /* lease info */> IPv4Hash;
  22. typedef std::map<uint32_t, Lease4Ptr>::iterator leaseIt;
  23. memfile_LeaseMgr(const std::string& filename, bool sync);
  24. ~memfile_LeaseMgr();
  25. bool addLease(Lease4Ptr lease);
  26. Lease4Ptr getLease(uint32_t addr);
  27. Lease4Ptr updateLease(uint32_t addr, uint32_t new_cltt);
  28. bool deleteLease(uint32_t addr);
  29. protected:
  30. void writeLease(Lease4Ptr lease);
  31. std::string Filename_;
  32. bool Sync_; // should we do flush after each operation?
  33. // we have to use fe
  34. FILE * File_;
  35. IPv4Hash ip4Hash_;
  36. };
  37. memfile_LeaseMgr::memfile_LeaseMgr(const std::string& filename, bool sync)
  38. : Filename_(filename), Sync_(sync) {
  39. File_ = fopen(filename.c_str(), "w");
  40. if (!File_) {
  41. throw "Failed to create file " + filename;
  42. }
  43. }
  44. memfile_LeaseMgr::~memfile_LeaseMgr() {
  45. fclose(File_);
  46. }
  47. void memfile_LeaseMgr::writeLease(Lease4Ptr lease) {
  48. fprintf(File_, "lease %d {\n hw-addr ", lease->addr);
  49. for (std::vector<uint8_t>::const_iterator it = lease->hwaddr.begin();
  50. it != lease->hwaddr.end(); ++it) {
  51. fprintf(File_,"%02x:", *it);
  52. }
  53. fprintf(File_, ";\n client-id ");
  54. for (std::vector<uint8_t>::const_iterator it = lease->client_id.begin();
  55. it != lease->client_id.end(); ++it) {
  56. fprintf(File_, "%02x:", *it);
  57. }
  58. fprintf(File_, ";\n valid-lifetime %d;\n recycle-time %d;\n"
  59. " cltt %d;\n pool-id %d;\n fixed %s; hostname %s;\n"
  60. " fqdn_fwd %s;\n fqdn_rev %s;\n};\n",
  61. lease->valid_lft, lease->recycle_time, (int)lease->cltt,
  62. lease->pool_id, lease->fixed?"true":"false",
  63. lease->hostname.c_str(), lease->fqdn_fwd?"true":"false",
  64. lease->fqdn_rev?"true":"false");
  65. if (Sync_) {
  66. fflush(File_);
  67. fsync(fileno(File_));
  68. }
  69. }
  70. bool memfile_LeaseMgr::addLease(Lease4Ptr lease) {
  71. if (ip4Hash_.find(lease->addr) != ip4Hash_.end()) {
  72. // there is such an address already in the hash
  73. return false;
  74. }
  75. ip4Hash_.insert(pair<uint32_t, Lease4Ptr>(lease->addr, lease));
  76. lease->hostname = "add";
  77. writeLease(lease);
  78. return (true);
  79. }
  80. Lease4Ptr memfile_LeaseMgr::getLease(uint32_t addr) {
  81. leaseIt x = ip4Hash_.find(addr);
  82. if (x != ip4Hash_.end())
  83. return x->second; // found
  84. // not found
  85. return Lease4Ptr();
  86. }
  87. Lease4Ptr memfile_LeaseMgr::updateLease(uint32_t addr, uint32_t new_cltt) {
  88. leaseIt x = ip4Hash_.find(addr);
  89. if (x != ip4Hash_.end()) {
  90. x->second->cltt = new_cltt;
  91. x->second->hostname = "update";
  92. writeLease(x->second);
  93. return x->second;
  94. }
  95. return Lease4Ptr();
  96. }
  97. bool memfile_LeaseMgr::deleteLease(uint32_t addr) {
  98. leaseIt x = ip4Hash_.find(addr);
  99. if (x != ip4Hash_.end()) {
  100. x->second->hostname = "delete";
  101. writeLease(x->second);
  102. ip4Hash_.erase(x);
  103. return true;
  104. }
  105. return false;
  106. }
  107. memfile_uBenchmark::memfile_uBenchmark(const string& filename,
  108. uint32_t num_iterations,
  109. bool sync,
  110. bool verbose)
  111. :uBenchmark(num_iterations, filename, sync, verbose),
  112. Filename_(filename) {
  113. }
  114. void memfile_uBenchmark::failure(const char* operation) {
  115. throw string(operation);
  116. }
  117. void memfile_uBenchmark::connect() {
  118. try {
  119. LeaseMgr_ = new memfile_LeaseMgr(Filename_, Sync_);
  120. } catch (const std::string& e) {
  121. failure(e.c_str());
  122. }
  123. }
  124. void memfile_uBenchmark::disconnect() {
  125. delete LeaseMgr_;
  126. LeaseMgr_ = NULL;
  127. }
  128. void memfile_uBenchmark::createLease4Test() {
  129. if (!LeaseMgr_) {
  130. throw "No LeaseMgr instantiated.";
  131. }
  132. uint32_t addr = BASE_ADDR4; // Let's start with 1.0.0.0 address
  133. char hwaddr_tmp[20];
  134. uint8_t hwaddr_len = 20; // not a real field
  135. char client_id_tmp[128];
  136. uint8_t client_id_len = 128;
  137. uint32_t valid_lft = 1000; // we can use the same value for all leases
  138. uint32_t recycle_time = 0; // not supported in any foresable future,
  139. // so keep this as 0
  140. time_t cltt = time(NULL); // timestamp
  141. uint32_t pool_id = 0; // let's use pools 0-99
  142. bool fixed = false; //
  143. string hostname("foo"); // will generate it dynamically
  144. bool fqdn_fwd = true; // let's pretend to do AAAA update
  145. bool fqdn_rev = true; // let's pretend to do PTR update
  146. printf("CREATE: ");
  147. for (uint8_t i = 0; i < 20; i++) {
  148. hwaddr_tmp[i] = 65 + i;
  149. }
  150. vector<uint8_t> hwaddr(hwaddr_tmp, hwaddr_tmp+19);
  151. for (uint8_t i = 0; i < 128; i++) {
  152. client_id_tmp[i] = 33 + i;
  153. }
  154. vector<uint8_t> client_id(client_id_tmp, client_id_tmp+19);
  155. for (uint32_t i = 0; i < Num_; i++) {
  156. cltt++;
  157. Lease4Ptr lease = boost::shared_ptr<Lease4>(new Lease4());
  158. lease->addr = addr;
  159. lease->hwaddr = hwaddr;
  160. lease->client_id = client_id;
  161. lease->valid_lft = valid_lft;
  162. lease->recycle_time = 0;
  163. lease->cltt = cltt;
  164. lease->pool_id = 0;
  165. lease->fixed = false;
  166. lease->hostname = "foo";
  167. lease->fqdn_fwd = true;
  168. lease->fqdn_rev = true;
  169. if (!LeaseMgr_->addLease(lease)) {
  170. failure("addLease() failed");
  171. } else {
  172. if (Verbose_) {
  173. printf(".");
  174. }
  175. };
  176. addr++;
  177. }
  178. printf("\n");
  179. }
  180. void memfile_uBenchmark::searchLease4Test() {
  181. if (!LeaseMgr_) {
  182. throw "No LeaseMgr instantiated.";
  183. }
  184. // this formula should roughly find something a lease in 90% cases
  185. float hitRatio = 0.5;
  186. printf("RETRIEVE: ");
  187. for (uint32_t i = 0; i < Num_; i++) {
  188. uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
  189. Lease4Ptr lease = LeaseMgr_->getLease(x);
  190. if (Verbose_) {
  191. if (lease) {
  192. printf(".");
  193. } else {
  194. printf("X");
  195. }
  196. }
  197. }
  198. printf("\n");
  199. }
  200. void memfile_uBenchmark::updateLease4Test() {
  201. if (!LeaseMgr_) {
  202. throw "No LeaseMgr instantiated.";
  203. }
  204. printf("UPDATE: ");
  205. time_t cltt = time(NULL);
  206. for (uint32_t i = 0; i < Num_; i++) {
  207. uint32_t x = BASE_ADDR4 + random() % Num_;
  208. Lease4Ptr lease = LeaseMgr_->updateLease(x, cltt);
  209. if (!lease) {
  210. stringstream tmp;
  211. tmp << "UPDATE failed for lease " << hex << x << dec;
  212. failure(tmp.str().c_str());
  213. }
  214. if (Verbose_) {
  215. printf(".");
  216. }
  217. }
  218. printf("\n");
  219. }
  220. void memfile_uBenchmark::deleteLease4Test() {
  221. if (!LeaseMgr_) {
  222. throw "No LeaseMgr instantiated.";
  223. }
  224. printf("DELETE: ");
  225. char * errorMsg = NULL;
  226. for (uint32_t i = 0; i < Num_; i++) {
  227. uint32_t x = BASE_ADDR4 + i;
  228. if (!LeaseMgr_->deleteLease(x)) {
  229. stringstream tmp;
  230. tmp << "UPDATE failed for lease " << hex << x << dec;
  231. failure(tmp.str().c_str());
  232. }
  233. if (Verbose_) {
  234. printf(".");
  235. }
  236. }
  237. printf("\n");
  238. }
  239. void memfile_uBenchmark::printInfo() {
  240. cout << "Using memory db + write-only file." << endl;
  241. }
  242. int main(int argc, const char * argv[]) {
  243. const char * filename = "dhcpd.leases";
  244. uint32_t num = 100;
  245. bool sync = true;
  246. bool verbose = false;
  247. memfile_uBenchmark bench(filename, num, sync, verbose);
  248. int result = bench.run();
  249. return (result);
  250. }