query_bench.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_config.h>
  28. #include <auth/datasrc_clients_mgr.h>
  29. #include <auth/query.h>
  30. #include <asiodns/asiodns.h>
  31. #include <asiolink/asiolink.h>
  32. #include <boost/shared_ptr.hpp>
  33. #include <stdlib.h>
  34. #include <iostream>
  35. #include <vector>
  36. using namespace std;
  37. using namespace isc;
  38. using namespace isc::data;
  39. using namespace isc::auth;
  40. using namespace isc::dns;
  41. using namespace isc::log;
  42. using namespace isc::util;
  43. using namespace isc::util::unittests;
  44. using namespace isc::xfr;
  45. using namespace isc::bench;
  46. using namespace isc::asiodns;
  47. using namespace isc::asiolink;
  48. namespace {
  49. // Commonly used constant:
  50. XfroutClient xfrout_client("dummy_path"); // path doesn't matter
  51. // Just something to pass as the server to resume
  52. class DummyServer : public DNSServer {
  53. public:
  54. virtual void operator()(asio::error_code, size_t) {}
  55. virtual void resume(const bool) {}
  56. virtual DNSServer* clone() {
  57. return (new DummyServer(*this));
  58. }
  59. };
  60. class QueryBenchMark {
  61. protected:
  62. // Maintain dynamically generated objects via shared pointers because
  63. // QueryBenchMark objects will be copied.
  64. typedef boost::shared_ptr<AuthSrv> AuthSrvPtr;
  65. private:
  66. typedef boost::shared_ptr<const IOEndpoint> IOEndpointPtr;
  67. protected:
  68. QueryBenchMark(const BenchQueries& queries, Message& query_message,
  69. OutputBuffer& buffer) :
  70. server_(new AuthSrv(xfrout_client, ddns_forwarder)),
  71. queries_(queries),
  72. query_message_(query_message),
  73. buffer_(buffer),
  74. dummy_socket(IOSocket::getDummyUDPSocket()),
  75. dummy_endpoint(IOEndpointPtr(IOEndpoint::create(IPPROTO_UDP,
  76. IOAddress("192.0.2.1"),
  77. 53210)))
  78. {}
  79. public:
  80. unsigned int run() {
  81. BenchQueries::const_iterator query;
  82. const BenchQueries::const_iterator query_end = queries_.end();
  83. DummyServer server;
  84. for (query = queries_.begin(); query != query_end; ++query) {
  85. IOMessage io_message(&(*query)[0], (*query).size(), dummy_socket,
  86. *dummy_endpoint);
  87. query_message_.clear(Message::PARSE);
  88. buffer_.clear();
  89. server_->processMessage(io_message, query_message_, buffer_,
  90. &server);
  91. }
  92. return (queries_.size());
  93. }
  94. private:
  95. MockSocketSessionForwarder ddns_forwarder;
  96. protected:
  97. AuthSrvPtr server_;
  98. private:
  99. const BenchQueries& queries_;
  100. Message& query_message_;
  101. OutputBuffer& buffer_;
  102. IOSocket& dummy_socket;
  103. IOEndpointPtr dummy_endpoint;
  104. };
  105. class Sqlite3QueryBenchMark : public QueryBenchMark {
  106. public:
  107. Sqlite3QueryBenchMark(const char* const datasrc_file,
  108. const BenchQueries& queries,
  109. Message& query_message,
  110. OutputBuffer& buffer) :
  111. QueryBenchMark(queries, query_message, buffer)
  112. {
  113. // Note: swapDataSrcClientLists() may be deprecated, but until then
  114. // we use it because we want to be synchronized with the server.
  115. server_->getDataSrcClientsMgr().swapDataSrcClientLists(
  116. configureDataSource(
  117. Element::fromJSON("{\"IN\":"
  118. " [{\"type\": \"sqlite3\","
  119. " \"params\": {"
  120. " \"database_file\": \"" +
  121. string(datasrc_file) + "\"}}]}")));
  122. }
  123. };
  124. class MemoryQueryBenchMark : public QueryBenchMark {
  125. public:
  126. MemoryQueryBenchMark(const char* const zone_file,
  127. const char* const zone_origin,
  128. const BenchQueries& queries,
  129. Message& query_message,
  130. OutputBuffer& buffer) :
  131. QueryBenchMark(queries, query_message, buffer)
  132. {
  133. server_->getDataSrcClientsMgr().swapDataSrcClientLists(
  134. configureDataSource(
  135. Element::fromJSON("{\"IN\":"
  136. " [{\"type\": \"MasterFiles\","
  137. " \"cache-enable\": true, "
  138. " \"params\": {\"" +
  139. string(zone_origin) + "\": \"" +
  140. string(zone_file) + "\"}}]}")));
  141. }
  142. };
  143. void
  144. printQPSResult(unsigned int iteration, double duration,
  145. double iteration_per_second)
  146. {
  147. cout.precision(6);
  148. cout << "Processed " << iteration << " queries in "
  149. << fixed << duration << "s";
  150. cout.precision(2);
  151. cout << " (" << fixed << iteration_per_second << "qps)" << endl;
  152. }
  153. }
  154. namespace isc {
  155. namespace bench {
  156. template<>
  157. void
  158. BenchMark<Sqlite3QueryBenchMark>::printResult() const {
  159. printQPSResult(getIteration(), getDuration(), getIterationPerSecond());
  160. }
  161. template<>
  162. void
  163. BenchMark<MemoryQueryBenchMark>::printResult() const {
  164. printQPSResult(getIteration(), getDuration(), getIterationPerSecond());
  165. }
  166. }
  167. }
  168. namespace {
  169. const int ITERATION_DEFAULT = 1;
  170. enum DataSrcType {
  171. SQLITE3,
  172. MEMORY
  173. };
  174. void
  175. usage() {
  176. cerr <<
  177. "Usage: query_bench [-d] [-n iterations] [-t datasrc_type] [-o origin]"
  178. " datasrc_file query_datafile\n"
  179. " -d Enable debug logging to stdout\n"
  180. " -n Number of iterations per test case (default: "
  181. << ITERATION_DEFAULT << ")\n"
  182. " -t Type of data source: sqlite3|memory (default: sqlite3)\n"
  183. " -o Origin name of datasrc_file necessary for \"memory\", "
  184. "ignored for others\n"
  185. " datasrc_file: sqlite3 DB file for \"sqlite3\", "
  186. "textual master file for \"memory\" datasrc\n"
  187. " query_datafile: queryperf style input data"
  188. << endl;
  189. exit (1);
  190. }
  191. }
  192. int
  193. main(int argc, char* argv[]) {
  194. int ch;
  195. int iteration = ITERATION_DEFAULT;
  196. const char* opt_datasrc_type = "sqlite3";
  197. const char* origin = NULL;
  198. bool debug_log = false;
  199. while ((ch = getopt(argc, argv, "dn:t:o:")) != -1) {
  200. switch (ch) {
  201. case 'n':
  202. iteration = atoi(optarg);
  203. break;
  204. case 't':
  205. opt_datasrc_type = optarg;
  206. break;
  207. case 'o':
  208. origin = optarg;
  209. break;
  210. case 'd':
  211. debug_log = true;
  212. break;
  213. case '?':
  214. default:
  215. usage();
  216. }
  217. }
  218. argc -= optind;
  219. argv += optind;
  220. if (argc < 2) {
  221. usage();
  222. }
  223. const char* const datasrc_file = argv[0];
  224. const char* const query_data_file = argv[1];
  225. // By default disable logging to avoid unwanted noise.
  226. initLogger("query-bench", debug_log ? isc::log::DEBUG : isc::log::NONE,
  227. isc::log::MAX_DEBUG_LEVEL, NULL);
  228. DataSrcType datasrc_type = SQLITE3;
  229. if (strcmp(opt_datasrc_type, "sqlite3") == 0) {
  230. ; // no need to override
  231. } else if (strcmp(opt_datasrc_type, "memory") == 0) {
  232. datasrc_type = MEMORY;
  233. } else {
  234. cerr << "Unknown data source type: " << datasrc_type << endl;
  235. return (1);
  236. }
  237. if (datasrc_type == MEMORY && origin == NULL) {
  238. cerr << "'-o Origin' is missing for memory data source " << endl;
  239. return (1);
  240. }
  241. try {
  242. BenchQueries queries;
  243. loadQueryData(query_data_file, queries, RRClass::IN());
  244. OutputBuffer buffer(4096);
  245. Message message(Message::PARSE);
  246. cout << "Parameters:" << endl;
  247. cout << " Iterations: " << iteration << endl;
  248. cout << " Data Source: type=" << opt_datasrc_type << ", file=" <<
  249. datasrc_file << endl;
  250. if (origin != NULL) {
  251. cout << " Origin: " << origin << endl;
  252. }
  253. cout << " Query data: file=" << query_data_file << " ("
  254. << queries.size() << " queries)" << endl << endl;
  255. switch (datasrc_type) {
  256. case SQLITE3:
  257. cout << "Benchmark with SQLite3" << endl;
  258. BenchMark<Sqlite3QueryBenchMark>(
  259. iteration, Sqlite3QueryBenchMark(datasrc_file, queries,
  260. message, buffer));
  261. break;
  262. case MEMORY:
  263. cout << "Benchmark with In Memory Data Source" << endl;
  264. BenchMark<MemoryQueryBenchMark>(
  265. iteration, MemoryQueryBenchMark(datasrc_file, origin, queries,
  266. message, buffer));
  267. break;
  268. }
  269. } catch (const std::exception& ex) {
  270. cout << "Test unexpectedly failed: " << ex.what() << endl;
  271. return (1);
  272. }
  273. return (0);
  274. }