[svn] commit: r2490 - in /branches/trac241: ./ src/lib/ src/lib/bench/ src/lib/bench/example/ src/lib/bench/tests/ src/lib/bench/tests/testdata/

BIND 10 source code commits bind10-changes at lists.isc.org
Mon Jul 12 07:18:40 UTC 2010


Author: jinmei
Date: Mon Jul 12 07:18:40 2010
New Revision: 2490

Log:
added tests and an example.

Added:
    branches/trac241/src/lib/bench/example/
    branches/trac241/src/lib/bench/example/Makefile.am
    branches/trac241/src/lib/bench/example/search_bench.cc
    branches/trac241/src/lib/bench/tests/
    branches/trac241/src/lib/bench/tests/Makefile.am
    branches/trac241/src/lib/bench/tests/benchmark_unittest.cc
    branches/trac241/src/lib/bench/tests/loadquery_unittest.cc
    branches/trac241/src/lib/bench/tests/run_unittests.cc
    branches/trac241/src/lib/bench/tests/testdata/
    branches/trac241/src/lib/bench/tests/testdata/query.txt
Modified:
    branches/trac241/configure.ac
    branches/trac241/src/lib/Makefile.am
    branches/trac241/src/lib/bench/Makefile.am
    branches/trac241/src/lib/bench/benchmark.h
    branches/trac241/src/lib/bench/benchmark_util.cc
    branches/trac241/src/lib/bench/benchmark_util.h

Modified: branches/trac241/configure.ac
==============================================================================
--- branches/trac241/configure.ac (original)
+++ branches/trac241/configure.ac Mon Jul 12 07:18:40 2010
@@ -405,6 +405,9 @@
                  src/bin/xfrout/tests/Makefile
                  src/bin/usermgr/Makefile
                  src/lib/Makefile
+                 src/lib/bench/Makefile
+                 src/lib/bench/example/Makefile
+                 src/lib/bench/tests/Makefile
                  src/lib/cc/Makefile
                  src/lib/python/Makefile
                  src/lib/python/isc/Makefile

Modified: branches/trac241/src/lib/Makefile.am
==============================================================================
--- branches/trac241/src/lib/Makefile.am (original)
+++ branches/trac241/src/lib/Makefile.am Mon Jul 12 07:18:40 2010
@@ -1,4 +1,4 @@
-SUBDIRS = exceptions dns cc config datasrc python
+SUBDIRS = exceptions dns cc config datasrc bench python
 if HAVE_BOOST_PYTHON
 SUBDIRS += xfr
 endif

Modified: branches/trac241/src/lib/bench/Makefile.am
==============================================================================
--- branches/trac241/src/lib/bench/Makefile.am (original)
+++ branches/trac241/src/lib/bench/Makefile.am Mon Jul 12 07:18:40 2010
@@ -1,4 +1,9 @@
+SUBDIRS = . tests example
+
 AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
+AM_CXXFLAGS = $(B10_CXXFLAGS)
+
+CLEANFILES = *.gcno *.gcda
 
 lib_LTLIBRARIES = libbench.la
 libbench_la_SOURCES = benchmark_util.h benchmark_util.cc

Modified: branches/trac241/src/lib/bench/benchmark.h
==============================================================================
--- branches/trac241/src/lib/bench/benchmark.h (original)
+++ branches/trac241/src/lib/bench/benchmark.h Mon Jul 12 07:18:40 2010
@@ -28,8 +28,20 @@
 template <typename T>
 class BenchMark {
 public:
-    BenchMark(const int iterations, T& target) :
-        iterations_(iterations), sub_iterations_(0), target_(target) {}
+    BenchMark(const int iterations, T target) :
+        iterations_(iterations), sub_iterations_(0), target_(target)
+    {
+        run();
+        printResult();
+    }
+    BenchMark(const int iterations, T& target, const bool immediate = true) :
+        iterations_(iterations), sub_iterations_(0), target_(target)
+    {
+        if (immediate) {
+            run();
+            printResult();
+        }
+    }
     void setUp() {}
     void tearDown() {}
     void run() {
@@ -43,7 +55,6 @@
         gettimeofday(&end, NULL);
         tv_diff_ = tv_subtract(end, beg);
 
-        printResult();
         tearDown();
     }
     void printResult() const {
@@ -60,15 +71,24 @@
                 static_cast<double>(tv_diff_.tv_usec) / ONE_MILLION);
     }
     double getAverageTime() const {
+        if (sub_iterations_ == 0) {
+            return (TIME_FAILURE);
+        }
         return ((tv_diff_.tv_sec +
                  static_cast<double>(tv_diff_.tv_usec) / ONE_MILLION ) /
                 sub_iterations_);
     }
     double getIterationPerSecond() const {
-        return (sub_iterations_ /
-                (tv_diff_.tv_sec +
-                 static_cast<double>(tv_diff_.tv_usec) / ONE_MILLION));
+        const double duration_usec = tv_diff_.tv_sec +
+            static_cast<double>(tv_diff_.tv_usec) / ONE_MILLION;
+        if (duration_usec == 0) {
+            return (ITERATION_FAILURE);
+        }
+        return (sub_iterations_ / duration_usec);
     }
+public:
+    static const double TIME_FAILURE = -1;
+    static const double ITERATION_FAILURE = -1;
 private:
     // return t1 - t2
     struct timeval tv_subtract(const struct timeval& t1,

Modified: branches/trac241/src/lib/bench/benchmark_util.cc
==============================================================================
--- branches/trac241/src/lib/bench/benchmark_util.cc (original)
+++ branches/trac241/src/lib/bench/benchmark_util.cc Mon Jul 12 07:18:40 2010
@@ -40,25 +40,32 @@
 namespace bench {
 void
 loadQueryData(const char* const input_file, BenchQueries& queries,
-              const RRClass& qclass)
+              const RRClass& qclass, const bool strict)
 {
     ifstream ifs;
 
     ifs.open(input_file, ios_base::in);
     if ((ifs.rdstate() & istream::failbit) != 0) {
-        isc_throw(Exception, "failed to query data file: " +
+        isc_throw(BenchMarkError, "failed to load query data file: " +
                   string(input_file));
     }
+    loadQueryData(ifs, queries, qclass, strict);
+    ifs.close();
+}
 
+void
+loadQueryData(istream& input, BenchQueries& queries, const RRClass& qclass,
+              const bool strict)
+{
     string line;
     unsigned int linenum = 0;
     Message query_message(Message::RENDER);
     OutputBuffer buffer(128); // this should be sufficiently large
     MessageRenderer renderer(buffer);
-    while (getline(ifs, line), !ifs.eof()) {
+    while (getline(input, line), !input.eof()) {
         ++linenum;
-        if (ifs.bad() || ifs.fail()) {
-            isc_throw(Exception,
+        if (input.bad() || input.fail()) {
+            isc_throw(BenchMarkError,
                       "Unexpected line in query data file around line " <<
                       linenum);
         }
@@ -70,22 +77,23 @@
         string qname_string, qtype_string;
         iss >> qname_string >> qtype_string;
         if (iss.bad() || iss.fail()) {
-            cerr << "unexpected input around line " << linenum << " (ignored)"
-                 << endl;
+            if (strict) {
+                isc_throw(BenchMarkError,
+                          "load query: unexpected input around line " <<
+                          linenum);
+            }
             continue;
         }
 
         // We expect broken lines of data, which will be ignored with a
         // warning message.
         try {
-            Name qname(qname_string);
-            RRType qtype(qtype_string);
-
             query_message.clear(Message::RENDER);
             query_message.setQid(0);
             query_message.setOpcode(Opcode::QUERY());
             query_message.setRcode(Rcode::NOERROR());
-            query_message.addQuestion(Question(qname, qclass, qtype));
+            query_message.addQuestion(Question(Name(qname_string), qclass,
+                                               RRType(qtype_string)));
 
             renderer.clear();
             query_message.toWire(renderer);
@@ -95,8 +103,11 @@
                 buffer.getLength());
             queries.push_back(query_data);
         } catch (const Exception& error) {
-            cerr << "failed to parse/create query around line " << linenum <<
-                ": " << error.what() << " (ignored)" << endl;
+            if (strict) {
+                isc_throw(BenchMarkError,
+                          "failed to parse/create query around line " <<
+                          linenum);
+            }
             continue;
         }
     }

Modified: branches/trac241/src/lib/bench/benchmark_util.h
==============================================================================
--- branches/trac241/src/lib/bench/benchmark_util.h (original)
+++ branches/trac241/src/lib/bench/benchmark_util.h Mon Jul 12 07:18:40 2010
@@ -17,16 +17,31 @@
 #ifndef __BENCHMARK_UTIL_H
 #define __BENCHMARK_UTIL_H 1
 
+#include <istream>
 #include <vector>
+
+#include <exceptions/exceptions.h>
 
 namespace isc {
 namespace dns {
 class RRClass;
 }
+
 namespace bench {
+class BenchMarkError : public Exception {
+public:
+    BenchMarkError(const char* file, size_t line, const char* what) :
+        isc::Exception(file, line, what) {}
+};
+
 typedef std::vector<std::vector<unsigned char> > BenchQueries; 
+
+/// Describe exception guarantee.  This function only offers the basic
+/// exception guarantee.
 void loadQueryData(const char* input_file, BenchQueries& queries,
-                   const isc::dns::RRClass& qclass);
+                   const isc::dns::RRClass& qclass, const bool strict = false);
+void loadQueryData(std::istream& input, BenchQueries& queries,
+                   const isc::dns::RRClass& qclass, const bool strict = false);
 }
 }
 #endif  // __BENCHMARK_UTIL_H




More information about the bind10-changes mailing list