Client benchmark.cpp

From pCT
Revision as of 21:49, 17 September 2020 by Wac002 (talk | contribs) (Created page with " <syntaxhighlight lang="cpp"> #include <errno.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <unistd.h> #include <chrono> #incl...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<syntaxhighlight lang="cpp">

  1. include <errno.h>
  2. include <netdb.h>
  3. include <stdio.h>
  4. include <stdlib.h>
  5. include <sys/socket.h>
  6. include <unistd.h>
  1. include <chrono>
  2. include <cstring>
  3. include <iostream>
  4. include <string>
  5. include <thread>

int main(int argc, char *argv[]) {

 int s;
 unsigned short port;
 struct sockaddr_in server;
 struct hostent *hp;
 char buf[4096];
 // if (argc != 3) {
 //  printf("Usage: %s <host address> <port> \n", argv[0]);
 //  exit(1);
 //}
 port = htons(atoi(argv[2]));
 /* Create a datagram socket in the internet domain and use the
  * default protocol (UDP).
  */
 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
   perror("socket()");
   exit(1);
 }
 struct timeval tv;
 tv.tv_sec = 0;
 tv.tv_usec = 2000;
 if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
   perror("Error setting timeout");
 }
 // printf(tvStart.tv_sec);
 /* Set up the server name */
 server.sin_family = AF_INET; /* Internet Domain    */
 server.sin_port = port;      /* Server Port        */
 std::string ip = "192.168.2.20";
 hp = gethostbyname(ip.c_str());
 if (hp == 0) perror("Unknown host");
 bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
 char msg[] = {0x20, 0xFF, 0xFF, 0x8};  // Send command no ack, request
 /* Send the message in buf to the server */
 unsigned int slen = sizeof(struct sockaddr_in);
 struct timeval tval;
 int rslt;
 int packNum = 65535;
 std::chrono::high_resolution_clock nowTimePoint2;
 auto statSize = 10;
 std::cout << "start\n";
 auto recvdBytes = 0;
 auto timeouts = 0;
 msg[0] = 0x60;
 if (sendto(s, msg, (sizeof(msg)), 0, (struct sockaddr *)&server,
            sizeof(server)) < 0) {
   perror("sendto()");
   exit(2);
 }
 std::this_thread::sleep_for(std::chrono::milliseconds(100));
 for (size_t l = 2480; l <= 4080; l += 100) {
   timeouts = 0;
   msg[3] = (l / 16);
   // printf("%hhX", msg[3]);
   std::cout << (l / 16) * 16 << ',';
   for (size_t i = 0; i < statSize; i++) {
     msg[0] = 0x20;
     auto start = std::chrono::time_point_cast<std::chrono::nanoseconds>(
         nowTimePoint2.now());
     if (sendto(s, msg, (sizeof(msg)), 0, (struct sockaddr *)&server,
                sizeof(server)) < 0) {
       perror("sendto()");
       exit(2);
     }
     for (size_t j = 0; j < packNum; j++) {
       recvdBytes =
           recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&server, &slen);
       if (recvdBytes <= 0) {
         // std::cout << "ERROR IN RECV\n";
         timeouts++;
       }
     }
     auto stop = std::chrono::time_point_cast<std::chrono::nanoseconds>(
         nowTimePoint2.now());
     auto duration = stop - start;
     std::cout << timeouts << ',';

std::cout << static_cast<unsigned int>(

                      (packNum * 1000.f * 1000.f * 1000.f) / duration.count() *
                      1.f)
               << ',';
     msg[0] = 0x60;
     if (sendto(s, msg, (sizeof(msg)), 0, (struct sockaddr *)&server,
                sizeof(server)) < 0) {
       perror("sendto()");
       exit(2);
     }
   }
   std::cout << '\n';
 }
 /* Deallocate the socket */
 close(s);

}

</syntaxhighlight>