xref: /freebsd/contrib/libpcap/testprogs/findalldevstest-perf.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #ifdef _WIN32
9   #include <winsock2.h>
10   #include <ws2tcpip.h>
11   #include <windows.h>
12 #else
13   #include <unistd.h>
14   #include <sys/resource.h>
15 #endif
16 
17 #include <pcap.h>
18 
19 #include "varattrs.h"
20 #include "pcap/funcattrs.h"
21 #include "portability.h"
22 
23 int main(int argc _U_, char **argv _U_)
24 {
25   pcap_if_t *alldevs;
26   int exit_status = 0;
27   char errbuf[PCAP_ERRBUF_SIZE+1];
28 #ifdef _WIN32
29   FILETIME start_ktime, start_utime, end_ktime, end_utime;
30   FILETIME dummy1, dummy2;
31   ULARGE_INTEGER start_kticks, end_kticks, start_uticks, end_uticks;
32   ULONGLONG ktime, utime, tottime;
33 #else
34   struct rusage start_rusage, end_rusage;
35   struct timeval ktime, utime, tottime;
36 #endif
37 
38 #ifdef _WIN32
39   if (!GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2,
40                        &start_ktime, &start_utime))
41   {
42     fprintf(stderr, "GetProcessTimes() fails at start\n");
43     exit(1);
44   }
45   start_kticks.LowPart = start_ktime.dwLowDateTime;
46   start_kticks.HighPart = start_ktime.dwHighDateTime;
47   start_uticks.LowPart = start_utime.dwLowDateTime;
48   start_uticks.HighPart = start_utime.dwHighDateTime;
49 #else
50   if (getrusage(RUSAGE_SELF, &start_rusage) == -1) {
51     fprintf(stderr, "getrusage() fails at start\n");
52     exit(1);
53   }
54 #endif
55   for (int i = 0; i < 500; i++)
56   {
57     if (pcap_findalldevs(&alldevs, errbuf) == -1)
58     {
59       fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
60       exit(1);
61     }
62     pcap_freealldevs(alldevs);
63   }
64 
65 #ifdef _WIN32
66   if (!GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2,
67                        &end_ktime, &end_utime))
68   {
69     fprintf(stderr, "GetProcessTimes() fails at end\n");
70     exit(1);
71   }
72   end_kticks.LowPart = end_ktime.dwLowDateTime;
73   end_kticks.HighPart = end_ktime.dwHighDateTime;
74   end_uticks.LowPart = end_utime.dwLowDateTime;
75   end_uticks.HighPart = end_utime.dwHighDateTime;
76   ktime = end_kticks.QuadPart - start_kticks.QuadPart;
77   utime = end_uticks.QuadPart - start_uticks.QuadPart;
78   tottime = ktime + utime;
79   printf("Total CPU secs: kernel %g, user %g, total %g\n",
80       ((double)ktime) / 10000000.0,
81       ((double)utime) / 10000000.0,
82       ((double)tottime) / 10000000.0);
83 #else
84   if (getrusage(RUSAGE_SELF, &end_rusage) == -1) {
85     fprintf(stderr, "getrusage() fails at end\n");
86     exit(1);
87   }
88   timersub(&end_rusage.ru_stime, &start_rusage.ru_stime, &ktime);
89   timersub(&end_rusage.ru_utime, &start_rusage.ru_utime, &utime);
90   timeradd(&ktime, &utime, &tottime);
91   printf("Total CPU secs: kernel %g, user %g, total %g\n",
92       (double)ktime.tv_sec + ((double)ktime.tv_usec / 1000000.0),
93       (double)utime.tv_sec + ((double)utime.tv_usec / 1000000.0),
94       (double)tottime.tv_sec + ((double)tottime.tv_usec / 1000000.0));
95 #endif
96   exit(exit_status);
97 }
98