Browse Source

[2040] Support for sync/async switch implemented.

Tomek Mrugalski 12 years ago
parent
commit
325bc58ba3

+ 13 - 2
tests/tools/dhcp-ubench/benchmark.cc

@@ -19,9 +19,20 @@
 
 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) {

+ 14 - 1
tests/tools/dhcp-ubench/benchmark.h

@@ -20,7 +20,11 @@
 
 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;
@@ -38,9 +42,18 @@ public:
 
     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)

+ 51 - 31
tests/tools/dhcp-ubench/memfile_ubench.cc

@@ -24,7 +24,7 @@ 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(const std::string& filename, bool sync);
     ~memfile_LeaseMgr();
     bool addLease(Lease4Ptr lease);
     Lease4Ptr getLease(uint32_t addr);
@@ -36,42 +36,49 @@ protected:
     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) {
@@ -117,8 +124,11 @@ bool memfile_LeaseMgr::deleteLease(uint32_t addr) {
 }
 
 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) {
 
 }
 
@@ -128,7 +138,7 @@ void memfile_uBenchmark::failure(const char* operation) {
 
 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());
@@ -192,7 +202,9 @@ void memfile_uBenchmark::createLease4Test() {
         if (!LeaseMgr_->addLease(lease)) {
             failure("addLease() failed");
         } else {
-            printf(".");
+            if (Verbose_) {
+                printf(".");
+            }
         };
 
         addr++;
@@ -215,10 +227,12 @@ void memfile_uBenchmark::searchLease4Test() {
         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");
+            }
         }
     }
 
@@ -244,7 +258,9 @@ void memfile_uBenchmark::updateLease4Test() {
             tmp << "UPDATE failed for lease " << hex << x << dec;
             failure(tmp.str().c_str());
         }
-        printf(".");
+        if (Verbose_) {
+            printf(".");
+        }
     }
 
     printf("\n");
@@ -267,7 +283,9 @@ void memfile_uBenchmark::deleteLease4Test() {
             tmp << "UPDATE failed for lease " << hex << x << dec;
             failure(tmp.str().c_str());
         }
-        printf(".");
+        if (Verbose_) {
+            printf(".");
+        }
     }
 
     printf("\n");
@@ -281,9 +299,11 @@ void memfile_uBenchmark::printInfo() {
 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();
 

+ 1 - 1
tests/tools/dhcp-ubench/memfile_ubench.h

@@ -41,7 +41,7 @@ class memfile_LeaseMgr;
 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();

+ 35 - 18
tests/tools/dhcp-ubench/mysql_ubench.cc

@@ -27,9 +27,10 @@ using namespace std;
 
 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) {
 
 }
 
@@ -50,10 +51,10 @@ void MySQL_uBenchmark::connect() {
     }
 
     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;
@@ -63,6 +64,17 @@ void MySQL_uBenchmark::connect() {
     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() {
@@ -132,7 +144,9 @@ void MySQL_uBenchmark::createLease4Test() {
             // something failed.
             failure("INSERT query");
         } else {
-            printf(".");
+            if (Verbose_) {
+                printf(".");
+            }
         };
     }
     printf("\n");
@@ -179,17 +193,15 @@ void MySQL_uBenchmark::searchLease4Test() {
             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");
+            }
         }
     }
 
@@ -213,7 +225,8 @@ void MySQL_uBenchmark::updateLease4Test() {
 
         MYSQL_RES * result = NULL;
 
-        printf(".");
+        if (Verbose_)
+            printf(".");
     }
 
     printf("\n");
@@ -236,7 +249,9 @@ void MySQL_uBenchmark::deleteLease4Test() {
 
         MYSQL_RES * result = NULL;
 
-        printf(".");
+        if (Verbose_) {
+            printf(".");
+        }
     }
 
     printf("\n");
@@ -253,9 +268,11 @@ int main(int argc, const char * argv[]) {
     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();
 

+ 3 - 6
tests/tools/dhcp-ubench/mysql_ubench.h

@@ -18,8 +18,9 @@
 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();
@@ -32,9 +33,5 @@ public:
 protected:
     void failure(const char* operation);
 
-    std::string Hostname_;
-    std::string User_;
-    std::string Pass_;
-    std::string DB_;
     MYSQL * Conn_;
 };

+ 30 - 12
tests/tools/dhcp-ubench/sqlite_ubench.cc

@@ -24,8 +24,10 @@
 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) {
 
 }
 
@@ -34,15 +36,19 @@ void SQLite_uBenchmark::failure(const char* operation) {
 }
 
 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,
@@ -116,7 +122,9 @@ void SQLite_uBenchmark::createLease4Test() {
             tmp << "INSERT error:" << errorMsg;
             failure(tmp.str().c_str());
         } else {
-            printf(".");
+            if (Verbose_) {
+                printf(".");
+            }
         };
 
     }
@@ -168,9 +176,13 @@ void SQLite_uBenchmark::searchLease4Test() {
         }
 
         if (cnt) {
-            printf(".");
+            if (Verbose_) {
+                printf(".");
+            }
         } else {
-            printf("X");
+            if (Verbose_) {
+                printf("X");
+            }
         }
 
     }
@@ -199,7 +211,9 @@ void SQLite_uBenchmark::updateLease4Test() {
             tmp << "UPDATE error:" << errorMsg;
             failure(tmp.str().c_str());
         }
-        printf(".");
+        if (Verbose_) {
+            printf(".");
+        }
     }
 
     printf("\n");
@@ -226,7 +240,9 @@ void SQLite_uBenchmark::deleteLease4Test() {
             tmp << "DELETE error:" << errorMsg;
             failure(tmp.str().c_str());
         }
-        printf(".");
+        if (Verbose_) {
+            printf(".");
+        }
     }
 
     printf("\n");
@@ -242,9 +258,11 @@ void SQLite_uBenchmark::printInfo() {
 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();
 

+ 2 - 2
tests/tools/dhcp-ubench/sqlite_ubench.h

@@ -18,7 +18,8 @@
 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();
@@ -31,6 +32,5 @@ public:
 protected:
     void failure(const char* operation);
 
-    std::string Filename_;
     sqlite3 *DB_;
 };