query_bench.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <bench/benchmark.h>
  16. #include <bench/benchmark_util.h>
  17. #include <util/buffer.h>
  18. #include <dns/message.h>
  19. #include <dns/name.h>
  20. #include <dns/question.h>
  21. #include <dns/rrclass.h>
  22. #include <log/logger_support.h>
  23. #include <xfr/xfrout_client.h>
  24. #include <util/unittests/mock_socketsession.h>
  25. #include <auth/auth_srv.h>
  26. #include <auth/auth_config.h>
  27. #include <auth/datasrc_configurator.h>
  28. #include <auth/query.h>
  29. #include <asiodns/asiodns.h>
  30. #include <asiolink/asiolink.h>
  31. #include <boost/shared_ptr.hpp>
  32. #include <stdlib.h>
  33. #include <iostream>
  34. #include <vector>
  35. using namespace std;
  36. using namespace isc;
  37. using namespace isc::data;
  38. using namespace isc::auth;
  39. using namespace isc::dns;
  40. using namespace isc::log;
  41. using namespace isc::util;
  42. using namespace isc::util::unittests;
  43. using namespace isc::xfr;
  44. using namespace isc::bench;
  45. using namespace isc::asiodns;
  46. using namespace isc::asiolink;
  47. namespace {
  48. // Commonly used constant:
  49. XfroutClient xfrout_client("dummy_path"); // path doesn't matter
  50. // Just something to pass as the server to resume
  51. class DummyServer : public DNSServer {
  52. public:
  53. virtual void operator()(asio::error_code, size_t) {}
  54. virtual void resume(const bool) {}
  55. virtual DNSServer* clone() {
  56. return (new DummyServer(*this));
  57. }
  58. };
  59. class QueryBenchMark {
  60. protected:
  61. // Maintain dynamically generated objects via shared pointers because
  62. // QueryBenchMark objects will be copied.
  63. typedef boost::shared_ptr<AuthSrv> AuthSrvPtr;
  64. private:
  65. typedef boost::shared_ptr<const IOEndpoint> IOEndpointPtr;
  66. protected:
  67. QueryBenchMark(const BenchQueries& queries, Message& query_message,
  68. OutputBuffer& buffer) :
  69. server_(new AuthSrv(xfrout_client, ddns_forwarder)),
  70. queries_(queries),
  71. query_message_(query_message),
  72. buffer_(buffer),
  73. dummy_socket(IOSocket::getDummyUDPSocket()),
  74. dummy_endpoint(IOEndpointPtr(IOEndpoint::create(IPPROTO_UDP,
  75. IOAddress("192.0.2.1"),
  76. 53210)))
  77. {}
  78. public:
  79. unsigned int run() {
  80. BenchQueries::const_iterator query;
  81. const BenchQueries::const_iterator query_end = queries_.end();
  82. DummyServer server;
  83. for (query = queries_.begin(); query != query_end; ++query) {
  84. IOMessage io_message(&(*query)[0], (*query).size(), dummy_socket,
  85. *dummy_endpoint);
  86. query_message_.clear(Message::PARSE);
  87. buffer_.clear();
  88. server_->processMessage(io_message, query_message_, buffer_,
  89. &server);
  90. }
  91. return (queries_.size());
  92. }
  93. private:
  94. MockSocketSessionForwarder ddns_forwarder;
  95. protected:
  96. AuthSrvPtr server_;
  97. private:
  98. const BenchQueries& queries_;
  99. Message& query_message_;
  100. OutputBuffer& buffer_;
  101. IOSocket& dummy_socket;
  102. IOEndpointPtr dummy_endpoint;
  103. };
  104. class Sqlite3QueryBenchMark : public QueryBenchMark {
  105. public:
  106. Sqlite3QueryBenchMark(const char* const datasrc_file,
  107. const BenchQueries& queries,
  108. Message& query_message,
  109. OutputBuffer& buffer) :
  110. QueryBenchMark(queries, query_message, buffer)
  111. {
  112. DataSourceConfigurator::testReconfigure(
  113. server_.get(),
  114. Element::fromJSON("{\"IN\":"
  115. " [{\"type\": \"sqlite3\","
  116. " \"params\": {"
  117. " \"database_file\": \"" +
  118. string(datasrc_file) + "\"}}]}"));
  119. }
  120. };
  121. class MemoryQueryBenchMark : public QueryBenchMark {
  122. public:
  123. MemoryQueryBenchMark(const char* const zone_file,
  124. const char* const zone_origin,
  125. const BenchQueries& queries,
  126. Message& query_message,
  127. OutputBuffer& buffer) :
  128. QueryBenchMark(queries, query_message, buffer)
  129. {
  130. DataSourceConfigurator::testReconfigure(
  131. server_.get(),
  132. Element::fromJSON("{\"IN\":"
  133. " [{\"type\": \"MasterFiles\","
  134. " \"cache-enable\": true, "
  135. " \"params\": {\"" +
  136. string(zone_origin) + "\": \"" +
  137. string(zone_file) + "\"}}]}"));
  138. }
  139. };
  140. void
  141. printQPSResult(unsigned int iteration, double duration,
  142. double iteration_per_second)
  143. {
  144. cout.precision(6);
  145. cout << "Processed " << iteration << " queries in "
  146. << fixed << duration << "s";
  147. cout.precision(2);
  148. cout << " (" << fixed << iteration_per_second << "qps)" << endl;
  149. }
  150. }
  151. namespace isc {
  152. namespace bench {
  153. template<>
  154. void
  155. BenchMark<Sqlite3QueryBenchMark>::printResult() const {
  156. printQPSResult(getIteration(), getDuration(), getIterationPerSecond());
  157. }
  158. template<>
  159. void
  160. BenchMark<MemoryQueryBenchMark>::printResult() const {
  161. printQPSResult(getIteration(), getDuration(), getIterationPerSecond());
  162. }
  163. }
  164. }
  165. namespace {
  166. const int ITERATION_DEFAULT = 1;
  167. enum DataSrcType {
  168. SQLITE3,
  169. MEMORY
  170. };
  171. void
  172. usage() {
  173. cerr <<
  174. "Usage: query_bench [-d] [-n iterations] [-t datasrc_type] [-o origin]"
  175. " datasrc_file query_datafile\n"
  176. " -d Enable debug logging to stdout\n"
  177. " -n Number of iterations per test case (default: "
  178. << ITERATION_DEFAULT << ")\n"
  179. " -t Type of data source: sqlite3|memory (default: sqlite3)\n"
  180. " -o Origin name of datasrc_file necessary for \"memory\", "
  181. "ignored for others\n"
  182. " datasrc_file: sqlite3 DB file for \"sqlite3\", "
  183. "textual master file for \"memory\" datasrc\n"
  184. " query_datafile: queryperf style input data"
  185. << endl;
  186. exit (1);
  187. }
  188. }
  189. int
  190. main(int argc, char* argv[]) {
  191. int ch;
  192. int iteration = ITERATION_DEFAULT;
  193. const char* opt_datasrc_type = "sqlite3";
  194. const char* origin = NULL;
  195. bool debug_log = false;
  196. while ((ch = getopt(argc, argv, "dn:t:o:")) != -1) {
  197. switch (ch) {
  198. case 'n':
  199. iteration = atoi(optarg);
  200. break;
  201. case 't':
  202. opt_datasrc_type = optarg;
  203. break;
  204. case 'o':
  205. origin = optarg;
  206. break;
  207. case 'd':
  208. debug_log = true;
  209. break;
  210. case '?':
  211. default:
  212. usage();
  213. }
  214. }
  215. argc -= optind;
  216. argv += optind;
  217. if (argc < 2) {
  218. usage();
  219. }
  220. const char* const datasrc_file = argv[0];
  221. const char* const query_data_file = argv[1];
  222. // By default disable logging to avoid unwanted noise.
  223. initLogger("query-bench", debug_log ? isc::log::DEBUG : isc::log::NONE,
  224. isc::log::MAX_DEBUG_LEVEL, NULL);
  225. DataSrcType datasrc_type = SQLITE3;
  226. if (strcmp(opt_datasrc_type, "sqlite3") == 0) {
  227. ; // no need to override
  228. } else if (strcmp(opt_datasrc_type, "memory") == 0) {
  229. datasrc_type = MEMORY;
  230. } else {
  231. cerr << "Unknown data source type: " << datasrc_type << endl;
  232. return (1);
  233. }
  234. if (datasrc_type == MEMORY && origin == NULL) {
  235. cerr << "'-o Origin' is missing for memory data source " << endl;
  236. return (1);
  237. }
  238. try {
  239. BenchQueries queries;
  240. loadQueryData(query_data_file, queries, RRClass::IN());
  241. OutputBuffer buffer(4096);
  242. Message message(Message::PARSE);
  243. cout << "Parameters:" << endl;
  244. cout << " Iterations: " << iteration << endl;
  245. cout << " Data Source: type=" << opt_datasrc_type << ", file=" <<
  246. datasrc_file << endl;
  247. if (origin != NULL) {
  248. cout << " Origin: " << origin << endl;
  249. }
  250. cout << " Query data: file=" << query_data_file << " ("
  251. << queries.size() << " queries)" << endl << endl;
  252. switch (datasrc_type) {
  253. case SQLITE3:
  254. cout << "Benchmark with SQLite3" << endl;
  255. BenchMark<Sqlite3QueryBenchMark>(
  256. iteration, Sqlite3QueryBenchMark(datasrc_file, queries,
  257. message, buffer));
  258. break;
  259. case MEMORY:
  260. cout << "Benchmark with In Memory Data Source" << endl;
  261. BenchMark<MemoryQueryBenchMark>(
  262. iteration, MemoryQueryBenchMark(datasrc_file, origin, queries,
  263. message, buffer));
  264. break;
  265. }
  266. } catch (const std::exception& ex) {
  267. cout << "Test unexpectedly failed: " << ex.what() << endl;
  268. return (1);
  269. }
  270. return (0);
  271. }