benchmark_util.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <fstream>
  16. #include <iostream>
  17. #include <string>
  18. #include <vector>
  19. #include <exceptions/exceptions.h>
  20. #include <dns/buffer.h>
  21. #include <dns/exceptions.h>
  22. #include <dns/name.h>
  23. #include <dns/message.h>
  24. #include <dns/messagerenderer.h>
  25. #include <dns/opcode.h>
  26. #include <dns/rcode.h>
  27. #include <dns/rrtype.h>
  28. #include <dns/rrclass.h>
  29. #include <dns/question.h>
  30. #include <bench/benchmark_util.h>
  31. using namespace std;
  32. using namespace isc;
  33. using namespace isc::dns;
  34. namespace isc {
  35. namespace bench {
  36. void
  37. loadQueryData(const char* const input_file, BenchQueries& queries,
  38. const RRClass& qclass, const bool strict)
  39. {
  40. ifstream ifs;
  41. ifs.open(input_file, ios_base::in);
  42. if ((ifs.rdstate() & istream::failbit) != 0) {
  43. isc_throw(BenchMarkError, "failed to load query data file: " +
  44. string(input_file));
  45. }
  46. loadQueryData(ifs, queries, qclass, strict);
  47. ifs.close();
  48. }
  49. void
  50. loadQueryData(istream& input, BenchQueries& queries, const RRClass& qclass,
  51. const bool strict)
  52. {
  53. string line;
  54. unsigned int linenum = 0;
  55. Message query_message(Message::RENDER);
  56. OutputBuffer buffer(128); // this should be sufficiently large
  57. MessageRenderer renderer(buffer);
  58. while (getline(input, line), !input.eof()) {
  59. ++linenum;
  60. if (input.bad() || input.fail()) {
  61. isc_throw(BenchMarkError,
  62. "Unexpected line in query data file around line " <<
  63. linenum);
  64. }
  65. if (line.empty() || line[0] == '#') {
  66. continue; // skip comment and blank lines
  67. }
  68. istringstream iss(line);
  69. string qname_string, qtype_string;
  70. iss >> qname_string >> qtype_string;
  71. if (iss.bad() || iss.fail()) {
  72. if (strict) {
  73. isc_throw(BenchMarkError,
  74. "load query: unexpected input around line " <<
  75. linenum);
  76. }
  77. continue;
  78. }
  79. // We expect broken lines of data, which will be ignored with a
  80. // warning message.
  81. try {
  82. query_message.clear(Message::RENDER);
  83. query_message.setQid(0);
  84. query_message.setOpcode(Opcode::QUERY());
  85. query_message.setRcode(Rcode::NOERROR());
  86. query_message.addQuestion(Question(Name(qname_string), qclass,
  87. RRType(qtype_string)));
  88. renderer.clear();
  89. query_message.toWire(renderer);
  90. vector<unsigned char> query_data(
  91. static_cast<const unsigned char*>(buffer.getData()),
  92. static_cast<const unsigned char*>(buffer.getData()) +
  93. buffer.getLength());
  94. queries.push_back(query_data);
  95. } catch (const Exception& error) {
  96. if (strict) {
  97. isc_throw(BenchMarkError,
  98. "failed to parse/create query around line " <<
  99. linenum);
  100. }
  101. continue;
  102. }
  103. }
  104. }
  105. }
  106. }