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.
29 #define ITERATIONS 80000
35 /* Default values, can be changed on command line */
36 #define SERV_TCP_PORT 3551
37 #define SERV_HOST_ADDR "127.0.0.1"
42 void error_abort(const char *msg
)
48 int main(int argc
, char *argv
[])
52 char msg
[200], *p
, *cmd
;
55 char recvline
[MAXLINE
+1];
57 strcpy(host
, SERV_HOST_ADDR
);
61 strcpy(host
, argv
[1]); /* get host from command line */
62 p
= strchr(host
, ':');
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
);
84 if (net_send(sockfd
, "status", 6) != 6)
85 error_abort("handle_client: write error on socket");
88 while ((n
= net_recv(sockfd
, recvline
, sizeof(recvline
))) > 0) {
91 /* fputs(recvline, stdout); */
95 sprintf(msg
, "handle_client: net_recv error: %s\n", strerror(-n
));
98 if ( (i
% 100) == 0) {
99 printf("%d lines=%d\n", i
, line
);
107 if ((sockfd
= net_open(host
, NULL
, port
)) < 0) {
108 sprintf(msg
, "client: tcp_open for host %s on %d failed\n", host
, port
);
113 for (i
=0; i
<ITERATIONS
; i
++) {
114 if (net_send(sockfd
, "status", 6) != 6)
115 error_abort("handle_client: write error on socket");
118 while ((n
= net_recv(sockfd
, recvline
, sizeof(recvline
))) > 0) {
121 /* fputs(recvline, stdout); */
125 sprintf(msg
, "handle_client: net_recv error: %s\n", strerror(-n
));
128 if ( (i
% 100) == 0) {
129 printf("%d lines=%d\n", i
, line
);
136 printf("Total time = %ld secs.\n", done
- now
);
140 #else /* HAVE_NISLIB */
142 int main(int argc
, char *argv
[]) {
143 printf("Sorry, NIS code is not compiled in apcupsd.\n");
147 #endif /* HAVE_NISLIB */