query_bench.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <util/threads/lock.h>
  19. #include <dns/message.h>
  20. #include <dns/name.h>
  21. #include <dns/question.h>
  22. #include <dns/rrclass.h>
  23. #include <log/logger_support.h>
  24. #include <xfr/xfrout_client.h>
  25. #include <util/unittests/mock_socketsession.h>
  26. #include <auth/auth_srv.h>
  27. #include <auth/auth_config.h>
  28. #include <auth/datasrc_config.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. isc::util::thread::Mutex::Locker locker(
  114. server_->getDataSrcClientListMutex());
  115. server_->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. isc::util::thread::Mutex::Locker locker(
  134. server_->getDataSrcClientListMutex());
  135. server_->swapDataSrcClientLists(
  136. configureDataSource(
  137. Element::fromJSON("{\"IN\":"
  138. " [{\"type\": \"MasterFiles\","
  139. " \"cache-enable\": true, "
  140. " \"params\": {\"" +
  141. string(zone_origin) + "\": \"" +
  142. string(zone_file) + "\"}}]}")));
  143. }
  144. };
  145. void
  146. printQPSResult(unsigned int iteration, double duration,
  147. double iteration_per_second)
  148. {
  149. cout.precision(6);
  150. cout << "Processed " << iteration << " queries in "
  151. << fixed << duration << "s";
  152. cout.precision(2);
  153. cout << " (" << fixed << iteration_per_second << "qps)" << endl;
  154. }
  155. }
  156. namespace isc {
  157. namespace bench {
  158. template<>
  159. void
  160. BenchMark<Sqlite3QueryBenchMark>::printResult() const {
  161. printQPSResult(getIteration(), getDuration(), getIterationPerSecond());
  162. }
  163. template<>
  164. void
  165. BenchMark<MemoryQueryBenchMark>::printResult() const {
  166. printQPSResult(getIteration(), getDuration(), getIterationPerSecond());
  167. }
  168. }
  169. }
  170. namespace {
  171. const int ITERATION_DEFAULT = 1;
  172. enum DataSrcType {
  173. SQLITE3,
  174. MEMORY
  175. };
  176. void
  177. usage() {
  178. cerr <<
  179. "Usage: query_bench [-d] [-n iterations] [-t datasrc_type] [-o origin]"
  180. " datasrc_file query_datafile\n"
  181. " -d Enable debug logging to stdout\n"
  182. " -n Number of iterations per test case (default: "
  183. << ITERATION_DEFAULT << ")\n"
  184. " -t Type of data source: sqlite3|memory (default: sqlite3)\n"
  185. " -o Origin name of datasrc_file necessary for \"memory\", "
  186. "ignored for others\n"
  187. " datasrc_file: sqlite3 DB file for \"sqlite3\", "
  188. "textual master file for \"memory\" datasrc\n"
  189. " query_datafile: queryperf style input data"
  190. << endl;
  191. exit (1);
  192. }
  193. }
  194. int
  195. main(int argc, char* argv[]) {
  196. int ch;
  197. int iteration = ITERATION_DEFAULT;
  198. const char* opt_datasrc_type = "sqlite3";
  199. const char* origin = NULL;
  200. bool debug_log = false;
  201. while ((ch = getopt(argc, argv, "dn:t:o:")) != -1) {
  202. switch (ch) {
  203. case 'n':
  204. iteration = atoi(optarg);
  205. break;
  206. case 't':
  207. opt_datasrc_type = optarg;
  208. break;
  209. case 'o':
  210. origin = optarg;
  211. break;
  212. case 'd':
  213. debug_log = true;
  214. break;
  215. case '?':
  216. default:
  217. usage();
  218. }
  219. }
  220. argc -= optind;
  221. argv += optind;
  222. if (argc < 2) {
  223. usage();
  224. }
  225. const char* const datasrc_file = argv[0];
  226. const char* const query_data_file = argv[1];
  227. // By default disable logging to avoid unwanted noise.
  228. initLogger("query-bench", debug_log ? isc::log::DEBUG : isc::log::NONE,
  229. isc::log::MAX_DEBUG_LEVEL, NULL);
  230. DataSrcType datasrc_type = SQLITE3;
  231. if (strcmp(opt_datasrc_type, "sqlite3") == 0) {
  232. ; // no need to override
  233. } else if (strcmp(opt_datasrc_type, "memory") == 0) {
  234. datasrc_type = MEMORY;
  235. } else {
  236. cerr << "Unknown data source type: " << datasrc_type << endl;
  237. return (1);
  238. }
  239. if (datasrc_type == MEMORY && origin == NULL) {
  240. cerr << "'-o Origin' is missing for memory data source " << endl;
  241. return (1);
  242. }
  243. try {
  244. BenchQueries queries;
  245. loadQueryData(query_data_file, queries, RRClass::IN());
  246. OutputBuffer buffer(4096);
  247. Message message(Message::PARSE);
  248. cout << "Parameters:" << endl;
  249. cout << " Iterations: " << iteration << endl;
  250. cout << " Data Source: type=" << opt_datasrc_type << ", file=" <<
  251. datasrc_file << endl;
  252. if (origin != NULL) {
  253. cout << " Origin: " << origin << endl;
  254. }
  255. cout << " Query data: file=" << query_data_file << " ("
  256. << queries.size() << " queries)" << endl << endl;
  257. switch (datasrc_type) {
  258. case SQLITE3:
  259. cout << "Benchmark with SQLite3" << endl;
  260. BenchMark<Sqlite3QueryBenchMark>(
  261. iteration, Sqlite3QueryBenchMark(datasrc_file, queries,
  262. message, buffer));
  263. break;
  264. case MEMORY:
  265. cout << "Benchmark with In Memory Data Source" << endl;
  266. BenchMark<MemoryQueryBenchMark>(
  267. iteration, MemoryQueryBenchMark(datasrc_file, origin, queries,
  268. message, buffer));
  269. break;
  270. }
  271. } catch (const std::exception& ex) {
  272. cout << "Test unexpectedly failed: " << ex.what() << endl;
  273. return (1);
  274. }
  275. return (0);
  276. }