using namespace std;
-uBenchmark::uBenchmark(uint32_t iterations)
- :Num_(iterations) {
+uBenchmark::uBenchmark(uint32_t iterations, const std::string& dbname,
+ bool sync /*= false*/, bool verbose /*= true*/,
+ const std::string& host /* = "" */,
+ const std::string& user /* = "" */,
+ const std::string& pass /* = "" */)
+ :Num_(iterations), Sync_(sync), Verbose_(verbose),
+ Hostname_(host), User_(user), Passwd_(pass), DBName_(dbname)
+{
memset(ts, 0, sizeof(ts));
+
+}
+
+bool uBenchmark::parseCmdline(int args, const char* argv[]) {
+ return false;
}
void uBenchmark::failure(const char* operation) {
class uBenchmark {
public:
- uBenchmark(uint32_t numInterations);
+ uBenchmark(uint32_t iterations, const std::string& dbname,
+ bool sync, bool verbose,
+ const std::string& host = "",
+ const std::string& username = "",
+ const std::string& pass = "");
virtual void printInfo() = 0;
virtual void connect() = 0;
int run();
+ bool parseCmdline(int args, const char* argv[]);
+
protected:
uint32_t Num_; // number of operations (e.g. insert lease num times)
+ bool Sync_; // synchronous or asynchonous mode?
+ bool Verbose_;
+ std::string Hostname_;// used by MySQL only
+ std::string User_; // used by MySQL only
+ std::string Passwd_; // used by MySQL only
+ std::string DBName_; // used by MySQL, SQLite and memfile
+
const static uint32_t BASE_ADDR4 = 0x01000000; // let's start from 1.0.0.0 address
// five timestamps (1 at the beginning and 4 after each step)
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(const std::string& filename, bool sync);
~memfile_LeaseMgr();
bool addLease(Lease4Ptr lease);
Lease4Ptr getLease(uint32_t addr);
void writeLease(Lease4Ptr lease);
std::string Filename_;
- std::ofstream File_;
+ bool Sync_; // should we do flush after each operation?
+
+ // we have to use fe
+ FILE * File_;
IPv4Hash ip4Hash_;
};
-memfile_LeaseMgr::memfile_LeaseMgr(const std::string& filename)
- : File_(filename.c_str(), ios::trunc), Filename_(filename) {
- if (!File_.is_open()) {
+memfile_LeaseMgr::memfile_LeaseMgr(const std::string& filename, bool sync)
+ : Filename_(filename), Sync_(sync) {
+ File_ = fopen(filename.c_str(), "w");
+ if (!File_) {
throw "Failed to create file " + filename;
}
}
memfile_LeaseMgr::~memfile_LeaseMgr() {
- File_.close();
+ fclose(File_);
}
void memfile_LeaseMgr::writeLease(Lease4Ptr lease) {
- File_ << "lease " << lease->addr << " {" << endl << " hw-addr ";
+ fprintf(File_, "lease %d {\n hw-addr ", lease->addr);
for (std::vector<uint8_t>::const_iterator it = lease->hwaddr.begin();
it != lease->hwaddr.end(); ++it) {
- File_ << *it;
+ fprintf(File_,"%02x:", *it);
}
- File_ << ";" << endl << " client-id ";
+ fprintf(File_, ";\n client-id ");
for (std::vector<uint8_t>::const_iterator it = lease->client_id.begin();
it != lease->client_id.end(); ++it) {
- File_ << *it;
+ fprintf(File_, "%02x:", *it);
+ }
+ fprintf(File_, ";\n valid-lifetime %d;\n recycle-time %d;\n"
+ " cltt %d;\n pool-id %d;\n fixed %s; hostname %s;\n"
+ " fqdn_fwd %s;\n fqdn_rev %s;\n};\n",
+ lease->valid_lft, lease->recycle_time, (int)lease->cltt,
+ lease->pool_id, lease->fixed?"true":"false",
+ lease->hostname.c_str(), lease->fqdn_fwd?"true":"false",
+ lease->fqdn_rev?"true":"false");
+
+ if (Sync_) {
+ fflush(File_);
+ fsync(fileno(File_));
}
- 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) {
}
memfile_uBenchmark::memfile_uBenchmark(const string& filename,
- uint32_t num_iterations)
- :uBenchmark(num_iterations), Filename_(filename) {
+ uint32_t num_iterations,
+ bool sync,
+ bool verbose)
+ :uBenchmark(num_iterations, filename, sync, verbose),
+ Filename_(filename) {
}
void memfile_uBenchmark::connect() {
try {
- LeaseMgr_ = new memfile_LeaseMgr(Filename_);
+ LeaseMgr_ = new memfile_LeaseMgr(Filename_, Sync_);
} catch (const std::string& e) {
failure(e.c_str());
if (!LeaseMgr_->addLease(lease)) {
failure("addLease() failed");
} else {
- printf(".");
+ if (Verbose_) {
+ printf(".");
+ }
};
addr++;
uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
Lease4Ptr lease = LeaseMgr_->getLease(x);
- if (lease) {
- printf(".");
- } else {
- printf("X");
+ if (Verbose_) {
+ if (lease) {
+ printf(".");
+ } else {
+ printf("X");
+ }
}
}
tmp << "UPDATE failed for lease " << hex << x << dec;
failure(tmp.str().c_str());
}
- printf(".");
+ if (Verbose_) {
+ printf(".");
+ }
}
printf("\n");
tmp << "UPDATE failed for lease " << hex << x << dec;
failure(tmp.str().c_str());
}
- printf(".");
+ if (Verbose_) {
+ printf(".");
+ }
}
printf("\n");
int main(int argc, const char * argv[]) {
const char * filename = "dhcpd.leases";
- uint32_t num = 10000;
+ uint32_t num = 100;
+ bool sync = true;
+ bool verbose = false;
- memfile_uBenchmark bench(filename, num);
+ memfile_uBenchmark bench(filename, num, sync, verbose);
int result = bench.run();
class memfile_uBenchmark: public uBenchmark {
public:
memfile_uBenchmark(const std::string& filename,
- uint32_t num_iterations);
+ uint32_t num_iterations, bool sync, bool verbose);
virtual void printInfo();
virtual void connect();
MySQL_uBenchmark::MySQL_uBenchmark(const string& hostname, const string& user,
const string& pass, const string& db,
- uint32_t num_iterations)
- :uBenchmark(num_iterations), Hostname_(hostname), User_(user),
- Pass_(pass), DB_(db), Conn_(NULL) {
+ uint32_t num_iterations, bool sync,
+ bool verbose)
+ :uBenchmark(num_iterations, db, sync, verbose, hostname, user, pass),
+ Conn_(NULL) {
}
}
cout << "hostname=" << Hostname_ << ", user=" << User_
- << "pass=" << Pass_ << " db=" << DB_ << endl;
+ << "pass=" << Passwd_ << " db=" << DBName_ << endl;
if (!mysql_real_connect(Conn_, Hostname_.c_str(), User_.c_str(),
- Pass_.c_str(), DB_.c_str(), 0, NULL, 0)) {
+ Passwd_.c_str(), DBName_.c_str(), 0, NULL, 0)) {
failure("connecting to MySQL server");
} else {
cout << "MySQL connection established." << endl;
if (mysql_real_query(Conn_, q.c_str(), strlen(q.c_str()))) {
failure("dropping old lease4 entries.");
}
+
+ q = "ALTER TABLE lease4 engine=";
+ if (Sync_) {
+ q += "InnoDB";
+ } else {
+ q += "MyISAM";
+ }
+ if (mysql_query(Conn_, q.c_str())) {
+ q = "Failed to run query:" + q;
+ failure(q.c_str());
+ }
}
void MySQL_uBenchmark::disconnect() {
// something failed.
failure("INSERT query");
} else {
- printf(".");
+ if (Verbose_) {
+ printf(".");
+ }
};
}
printf("\n");
MYSQL_ROW row = mysql_fetch_row(result);
// pretend to do something with it
- printf(".");
- /* printf("lease_id=%s addr=%s valid_lft=%s cltt=%s\n",
- (row[0]?row[0]:"NULL"),
- (row[1]?row[1]:"NULL"),
- (row[4]?row[4]:"NULL"),
- (row[5]?row[5]:"NULL")); */
+ if (Verbose_) {
+ printf(".");
+ }
mysql_free_result(result);
} else {
- // printf("Address %x not found.\n", x);
- printf("x");
+ if (Verbose_) {
+ printf("x");
+ }
}
}
MYSQL_RES * result = NULL;
- printf(".");
+ if (Verbose_)
+ printf(".");
}
printf("\n");
MYSQL_RES * result = NULL;
- printf(".");
+ if (Verbose_) {
+ printf(".");
+ }
}
printf("\n");
const char * user = "root";
const char * passwd = "secret";
const char * dbname = "kea";
- uint32_t num = 10000;
+ uint32_t num = 100;
+ bool sync = true;
+ bool verbose = true;
- MySQL_uBenchmark bench(hostname, user, passwd, dbname, num);
+ MySQL_uBenchmark bench(hostname, user, passwd, dbname, num, sync, verbose);
int result = bench.run();
class MySQL_uBenchmark: public uBenchmark {
public:
MySQL_uBenchmark(const std::string& hostname, const std::string& user,
- const std::string& passwd, const std::string& db,
- uint32_t num_iterations);
+ const std::string& pass, const std::string& db,
+ uint32_t num_iterations, bool sync,
+ bool verbose);
virtual void printInfo();
virtual void connect();
protected:
void failure(const char* operation);
- std::string Hostname_;
- std::string User_;
- std::string Pass_;
- std::string DB_;
MYSQL * Conn_;
};
using namespace std;
SQLite_uBenchmark::SQLite_uBenchmark(const string& filename,
- uint32_t num_iterations)
- :uBenchmark(num_iterations), Filename_(filename), DB_(NULL) {
+ uint32_t num_iterations,
+ bool sync, bool verbose)
+ :uBenchmark(num_iterations, filename, sync, verbose),
+ DB_(NULL) {
}
}
void SQLite_uBenchmark::connect() {
- int result = sqlite3_open(Filename_.c_str(), &DB_);
+ int result = sqlite3_open(DBName_.c_str(), &DB_);
if (result) {
- sqlite3_open(Filename_.c_str(), &DB_);
+ sqlite3_open(DBName_.c_str(), &DB_);
failure("Failed to open DB file");
}
sqlite3_exec(DB_, "DELETE FROM lease4", NULL, NULL, NULL);
- sqlite3_exec(DB_, "PRAGMA synchronous = OFF", NULL, NULL, NULL);
+ if (Sync_) {
+ sqlite3_exec(DB_, "PRAGMA synchronous = ON", NULL, NULL, NULL);
+ } else {
+ sqlite3_exec(DB_, "PRAGMA synchronous = OFF", NULL, NULL, NULL);
+ }
// see http://www.sqlite.org/pragma.html#pragma_journal_mode
// for detailed explanation. Available modes: DELETE, TRUNCATE,
tmp << "INSERT error:" << errorMsg;
failure(tmp.str().c_str());
} else {
- printf(".");
+ if (Verbose_) {
+ printf(".");
+ }
};
}
}
if (cnt) {
- printf(".");
+ if (Verbose_) {
+ printf(".");
+ }
} else {
- printf("X");
+ if (Verbose_) {
+ printf("X");
+ }
}
}
tmp << "UPDATE error:" << errorMsg;
failure(tmp.str().c_str());
}
- printf(".");
+ if (Verbose_) {
+ printf(".");
+ }
}
printf("\n");
tmp << "DELETE error:" << errorMsg;
failure(tmp.str().c_str());
}
- printf(".");
+ if (Verbose_) {
+ printf(".");
+ }
}
printf("\n");
int main(int argc, const char * argv[]) {
const char * filename = "sqlite.db";
- uint32_t num = 10000;
+ uint32_t num = 100;
+ bool sync = true;
+ bool verbose = true;
- SQLite_uBenchmark bench(filename, num);
+ SQLite_uBenchmark bench(filename, num, sync, verbose);
int result = bench.run();
class SQLite_uBenchmark: public uBenchmark {
public:
SQLite_uBenchmark(const std::string& filename,
- uint32_t num_iterations);
+ uint32_t num_iterations,
+ bool sync, bool verbose);
virtual void printInfo();
virtual void connect();
protected:
void failure(const char* operation);
- std::string Filename_;
sqlite3 *DB_;
};