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