query_bench.cc 9.5 KB

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