Browse Source

copy benchmark framework from the experimental branch

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac241@2138 e5f2f494-b856-4b98-b285-d166d9295462
JINMEI Tatuya 15 years ago
parent
commit
5389af6603
4 changed files with 248 additions and 0 deletions
  1. 4 0
      src/lib/bench/Makefile.am
  2. 103 0
      src/lib/bench/benchmark.h
  3. 105 0
      src/lib/bench/benchmark_util.cc
  4. 36 0
      src/lib/bench/benchmark_util.h

+ 4 - 0
src/lib/bench/Makefile.am

@@ -0,0 +1,4 @@
+AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
+
+lib_LTLIBRARIES = libbench.la
+libbench_la_SOURCES = benchmark_util.h benchmark_util.cc

+ 103 - 0
src/lib/bench/benchmark.h

@@ -0,0 +1,103 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __BENCHMARK_H
+#define __BENCHMARK_H 1
+
+#include <sys/time.h>
+
+#include <iostream>
+#include <ios>
+
+namespace isc {
+namespace bench {
+
+template <typename T>
+class BenchMark {
+public:
+    BenchMark(const int iterations, T& target) :
+        iterations_(iterations), sub_iterations_(0), target_(target) {}
+    void setUp() {}
+    void tearDown() {}
+    void run() {
+        setUp();
+
+        struct timeval beg, end;
+        gettimeofday(&beg, NULL);
+        for (int i = 0; i < iterations_; ++i) {
+            sub_iterations_ += target_.run();
+        }
+        gettimeofday(&end, NULL);
+        tv_diff_ = tv_subtract(end, beg);
+
+        printResult();
+        tearDown();
+    }
+    void printResult() const {
+        std::cout.precision(6);
+        std::cout << "Performed " << getIteration() << " iterations in "
+                  << std::fixed << getDuration() << "s";
+        std::cout.precision(2);
+        std::cout << " (" << std::fixed << getIterationPerSecond() << "ips)"
+                  << std::endl;
+    }
+    unsigned int getIteration() const { return (sub_iterations_); }
+    double getDuration() const {
+        return (tv_diff_.tv_sec +
+                static_cast<double>(tv_diff_.tv_usec) / ONE_MILLION);
+    }
+    double getAverageTime() const {
+        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));
+    }
+private:
+    // return t1 - t2
+    struct timeval tv_subtract(const struct timeval& t1,
+                               const struct timeval& t2)
+    {
+        struct timeval result;
+
+        result.tv_sec = t1.tv_sec - t2.tv_sec;
+        if (t1.tv_usec >= t2.tv_usec) {
+            result.tv_usec = t1.tv_usec- t2.tv_usec;
+        } else {
+            result.tv_usec = ONE_MILLION + t1.tv_usec - t2.tv_usec;
+            --result.tv_sec;
+        }
+
+        return (result);
+    }
+private:
+    static const int ONE_MILLION = 1000000;
+    const unsigned int iterations_;
+    unsigned int sub_iterations_;
+    T& target_;
+    struct timeval tv_diff_;
+};
+
+}
+}
+#endif  // __BENCHMARK_H
+
+// Local Variables: 
+// mode: c++
+// End: 

+ 105 - 0
src/lib/bench/benchmark_util.cc

@@ -0,0 +1,105 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#include <fstream>
+#include <iostream>
+#include <string>
+#include <vector>
+
+#include <exceptions/exceptions.h>
+
+#include <dns/buffer.h>
+#include <dns/exceptions.h>
+#include <dns/name.h>
+#include <dns/message.h>
+#include <dns/messagerenderer.h>
+#include <dns/rrtype.h>
+#include <dns/rrclass.h>
+#include <dns/question.h>
+
+#include <bench/benchmark_util.h>
+
+using namespace std;
+using namespace isc;
+using namespace isc::dns;
+
+namespace isc {
+namespace bench {
+void
+loadQueryData(const char* const input_file, BenchQueries& queries,
+              const RRClass& qclass)
+{
+    ifstream ifs;
+
+    ifs.open(input_file, ios_base::in);
+    if ((ifs.rdstate() & istream::failbit) != 0) {
+        isc_throw(Exception, "failed to query data file: " +
+                  string(input_file));
+    }
+
+    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()) {
+        ++linenum;
+        if (ifs.bad() || ifs.fail()) {
+            isc_throw(Exception,
+                      "Unexpected line in query data file around line " <<
+                      linenum);
+        }
+        if (line.empty() || line[0] == '#') {
+            continue;           // skip comment and blank lines
+        }
+
+        istringstream iss(line);
+        string qname_string, qtype_string;
+        iss >> qname_string >> qtype_string;
+        if (iss.bad() || iss.fail()) {
+            cerr << "unexpected input around line " << linenum << " (ignored)"
+                 << endl;
+            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));
+
+            renderer.clear();
+            query_message.toWire(renderer);
+            vector<unsigned char> query_data(
+                static_cast<const unsigned char*>(buffer.getData()),
+                static_cast<const unsigned char*>(buffer.getData()) +
+                buffer.getLength());
+            queries.push_back(query_data);
+        } catch (const Exception& error) {
+            cerr << "failed to parse/create query around line " << linenum <<
+                ": " << error.what() << " (ignored)" << endl;
+            continue;
+        }
+    }
+}
+}
+}

+ 36 - 0
src/lib/bench/benchmark_util.h

@@ -0,0 +1,36 @@
+// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+// $Id$
+
+#ifndef __BENCHMARK_UTIL_H
+#define __BENCHMARK_UTIL_H 1
+
+#include <vector>
+
+namespace isc {
+namespace dns {
+class RRClass;
+}
+namespace bench {
+typedef std::vector<std::vector<unsigned char> > BenchQueries; 
+void loadQueryData(const char* input_file, BenchQueries& queries,
+                   const isc::dns::RRClass& qclass);
+}
+}
+#endif  // __BENCHMARK_UTIL_H
+
+// Local Variables: 
+// mode: c++
+// End: