]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
attempt at calibrated qps generator
authorbert hubert <bert.hubert@netherlabs.nl>
Sat, 31 Jan 2015 19:38:49 +0000 (20:38 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Sat, 31 Jan 2015 19:38:49 +0000 (20:38 +0100)
pdns/Makefile.am
pdns/calidns.cc [new file with mode: 0644]

index 165e82608bed8d0b588c8c4867b49401c44eb937..cab789a9947b772ab9ce3e915811d42e664129e9 100644 (file)
@@ -75,12 +75,13 @@ bin_PROGRAMS += \
        saxfr
        
 if CXX2011
-bin_PROGRAMS += dnsdist
+bin_PROGRAMS += dnsdist calidns
 endif
 
 endif
 
 EXTRA_PROGRAMS = \
+       calidns \
        dnsbulktest \
        dnsdemog \
        dnsdist \
@@ -464,6 +465,28 @@ sdig_SOURCES = \
 sdig_LDADD = $(POLARSSL_LIBS)  
 sdig_LDFLAGS=$(THREADFLAGS) 
 
+calidns_SOURCES = \
+       base32.cc \
+       base64.cc base64.hh \
+       calidns.cc \
+       dnslabeltext.cc \
+       dnsparser.cc dnsparser.hh \
+       dnsrecords.cc \
+       dnswriter.cc dnswriter.hh \
+       logger.cc \
+       misc.cc misc.hh \
+       nsecrecords.cc \
+       qtype.cc \
+       rcpgenerator.cc rcpgenerator.hh \
+       sillyrecords.cc \
+       sstuff.hh \
+       statbag.cc \
+       unix_utility.cc
+
+calidns_LDADD = $(POLARSSL_LIBS)       
+calidns_LDFLAGS=$(THREADFLAGS) 
+
+
 saxfr_SOURCES = \
        base32.cc \
        base64.cc base64.hh \
diff --git a/pdns/calidns.cc b/pdns/calidns.cc
new file mode 100644 (file)
index 0000000..5e8399c
--- /dev/null
@@ -0,0 +1,114 @@
+#include <iostream>
+#include "dnsparser.hh"
+#include "sstuff.hh"
+#include "misc.hh"
+#include "dnswriter.hh"
+#include "dnsrecords.hh"
+#include <thread>
+#include <atomic>
+#include "statbag.hh"
+#include <fstream>
+using std::thread;
+
+StatBag S;
+
+std::atomic<unsigned int> g_recvcounter;
+volatile bool g_done;
+
+void* recvThread(Socket* s)
+{
+  char response[1500];
+  while(!g_done) {
+    try {
+      s->read(response, sizeof(response));
+      g_recvcounter++;
+    }
+    catch(...){}
+  }
+  return 0;
+}
+
+
+void setSocketBuffer(int fd, int optname, uint32_t size)
+{
+  uint32_t psize=0;
+  socklen_t len=sizeof(psize);
+  
+  if(!getsockopt(fd, SOL_SOCKET, optname, (char*)&psize, &len) && psize > size) {
+    cerr<<"Not decreasing socket buffer size from "<<psize<<" to "<<size<<endl;
+    return; 
+  }
+
+  if (setsockopt(fd, SOL_SOCKET, optname, (char*)&size, sizeof(size)) < 0 )
+    cerr<<"Warning: unable to raise socket buffer size to "<<size<<": "<<strerror(errno)<<endl;
+}
+
+
+static void setSocketReceiveBuffer(int fd, uint32_t size)
+{
+  setSocketBuffer(fd, SO_RCVBUF, size);
+}
+
+static void setSocketSendBuffer(int fd, uint32_t size)
+{
+  setSocketBuffer(fd, SO_SNDBUF, size);
+}
+
+
+// calidns queryfile destination qps
+
+int main(int argc, char** argv)
+try
+{
+  ifstream ifs(argv[1]);
+  string line;
+  reportAllTypes();
+  vector<vector<uint8_t> > packets;
+  while(getline(ifs, line)) {
+    vector<uint8_t> packet;
+    boost::trim(line);
+    auto p = splitField(line, ' ');
+    DNSPacketWriter pw(packet, p.first, DNSRecordContent::TypeToNumber(p.second));
+    packets.push_back(packet);
+  }
+  cout<<"Generated "<<packets.size()<<" queries"<<endl;
+  random_shuffle(packets.begin(), packets.end());
+  
+
+  Socket sock(AF_INET, SOCK_DGRAM);
+  ComboAddress dest(argv[2]);
+  sock.connect(dest);
+  setSocketSendBuffer(sock.getHandle(), 1000000);
+  setSocketReceiveBuffer(sock.getHandle(), 1000000);
+
+
+  thread t1(recvThread, &sock);
+
+  int qps=atoi(argv[3]);
+  cout<<"Calibration run, aiming at "<<qps<< "qps"<<endl;
+  int burst=40;
+  struct timespec nsec;
+  nsec.tv_sec=0;
+  nsec.tv_nsec=(unsigned long)(burst*1000000000.0/qps);
+
+  DTime dt;
+  dt.set();
+  int count=0;
+  for(const auto& p : packets) {
+    sock.write((const char*)&p[0], p.size());
+    if(!((count++)%burst))
+       nanosleep(&nsec, 0);
+  }
+  auto udiff = dt.udiff();
+  auto realqps=packets.size()/(udiff/1000000.0);
+  cout<<"Achieved "<<realqps<<"qps"<< " over "<< udiff/1000000.0<<" seconds"<<endl;
+  
+  sleep(1);
+  
+  cout<<"Received "<<g_recvcounter.load()<<" packets"<<endl;
+  t1.detach();
+}
+ catch(std::exception& e)
+{
+  cerr<<"Fatal error: "<<e.what()<<endl;
+}