]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnstcpbench.cc
auth: Wrap pthread_ objects
[thirdparty/pdns.git] / pdns / dnstcpbench.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <boost/accumulators/statistics/median.hpp>
26 #include <boost/accumulators/statistics/mean.hpp>
27 #include <boost/accumulators/accumulators.hpp>
28
29 #include <boost/accumulators/statistics.hpp>
30
31 #include <thread>
32
33 #include "dnsparser.hh"
34 #include "sstuff.hh"
35 #include "misc.hh"
36 #include "dnswriter.hh"
37 #include "dnsrecords.hh"
38 #include "statbag.hh"
39 #include "threadname.hh"
40 #include <netinet/tcp.h>
41 #include <boost/array.hpp>
42 #include <boost/program_options.hpp>
43
44
45 StatBag S;
46 namespace po = boost::program_options;
47
48 po::variables_map g_vm;
49 bool g_verbose;
50 bool g_onlyTCP;
51 bool g_tcpNoDelay;
52 unsigned int g_timeoutMsec;
53 AtomicCounter g_networkErrors, g_otherErrors, g_OK, g_truncates, g_authAnswers, g_timeOuts;
54 ComboAddress g_dest;
55
56 static unsigned int makeUsec(const struct timeval& tv)
57 {
58 return 1000000*tv.tv_sec + tv.tv_usec;
59 }
60
61 /* On Linux, run echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
62 to prevent running out of free TCP ports */
63
64 struct BenchQuery
65 {
66 BenchQuery(const std::string& qname_, uint16_t qtype_) : qname(qname_), qtype(qtype_), udpUsec(0), tcpUsec(0), answerSecond(0) {}
67 BenchQuery(): qtype(0), udpUsec(0), tcpUsec(0), answerSecond(0) {}
68 DNSName qname;
69 uint16_t qtype;
70 uint32_t udpUsec, tcpUsec;
71 time_t answerSecond;
72 };
73
74 static void doQuery(BenchQuery* q)
75 try
76 {
77 vector<uint8_t> packet;
78 DNSPacketWriter pw(packet, q->qname, q->qtype);
79 int res;
80 string reply;
81
82 struct timeval tv, now;
83 gettimeofday(&tv, 0);
84
85 if(!g_onlyTCP) {
86 Socket udpsock(g_dest.sin4.sin_family, SOCK_DGRAM);
87
88 udpsock.sendTo(string(packet.begin(), packet.end()), g_dest);
89 ComboAddress origin;
90 res = waitForData(udpsock.getHandle(), 0, 1000 * g_timeoutMsec);
91 if(res < 0)
92 throw NetworkError("Error waiting for response");
93 if(!res) {
94 g_timeOuts++;
95 return;
96 }
97
98 udpsock.recvFrom(reply, origin);
99
100 gettimeofday(&now, 0);
101 q->udpUsec = makeUsec(now - tv);
102 tv=now;
103
104 MOADNSParser mdp(false, reply);
105 if(!mdp.d_header.tc)
106 return;
107 g_truncates++;
108 }
109
110 Socket sock(g_dest.sin4.sin_family, SOCK_STREAM);
111 int tmp=1;
112 if(setsockopt(sock.getHandle(),SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0)
113 throw runtime_error("Unable to set socket reuse: "+stringerror());
114
115 if(g_tcpNoDelay && setsockopt(sock.getHandle(), IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof tmp)<0)
116 throw runtime_error("Unable to set socket no delay: "+stringerror());
117
118 sock.connect(g_dest);
119 uint16_t len = htons(packet.size());
120 string tcppacket((char*)& len, 2);
121 tcppacket.append(packet.begin(), packet.end());
122
123 sock.writen(tcppacket);
124
125 res = waitForData(sock.getHandle(), 0, 1000 * g_timeoutMsec);
126 if(res < 0)
127 throw NetworkError("Error waiting for response");
128 if(!res) {
129 g_timeOuts++;
130 return;
131 }
132
133 if(sock.read((char *) &len, 2) != 2)
134 throw PDNSException("tcp read failed");
135
136 len=ntohs(len);
137 std::unique_ptr<char[]> creply(new char[len]);
138 int n=0;
139 int numread;
140 while(n<len) {
141 numread=sock.read(creply.get()+n, len-n);
142 if(numread<0)
143 throw PDNSException("tcp read failed");
144 n+=numread;
145 }
146
147 reply=string(creply.get(), len);
148
149 gettimeofday(&now, 0);
150 q->tcpUsec = makeUsec(now - tv);
151 q->answerSecond = now.tv_sec;
152
153 MOADNSParser mdp(false, reply);
154 // cout<<"Had correct TCP/IP response, "<<mdp.d_answers.size()<<" answers, aabit="<<mdp.d_header.aa<<endl;
155 if(mdp.d_header.aa)
156 g_authAnswers++;
157 g_OK++;
158 }
159 catch(NetworkError& ne)
160 {
161 cerr<<"Network error: "<<ne.what()<<endl;
162 g_networkErrors++;
163 }
164 catch(...)
165 {
166 g_otherErrors++;
167 }
168
169 /* read queries from stdin, put in vector
170 launch n worker threads, each picks a query using AtomicCounter
171 If a worker reaches the end of its queue, it stops */
172
173 AtomicCounter g_pos;
174
175 vector<BenchQuery> g_queries;
176
177 static void worker()
178 {
179 setThreadName("dnstcpb/worker");
180 for(;;) {
181 unsigned int pos = g_pos++;
182 if(pos >= g_queries.size())
183 break;
184
185 doQuery(&g_queries[pos]); // this is safe as long as nobody *inserts* to g_queries
186 }
187 }
188
189 static void usage(po::options_description &desc) {
190 cerr<<"Syntax: dnstcpbench REMOTE [PORT] < QUERIES"<<endl;
191 cerr<<"Where QUERIES is one query per line, format: qname qtype, just 1 space"<<endl;
192 cerr<<desc<<endl;
193 }
194
195 int main(int argc, char** argv)
196 try
197 {
198 po::options_description desc("Allowed options"), hidden, alloptions;
199 desc.add_options()
200 ("help,h", "produce help message")
201 ("version", "print version number")
202 ("verbose,v", "be verbose")
203 ("udp-first,u", "try UDP first")
204 ("file,f", po::value<string>(), "source file - if not specified, defaults to stdin")
205 ("tcp-no-delay", po::value<bool>()->default_value(true), "use TCP_NODELAY socket option")
206 ("timeout-msec", po::value<int>()->default_value(10), "wait for this amount of milliseconds for an answer")
207 ("workers", po::value<int>()->default_value(100), "number of parallel workers");
208
209 hidden.add_options()
210 ("remote-host", po::value<string>(), "remote-host")
211 ("remote-port", po::value<int>()->default_value(53), "remote-port");
212 alloptions.add(desc).add(hidden);
213
214 po::positional_options_description p;
215 p.add("remote-host", 1);
216 p.add("remote-port", 1);
217
218 po::store(po::command_line_parser(argc, argv).options(alloptions).positional(p).run(), g_vm);
219 po::notify(g_vm);
220
221 if(g_vm.count("version")) {
222 cerr<<"dnstcpbench "<<VERSION<<endl;
223 exit(EXIT_SUCCESS);
224 }
225
226 if(g_vm.count("help")) {
227 usage(desc);
228 exit(EXIT_SUCCESS);
229 }
230 g_tcpNoDelay = g_vm["tcp-no-delay"].as<bool>();
231
232 g_onlyTCP = !g_vm.count("udp-first");
233 g_verbose = g_vm.count("verbose");
234 g_timeoutMsec = g_vm["timeout-msec"].as<int>();
235
236 reportAllTypes();
237
238 if(g_vm["remote-host"].empty()) {
239 usage(desc);
240 exit(EXIT_FAILURE);
241 }
242
243 g_dest = ComboAddress(g_vm["remote-host"].as<string>().c_str(), g_vm["remote-port"].as<int>());
244
245 unsigned int numworkers=g_vm["workers"].as<int>();
246
247 if(g_verbose) {
248 cout<<"Sending queries to: "<<g_dest.toStringWithPort()<<endl;
249 cout<<"Attempting UDP first: " << (g_onlyTCP ? "no" : "yes") <<endl;
250 cout<<"Timeout: "<< g_timeoutMsec<<"msec"<<endl;
251 cout << "Using TCP_NODELAY: "<<g_tcpNoDelay<<endl;
252 }
253
254
255 std::vector<std::thread> workers;
256 workers.reserve(numworkers);
257
258 FILE* fp;
259 if(!g_vm.count("file"))
260 fp=fdopen(0, "r");
261 else {
262 fp=fopen(g_vm["file"].as<string>().c_str(), "r");
263 if(!fp)
264 unixDie("Unable to open "+g_vm["file"].as<string>()+" for input");
265 }
266 pair<string, string> q;
267 string line;
268 while(stringfgets(fp, line)) {
269 trim_right(line);
270 q=splitField(line, ' ');
271 g_queries.push_back(BenchQuery(q.first, DNSRecordContent::TypeToNumber(q.second)));
272 }
273 fclose(fp);
274
275 for (unsigned int n = 0; n < numworkers; ++n) {
276 workers.push_back(std::thread(worker));
277 }
278 for (auto& w : workers) {
279 w.join();
280 }
281
282 using namespace boost::accumulators;
283 typedef accumulator_set<
284 double
285 , stats<boost::accumulators::tag::median(with_p_square_quantile),
286 boost::accumulators::tag::mean(immediate)
287 >
288 > acc_t;
289
290 acc_t udpspeeds, tcpspeeds, qps;
291
292 typedef map<time_t, uint32_t> counts_t;
293 counts_t counts;
294
295 for(const BenchQuery& bq : g_queries) {
296 counts[bq.answerSecond]++;
297 udpspeeds(bq.udpUsec);
298 tcpspeeds(bq.tcpUsec);
299 }
300
301 for(const counts_t::value_type& val : counts) {
302 qps(val.second);
303 }
304
305 cout<<"Average qps: "<<mean(qps)<<", median qps: "<<median(qps)<<endl;
306 cout<<"Average UDP latency: "<<mean(udpspeeds)<<"usec, median: "<<median(udpspeeds)<<"usec"<<endl;
307 cout<<"Average TCP latency: "<<mean(tcpspeeds)<<"usec, median: "<<median(tcpspeeds)<<"usec"<<endl;
308
309 cout<<"OK: "<<g_OK<<", network errors: "<<g_networkErrors<<", other errors: "<<g_otherErrors<<endl;
310 cout<<"Timeouts: "<<g_timeOuts<<endl;
311 cout<<"Truncateds: "<<g_truncates<<", auth answers: "<<g_authAnswers<<endl;
312 }
313 catch(std::exception &e)
314 {
315 cerr<<"Fatal: "<<e.what()<<endl;
316 }