]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnstcpbench.cc
auth: switch circleci mssql image
[thirdparty/pdns.git] / pdns / dnstcpbench.cc
CommitLineData
22f4bd59 1/*
12471842
PL
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 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
4106b16e 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
a51b52c9 31#include "dnsparser.hh"
32#include "sstuff.hh"
33#include "misc.hh"
34#include "dnswriter.hh"
35#include "dnsrecords.hh"
36#include "statbag.hh"
519f5484 37#include "threadname.hh"
a8f90a6a 38#include <netinet/tcp.h>
a51b52c9 39#include <boost/array.hpp>
36658df5 40#include <boost/program_options.hpp>
fa8fd4d2 41
a51b52c9 42
36658df5 43StatBag S;
44namespace po = boost::program_options;
4106b16e 45
36658df5 46po::variables_map g_vm;
47bool g_verbose;
a51b52c9 48bool g_onlyTCP;
a8f90a6a 49bool g_tcpNoDelay;
36658df5 50unsigned int g_timeoutMsec;
51AtomicCounter g_networkErrors, g_otherErrors, g_OK, g_truncates, g_authAnswers, g_timeOuts;
1d74012c 52ComboAddress g_dest;
f7896b52 53
4106b16e 54unsigned int makeUsec(const struct timeval& tv)
55{
56 return 1000000*tv.tv_sec + tv.tv_usec;
57}
58
22f4bd59 59/* On Linux, run echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
60 to prevent running out of free TCP ports */
61
62struct BenchQuery
63{
4106b16e 64 BenchQuery(const std::string& qname_, uint16_t qtype_) : qname(qname_), qtype(qtype_), udpUsec(0), tcpUsec(0), answerSecond(0) {}
67252448 65 BenchQuery(): qtype(0), udpUsec(0), tcpUsec(0), answerSecond(0) {}
eaedd091 66 DNSName qname;
22f4bd59 67 uint16_t qtype;
4106b16e 68 uint32_t udpUsec, tcpUsec;
69 time_t answerSecond;
22f4bd59 70};
f7896b52 71
22f4bd59 72void doQuery(BenchQuery* q)
f7896b52 73try
a51b52c9 74{
75 vector<uint8_t> packet;
1d74012c 76 DNSPacketWriter pw(packet, q->qname, q->qtype);
36658df5 77 int res;
a51b52c9 78 string reply;
79
4106b16e 80 struct timeval tv, now;
81 gettimeofday(&tv, 0);
82
a51b52c9 83 if(!g_onlyTCP) {
528b753d 84 Socket udpsock(g_dest.sin4.sin_family, SOCK_DGRAM);
a51b52c9 85
16657041 86 udpsock.sendTo(string(packet.begin(), packet.end()), g_dest);
a51b52c9 87 ComboAddress origin;
36658df5 88 res = waitForData(udpsock.getHandle(), 0, 1000 * g_timeoutMsec);
89 if(res < 0)
90 throw NetworkError("Error waiting for response");
91 if(!res) {
92 g_timeOuts++;
93 return;
94 }
95
a51b52c9 96 udpsock.recvFrom(reply, origin);
4106b16e 97
98 gettimeofday(&now, 0);
99 q->udpUsec = makeUsec(now - tv);
100 tv=now;
101
27c0050c 102 MOADNSParser mdp(false, reply);
a51b52c9 103 if(!mdp.d_header.tc)
104 return;
f7896b52 105 g_truncates++;
a51b52c9 106 }
107
528b753d 108 Socket sock(g_dest.sin4.sin_family, SOCK_STREAM);
f7896b52 109 int tmp=1;
110 if(setsockopt(sock.getHandle(),SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0)
111 throw runtime_error("Unable to set socket reuse: "+string(strerror(errno)));
a8f90a6a 112
113 if(g_tcpNoDelay && setsockopt(sock.getHandle(), IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof tmp)<0)
114 throw runtime_error("Unable to set socket no delay: "+string(strerror(errno)));
a51b52c9 115
1d74012c 116 sock.connect(g_dest);
36658df5 117 uint16_t len = htons(packet.size());
118 string tcppacket((char*)& len, 2);
16657041 119 tcppacket.append(packet.begin(), packet.end());
36658df5 120
121 sock.writen(tcppacket);
122
123 res = waitForData(sock.getHandle(), 0, 1000 * g_timeoutMsec);
124 if(res < 0)
125 throw NetworkError("Error waiting for response");
126 if(!res) {
127 g_timeOuts++;
128 return;
129 }
a51b52c9 130
131 if(sock.read((char *) &len, 2) != 2)
3f81d239 132 throw PDNSException("tcp read failed");
a51b52c9 133
134 len=ntohs(len);
135 char *creply = new char[len];
136 int n=0;
137 int numread;
138 while(n<len) {
139 numread=sock.read(creply+n, len-n);
140 if(numread<0)
3f81d239 141 throw PDNSException("tcp read failed");
a51b52c9 142 n+=numread;
143 }
144
145 reply=string(creply, len);
146 delete[] creply;
147
4106b16e 148 gettimeofday(&now, 0);
149 q->tcpUsec = makeUsec(now - tv);
150 q->answerSecond = now.tv_sec;
151
27c0050c 152 MOADNSParser mdp(false, reply);
f7896b52 153 // cout<<"Had correct TCP/IP response, "<<mdp.d_answers.size()<<" answers, aabit="<<mdp.d_header.aa<<endl;
36658df5 154 if(mdp.d_header.aa)
155 g_authAnswers++;
f7896b52 156 g_OK++;
157}
158catch(NetworkError& ne)
159{
160 cerr<<"Network error: "<<ne.what()<<endl;
161 g_networkErrors++;
162}
163catch(...)
164{
165 g_otherErrors++;
a51b52c9 166}
167
168/* read queries from stdin, put in vector
169 launch n worker threads, each picks a query using AtomicCounter
170 If a worker reaches the end of its queue, it stops */
171
172AtomicCounter g_pos;
682549f2 173
22f4bd59 174vector<BenchQuery> g_queries;
1d74012c 175
682549f2 176static void* worker(void*)
a51b52c9 177{
519f5484 178 setThreadName("dnstcpb/worker");
a51b52c9 179 for(;;) {
f7896b52 180 unsigned int pos = g_pos++;
181 if(pos >= g_queries.size())
a51b52c9 182 break;
1d74012c 183
184 doQuery(&g_queries[pos]); // this is safe as long as nobody *inserts* to g_queries
a51b52c9 185 }
186 return 0;
187}
188
c4f20ff9
PL
189void 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
a51b52c9 195int main(int argc, char** argv)
196try
197{
36658df5 198 po::options_description desc("Allowed options"), hidden, alloptions;
199 desc.add_options()
200 ("help,h", "produce help message")
c4f20ff9 201 ("version", "print version number")
36658df5 202 ("verbose,v", "be verbose")
203 ("udp-first,u", "try UDP first")
74f463af 204 ("file,f", po::value<string>(), "source file - if not specified, defaults to stdin")
a8f90a6a 205 ("tcp-no-delay", po::value<bool>()->default_value(true), "use TCP_NODELAY socket option")
36658df5 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);
c4f20ff9
PL
220
221 if(g_vm.count("version")) {
222 cerr<<"dnstcpbench "<<VERSION<<endl;
223 exit(EXIT_SUCCESS);
224 }
225
36658df5 226 if(g_vm.count("help")) {
c4f20ff9 227 usage(desc);
36658df5 228 exit(EXIT_SUCCESS);
229 }
a8f90a6a 230 g_tcpNoDelay = g_vm["tcp-no-delay"].as<bool>();
231
36658df5 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
a51b52c9 236 reportAllTypes();
f7896b52 237
36658df5 238 if(g_vm["remote-host"].empty()) {
c4f20ff9 239 usage(desc);
f7896b52 240 exit(EXIT_FAILURE);
241 }
f7896b52 242
36658df5 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;
a8f90a6a 251 cout << "Using TCP_NODELAY: "<<g_tcpNoDelay<<endl;
36658df5 252 }
253
254
2c001eb6 255 std::vector<pthread_t> workers(numworkers);
a51b52c9 256
74f463af 257 FILE* fp;
258 if(!g_vm.count("file"))
259 fp=fdopen(0, "r");
260 else {
261 fp=fopen(g_vm["file"].as<string>().c_str(), "r");
262 if(!fp)
263 unixDie("Unable to open "+g_vm["file"].as<string>()+" for input");
264 }
f7896b52 265 pair<string, string> q;
266 string line;
267 while(stringfgets(fp, line)) {
268 trim_right(line);
269 q=splitField(line, ' ');
22f4bd59 270 g_queries.push_back(BenchQuery(q.first, DNSRecordContent::TypeToNumber(q.second)));
a51b52c9 271 }
f7896b52 272 fclose(fp);
273
a51b52c9 274 for(unsigned int n = 0; n < numworkers; ++n) {
275 pthread_create(&workers[n], 0, worker, 0);
276 }
277 for(unsigned int n = 0; n < numworkers; ++n) {
278 void* status;
279 pthread_join(workers[n], &status);
280 }
4106b16e 281
282 using namespace boost::accumulators;
283 typedef accumulator_set<
40cd3e09 284 double
4106b16e 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
ef7cd021 295 for(const BenchQuery& bq : g_queries) {
4106b16e 296 counts[bq.answerSecond]++;
297 udpspeeds(bq.udpUsec);
298 tcpspeeds(bq.tcpUsec);
299 }
300
ef7cd021 301 for(const counts_t::value_type& val : counts) {
4106b16e 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
f7896b52 309 cout<<"OK: "<<g_OK<<", network errors: "<<g_networkErrors<<", other errors: "<<g_otherErrors<<endl;
36658df5 310 cout<<"Timeouts: "<<g_timeOuts<<endl;
311 cout<<"Truncateds: "<<g_truncates<<", auth answers: "<<g_authAnswers<<endl;
a51b52c9 312}
313catch(std::exception &e)
314{
315 cerr<<"Fatal: "<<e.what()<<endl;
316}