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