|
@@ -13,69 +13,106 @@
|
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
#include <sstream>
|
|
|
-#include <vector>
|
|
|
-#include <map>
|
|
|
#include <iostream>
|
|
|
+#include <map>
|
|
|
#include "memfile_ubench.h"
|
|
|
-#include <boost/shared_ptr.hpp>
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
-struct Lease4 {
|
|
|
- uint32_t addr;
|
|
|
- std::vector<uint8_t> hwaddr;
|
|
|
- std::vector<uint8_t> client_id;
|
|
|
- uint32_t valid_lft;
|
|
|
- uint32_t recycle_time;
|
|
|
- time_t cltt;
|
|
|
- uint32_t pool_id;
|
|
|
- bool fixed;
|
|
|
- std::string hostname;
|
|
|
- bool fqdn_fwd;
|
|
|
- bool fqdn_rev;
|
|
|
- std::string options;
|
|
|
- std::string comments;
|
|
|
+class memfile_LeaseMgr {
|
|
|
+public:
|
|
|
+typedef std::map<uint32_t /* addr */, Lease4Ptr /* lease info */> IPv4Hash;
|
|
|
+typedef std::map<uint32_t, Lease4Ptr>::iterator leaseIt;
|
|
|
+ memfile_LeaseMgr(const std::string& filename);
|
|
|
+ ~memfile_LeaseMgr();
|
|
|
+ bool addLease(Lease4Ptr lease);
|
|
|
+ Lease4Ptr getLease(uint32_t addr);
|
|
|
+ Lease4Ptr updateLease(uint32_t addr, uint32_t new_cltt);
|
|
|
+ bool deleteLease(uint32_t addr);
|
|
|
+
|
|
|
+protected:
|
|
|
+
|
|
|
+ void writeLease(Lease4Ptr lease);
|
|
|
+
|
|
|
+ std::string Filename_;
|
|
|
+ std::ofstream File_;
|
|
|
+ IPv4Hash ip4Hash_;
|
|
|
};
|
|
|
|
|
|
-typedef boost::shared_ptr<Lease4> Lease4Ptr;
|
|
|
+memfile_LeaseMgr::memfile_LeaseMgr(const std::string& filename)
|
|
|
+ : File_(filename.c_str(), ios::trunc), Filename_(filename) {
|
|
|
+ if (!File_.is_open()) {
|
|
|
+ throw "Failed to create file " + filename;
|
|
|
+ }
|
|
|
|
|
|
-std::map<uint32_t /* addr */, Lease4Ptr /* lease info */> ip4Hash;
|
|
|
-typedef std::map<uint32_t, Lease4Ptr>::iterator leaseIt;
|
|
|
+}
|
|
|
|
|
|
-bool addLease(Lease4Ptr lease) {
|
|
|
- if (ip4Hash.find(lease->addr) != ip4Hash.end()) {
|
|
|
+memfile_LeaseMgr::~memfile_LeaseMgr() {
|
|
|
+ File_.close();
|
|
|
+}
|
|
|
+
|
|
|
+void memfile_LeaseMgr::writeLease(Lease4Ptr lease) {
|
|
|
+ File_ << "lease " << lease->addr << " {" << endl << " hw-addr ";
|
|
|
+ for (std::vector<uint8_t>::const_iterator it = lease->hwaddr.begin();
|
|
|
+ it != lease->hwaddr.end(); ++it) {
|
|
|
+ File_ << *it;
|
|
|
+ }
|
|
|
+ File_ << ";" << endl << " client-id ";
|
|
|
+ for (std::vector<uint8_t>::const_iterator it = lease->client_id.begin();
|
|
|
+ it != lease->client_id.end(); ++it) {
|
|
|
+ File_ << *it;
|
|
|
+ }
|
|
|
+ File_ << ";" << endl << " valid-lifetime " << lease->valid_lft << endl;
|
|
|
+ File_ << " recycle-time " << lease->recycle_time << endl;
|
|
|
+ File_ << " cltt " << lease->cltt << endl;
|
|
|
+ File_ << " pool-id " << lease->pool_id << endl;
|
|
|
+ File_ << " fixed " << lease->fixed << endl;
|
|
|
+ File_ << " hostname " << lease->hostname << endl;
|
|
|
+ File_ << " fqdn_fwd " << lease->fqdn_fwd << endl;
|
|
|
+ File_ << " fqdn_rev " << lease->fqdn_rev << endl;
|
|
|
+ File_ << "}" << endl;
|
|
|
+}
|
|
|
+
|
|
|
+bool memfile_LeaseMgr::addLease(Lease4Ptr lease) {
|
|
|
+ if (ip4Hash_.find(lease->addr) != ip4Hash_.end()) {
|
|
|
// there is such an address already in the hash
|
|
|
return false;
|
|
|
}
|
|
|
- ip4Hash.insert(pair<uint32_t, Lease4Ptr>(lease->addr, lease));
|
|
|
+ ip4Hash_.insert(pair<uint32_t, Lease4Ptr>(lease->addr, lease));
|
|
|
+ lease->hostname = "add";
|
|
|
+ writeLease(lease);
|
|
|
return (true);
|
|
|
}
|
|
|
|
|
|
-Lease4Ptr getLease(uint32_t addr) {
|
|
|
- leaseIt x = ip4Hash.find(addr);
|
|
|
- if (x != ip4Hash.end())
|
|
|
+Lease4Ptr memfile_LeaseMgr::getLease(uint32_t addr) {
|
|
|
+ leaseIt x = ip4Hash_.find(addr);
|
|
|
+ if (x != ip4Hash_.end())
|
|
|
return x->second; // found
|
|
|
-
|
|
|
+
|
|
|
// not found
|
|
|
return Lease4Ptr();
|
|
|
}
|
|
|
|
|
|
-bool updateLease(uint32_t addr, uint32_t new_cltt) {
|
|
|
- leaseIt x = ip4Hash.find(addr);
|
|
|
- if (x != ip4Hash.end()) {
|
|
|
+Lease4Ptr memfile_LeaseMgr::updateLease(uint32_t addr, uint32_t new_cltt) {
|
|
|
+ leaseIt x = ip4Hash_.find(addr);
|
|
|
+ if (x != ip4Hash_.end()) {
|
|
|
x->second->cltt = new_cltt;
|
|
|
- return true;
|
|
|
+ x->second->hostname = "update";
|
|
|
+ writeLease(x->second);
|
|
|
+ return x->second;
|
|
|
}
|
|
|
- return false;
|
|
|
+ return Lease4Ptr();
|
|
|
}
|
|
|
|
|
|
-bool deleteLease(uint32_t addr) {
|
|
|
- leaseIt x = ip4Hash.find(addr);
|
|
|
- if (x != ip4Hash.end()) {
|
|
|
- ip4Hash.erase(x);
|
|
|
+bool memfile_LeaseMgr::deleteLease(uint32_t addr) {
|
|
|
+ leaseIt x = ip4Hash_.find(addr);
|
|
|
+ if (x != ip4Hash_.end()) {
|
|
|
+ x->second->hostname = "delete";
|
|
|
+ writeLease(x->second);
|
|
|
+ ip4Hash_.erase(x);
|
|
|
return true;
|
|
|
- }
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -90,19 +127,22 @@ void memfile_uBenchmark::failure(const char* operation) {
|
|
|
}
|
|
|
|
|
|
void memfile_uBenchmark::connect() {
|
|
|
- File_.open(Filename_.c_str());
|
|
|
- if (!File_.is_open()) {
|
|
|
- failure("Failed to create output file");
|
|
|
+ try {
|
|
|
+ LeaseMgr_ = new memfile_LeaseMgr(Filename_);
|
|
|
+
|
|
|
+ } catch (const std::string& e) {
|
|
|
+ failure(e.c_str());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void memfile_uBenchmark::disconnect() {
|
|
|
- File_.close();
|
|
|
+ delete LeaseMgr_;
|
|
|
+ LeaseMgr_ = NULL;
|
|
|
}
|
|
|
|
|
|
void memfile_uBenchmark::createLease4Test() {
|
|
|
- if (!File_.is_open()) {
|
|
|
- throw "Lease file not open for writing.";
|
|
|
+ if (!LeaseMgr_) {
|
|
|
+ throw "No LeaseMgr instantiated.";
|
|
|
}
|
|
|
|
|
|
uint32_t addr = BASE_ADDR4; // Let's start with 1.0.0.0 address
|
|
@@ -149,7 +189,7 @@ void memfile_uBenchmark::createLease4Test() {
|
|
|
lease->fqdn_fwd = true;
|
|
|
lease->fqdn_rev = true;
|
|
|
|
|
|
- if (!addLease(lease)) {
|
|
|
+ if (!LeaseMgr_->addLease(lease)) {
|
|
|
failure("addLease() failed");
|
|
|
} else {
|
|
|
printf(".");
|
|
@@ -161,8 +201,8 @@ void memfile_uBenchmark::createLease4Test() {
|
|
|
}
|
|
|
|
|
|
void memfile_uBenchmark::searchLease4Test() {
|
|
|
- if (!File_.is_open()) {
|
|
|
- throw "Lease file not open for writing.";
|
|
|
+ if (!LeaseMgr_) {
|
|
|
+ throw "No LeaseMgr instantiated.";
|
|
|
}
|
|
|
|
|
|
// this formula should roughly find something a lease in 90% cases
|
|
@@ -174,7 +214,7 @@ void memfile_uBenchmark::searchLease4Test() {
|
|
|
|
|
|
uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
|
|
|
|
|
|
- Lease4Ptr lease = getLease(x);
|
|
|
+ Lease4Ptr lease = LeaseMgr_->getLease(x);
|
|
|
if (lease) {
|
|
|
printf(".");
|
|
|
} else {
|
|
@@ -186,8 +226,8 @@ void memfile_uBenchmark::searchLease4Test() {
|
|
|
}
|
|
|
|
|
|
void memfile_uBenchmark::updateLease4Test() {
|
|
|
- if (!File_.is_open()) {
|
|
|
- throw "Lease file not open for writing.";
|
|
|
+ if (!LeaseMgr_) {
|
|
|
+ throw "No LeaseMgr instantiated.";
|
|
|
}
|
|
|
|
|
|
printf("UPDATE: ");
|
|
@@ -198,7 +238,8 @@ void memfile_uBenchmark::updateLease4Test() {
|
|
|
|
|
|
uint32_t x = BASE_ADDR4 + random() % Num_;
|
|
|
|
|
|
- if (!updateLease(x, cltt)) {
|
|
|
+ Lease4Ptr lease = LeaseMgr_->updateLease(x, cltt);
|
|
|
+ if (!lease) {
|
|
|
stringstream tmp;
|
|
|
tmp << "UPDATE failed for lease " << hex << x << dec;
|
|
|
failure(tmp.str().c_str());
|
|
@@ -210,8 +251,8 @@ void memfile_uBenchmark::updateLease4Test() {
|
|
|
}
|
|
|
|
|
|
void memfile_uBenchmark::deleteLease4Test() {
|
|
|
- if (!File_.is_open()) {
|
|
|
- throw "Lease file not open for writing.";
|
|
|
+ if (!LeaseMgr_) {
|
|
|
+ throw "No LeaseMgr instantiated.";
|
|
|
}
|
|
|
|
|
|
printf("DELETE: ");
|
|
@@ -221,7 +262,7 @@ void memfile_uBenchmark::deleteLease4Test() {
|
|
|
|
|
|
uint32_t x = BASE_ADDR4 + i;
|
|
|
|
|
|
- if (!deleteLease(x)) {
|
|
|
+ if (!LeaseMgr_->deleteLease(x)) {
|
|
|
stringstream tmp;
|
|
|
tmp << "UPDATE failed for lease " << hex << x << dec;
|
|
|
failure(tmp.str().c_str());
|
|
@@ -240,7 +281,7 @@ void memfile_uBenchmark::printInfo() {
|
|
|
int main(int argc, const char * argv[]) {
|
|
|
|
|
|
const char * filename = "dhcpd.leases";
|
|
|
- uint32_t num = 1000000;
|
|
|
+ uint32_t num = 10000;
|
|
|
|
|
|
memfile_uBenchmark bench(filename, num);
|
|
|
|