UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / examples / megaclient.c
blobb5b1b593d56f9507b9d11cab4144ddac6c12a0f8
1 /*
2 * Client test program for apcnet
4 * This program beats the living daylights out of your
5 * server by sending it one million requests.
7 * Optionally, it can send one million requests, connecting
8 * and disconnecting each time.
11 * Build it with: cc megaclient.c ../lib/libapc.a -o megaclient
13 * Execute: ./megaclient [host[:port]]
15 * For additional examples of code, see cgi/upsfetch.c
20 * If RECONNECT is defined, megaclient will disconnect
21 * and reconnect for every request (iteration), which is the normal
22 * way that apcupsd is currently accessed.
24 * If RECONNECT is not defined, a single connection
25 * is made with multiple requests.
27 #define RECONNECT 1
29 #define ITERATIONS 80000
31 #include "apc.h"
33 #ifdef HAVE_NISLIB
35 /* Default values, can be changed on command line */
36 #define SERV_TCP_PORT 3551
37 #define SERV_HOST_ADDR "127.0.0.1"
39 #define MAXLINE 5000
42 void error_abort(const char *msg)
44 fprintf(stderr, msg);
45 exit(1);
48 int main(int argc, char *argv[])
50 int sockfd, port;
51 char host[200];
52 char msg[200], *p, *cmd;
53 int i, n, line;
54 time_t now, done;
55 char recvline[MAXLINE+1];
57 strcpy(host, SERV_HOST_ADDR);
58 port = SERV_TCP_PORT;
60 if (argc > 1) {
61 strcpy(host, argv[1]); /* get host from command line */
62 p = strchr(host, ':');
63 if (p) {
64 *p++ = 0;
65 port = atoi(p);
69 if (argc > 2) {
70 cmd = argv[2];
71 } else {
72 cmd = NULL;
75 #ifdef RECONNECT
77 now = time(NULL);
78 for (i=0; i<ITERATIONS; i++) {
79 if ((sockfd = net_open(host, NULL, port)) < 0) {
80 sprintf(msg, "client: tcp_open for host %s on %d failed\n", host, port);
81 error_abort(msg);
84 if (net_send(sockfd, "status", 6) != 6)
85 error_abort("handle_client: write error on socket");
87 line = 0;
88 while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
89 recvline[n] = 0;
90 line++;
91 /* fputs(recvline, stdout); */
93 if (n < 0) {
94 char msg[200];
95 sprintf(msg, "handle_client: net_recv error: %s\n", strerror(-n));
96 error_abort(msg);
98 if ( (i % 100) == 0) {
99 printf("%d lines=%d\n", i, line);
101 net_close(sockfd);
104 #else
105 /* Open once only */
107 if ((sockfd = net_open(host, NULL, port)) < 0) {
108 sprintf(msg, "client: tcp_open for host %s on %d failed\n", host, port);
109 error_abort(msg);
112 now = time(NULL);
113 for (i=0; i<ITERATIONS; i++) {
114 if (net_send(sockfd, "status", 6) != 6)
115 error_abort("handle_client: write error on socket");
117 line = 0;
118 while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
119 recvline[n] = 0;
120 line++;
121 /* fputs(recvline, stdout); */
123 if (n < 0) {
124 char msg[200];
125 sprintf(msg, "handle_client: net_recv error: %s\n", strerror(-n));
126 error_abort(msg);
128 if ( (i % 100) == 0) {
129 printf("%d lines=%d\n", i, line);
133 net_close(sockfd);
134 #endif
135 done = time(NULL);
136 printf("Total time = %ld secs.\n", done - now);
137 exit(0);
140 #else /* HAVE_NISLIB */
142 int main(int argc, char *argv[]) {
143 printf("Sorry, NIS code is not compiled in apcupsd.\n");
144 return 1;
147 #endif /* HAVE_NISLIB */