query_bench.cc 8.8 KB

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