spray.c revision 1.2 1 1.1 jtc /*
2 1.1 jtc * Copyright (c) 1993 Winning Strategies, Inc.
3 1.1 jtc * All rights reserved.
4 1.1 jtc *
5 1.1 jtc * Redistribution and use in source and binary forms, with or without
6 1.1 jtc * modification, are permitted provided that the following conditions
7 1.1 jtc * are met:
8 1.1 jtc * 1. Redistributions of source code must retain the above copyright
9 1.1 jtc * notice, this list of conditions and the following disclaimer.
10 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 jtc * notice, this list of conditions and the following disclaimer in the
12 1.1 jtc * documentation and/or other materials provided with the distribution.
13 1.1 jtc * 3. All advertising materials mentioning features or use of this software
14 1.1 jtc * must display the following acknowledgement:
15 1.1 jtc * This product includes software developed by Winning Strategies, Inc.
16 1.1 jtc * 4. The name of the author may not be used to endorse or promote products
17 1.2 jtc * derived from this software without specific prior written permission
18 1.1 jtc *
19 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 jtc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 jtc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 jtc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 jtc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 jtc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 jtc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 jtc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 jtc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 jtc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 jtc *
30 1.2 jtc * $Id: spray.c,v 1.2 1994/01/29 01:43:41 jtc Exp $
31 1.1 jtc */
32 1.1 jtc
33 1.1 jtc #include <stdio.h>
34 1.1 jtc #include <stdlib.h>
35 1.1 jtc #include <unistd.h>
36 1.1 jtc
37 1.1 jtc #include <rpc/rpc.h>
38 1.1 jtc #include <rpcsvc/spray.h>
39 1.1 jtc
40 1.1 jtc #ifndef SPRAYOVERHEAD
41 1.1 jtc #define SPRAYOVERHEAD 86
42 1.1 jtc #endif
43 1.1 jtc
44 1.1 jtc void usage ();
45 1.1 jtc void print_xferstats ();
46 1.1 jtc
47 1.1 jtc /* spray buffer */
48 1.1 jtc char spray_buffer[SPRAYMAX];
49 1.1 jtc
50 1.1 jtc /* RPC timeouts */
51 1.1 jtc struct timeval NO_DEFAULT = { -1, -1 };
52 1.1 jtc struct timeval ONE_WAY = { 0, 0 };
53 1.1 jtc struct timeval TIMEOUT = { 25, 0 };
54 1.1 jtc
55 1.1 jtc int
56 1.1 jtc main(argc, argv)
57 1.1 jtc int argc;
58 1.1 jtc char **argv;
59 1.1 jtc {
60 1.1 jtc char *progname;
61 1.1 jtc spraycumul host_stats;
62 1.1 jtc sprayarr host_array;
63 1.1 jtc CLIENT *cl;
64 1.1 jtc int c;
65 1.1 jtc int i;
66 1.1 jtc int count = 0;
67 1.1 jtc int delay = 0;
68 1.1 jtc int length = 0;
69 1.1 jtc double xmit_time; /* time to receive data */
70 1.1 jtc
71 1.1 jtc progname = *argv;
72 1.1 jtc while ((c = getopt(argc, argv, "c:d:l:")) != -1) {
73 1.1 jtc switch (c) {
74 1.1 jtc case 'c':
75 1.1 jtc count = atoi(optarg);
76 1.1 jtc break;
77 1.1 jtc case 'd':
78 1.1 jtc delay = atoi(optarg);
79 1.1 jtc break;
80 1.1 jtc case 'l':
81 1.1 jtc length = atoi(optarg);
82 1.1 jtc break;
83 1.1 jtc default:
84 1.1 jtc usage();
85 1.1 jtc /* NOTREACHED */
86 1.1 jtc }
87 1.1 jtc }
88 1.1 jtc argc -= optind;
89 1.1 jtc argv += optind;
90 1.1 jtc
91 1.1 jtc if (argc != 1) {
92 1.1 jtc usage();
93 1.1 jtc /* NOTREACHED */
94 1.1 jtc }
95 1.1 jtc
96 1.1 jtc
97 1.1 jtc /* Correct packet length. */
98 1.1 jtc if (length > SPRAYMAX) {
99 1.1 jtc length = SPRAYMAX;
100 1.1 jtc } else if (length < SPRAYOVERHEAD) {
101 1.1 jtc length = SPRAYOVERHEAD;
102 1.1 jtc } else {
103 1.1 jtc /* The RPC portion of the packet is a multiple of 32 bits. */
104 1.1 jtc length -= SPRAYOVERHEAD - 3;
105 1.1 jtc length &= ~3;
106 1.1 jtc length += SPRAYOVERHEAD;
107 1.1 jtc }
108 1.1 jtc
109 1.1 jtc
110 1.1 jtc /*
111 1.1 jtc * The default value of count is the number of packets required
112 1.1 jtc * to make the total stream size 100000 bytes.
113 1.1 jtc */
114 1.1 jtc if (!count) {
115 1.1 jtc count = 100000 / length;
116 1.1 jtc }
117 1.1 jtc
118 1.1 jtc /* Initialize spray argument */
119 1.1 jtc host_array.sprayarr_len = length - SPRAYOVERHEAD;
120 1.1 jtc host_array.sprayarr_val = spray_buffer;
121 1.1 jtc
122 1.1 jtc
123 1.1 jtc /* create connection with server */
124 1.1 jtc cl = clnt_create(*argv, SPRAYPROG, SPRAYVERS, "udp");
125 1.1 jtc if (cl == NULL) {
126 1.1 jtc clnt_pcreateerror(progname);
127 1.1 jtc exit(1);
128 1.1 jtc }
129 1.1 jtc
130 1.1 jtc
131 1.1 jtc /*
132 1.1 jtc * For some strange reason, RPC 4.0 sets the default timeout,
133 1.1 jtc * thus timeouts specified in clnt_call() are always ignored.
134 1.1 jtc *
135 1.1 jtc * The following (undocumented) hack resets the internal state
136 1.1 jtc * of the client handle.
137 1.1 jtc */
138 1.1 jtc clnt_control(cl, CLSET_TIMEOUT, &NO_DEFAULT);
139 1.1 jtc
140 1.1 jtc
141 1.1 jtc /* Clear server statistics */
142 1.1 jtc if (clnt_call(cl, SPRAYPROC_CLEAR, xdr_void, NULL, xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
143 1.1 jtc clnt_perror(cl, progname);
144 1.1 jtc exit(1);
145 1.1 jtc }
146 1.1 jtc
147 1.1 jtc
148 1.1 jtc /* Spray server with packets */
149 1.1 jtc printf ("sending %d packets of lnth %d to %s ...", count, length, *argv);
150 1.1 jtc fflush (stdout);
151 1.1 jtc
152 1.1 jtc for (i = 0; i < count; i++) {
153 1.1 jtc clnt_call(cl, SPRAYPROC_SPRAY, xdr_sprayarr, &host_array, xdr_void, NULL, ONE_WAY);
154 1.1 jtc
155 1.1 jtc if (delay) {
156 1.1 jtc usleep(delay);
157 1.1 jtc }
158 1.1 jtc }
159 1.1 jtc
160 1.1 jtc
161 1.1 jtc /* Collect statistics from server */
162 1.1 jtc if (clnt_call(cl, SPRAYPROC_GET, xdr_void, NULL, xdr_spraycumul, &host_stats, TIMEOUT) != RPC_SUCCESS) {
163 1.1 jtc clnt_perror(cl, progname);
164 1.1 jtc exit(1);
165 1.1 jtc }
166 1.1 jtc
167 1.1 jtc xmit_time = host_stats.clock.sec +
168 1.1 jtc (host_stats.clock.usec / 1000000.0);
169 1.1 jtc
170 1.1 jtc printf ("\n\tin %.2f seconds elapsed time\n", xmit_time);
171 1.1 jtc
172 1.1 jtc
173 1.1 jtc /* report dropped packets */
174 1.1 jtc if (host_stats.counter != count) {
175 1.1 jtc int packets_dropped = count - host_stats.counter;
176 1.1 jtc
177 1.1 jtc printf("\t%d packets (%.2f%%) dropped\n",
178 1.1 jtc packets_dropped,
179 1.1 jtc 100.0 * packets_dropped / count );
180 1.1 jtc } else {
181 1.1 jtc printf("\tno packets dropped\n");
182 1.1 jtc }
183 1.1 jtc
184 1.1 jtc printf("Sent:");
185 1.1 jtc print_xferstats(count, length, xmit_time);
186 1.1 jtc
187 1.1 jtc printf("Rcvd:");
188 1.1 jtc print_xferstats(host_stats.counter, length, xmit_time);
189 1.1 jtc
190 1.1 jtc exit (0);
191 1.1 jtc }
192 1.1 jtc
193 1.1 jtc
194 1.1 jtc void
195 1.1 jtc print_xferstats(packets, packetlen, xfertime)
196 1.1 jtc int packets;
197 1.1 jtc int packetlen;
198 1.1 jtc double xfertime;
199 1.1 jtc {
200 1.1 jtc int datalen;
201 1.1 jtc double pps; /* packets per second */
202 1.1 jtc double bps; /* bytes per second */
203 1.1 jtc
204 1.1 jtc datalen = packets * packetlen;
205 1.1 jtc pps = packets / xfertime;
206 1.1 jtc bps = datalen / xfertime;
207 1.1 jtc
208 1.1 jtc printf("\t%.0f packets/sec, ", pps);
209 1.1 jtc
210 1.1 jtc if (bps >= 1024)
211 1.1 jtc printf ("%.1fK ", bps / 1024);
212 1.1 jtc else
213 1.1 jtc printf ("%.0f ", bps);
214 1.1 jtc
215 1.1 jtc printf("bytes/sec\n");
216 1.1 jtc }
217 1.1 jtc
218 1.1 jtc
219 1.1 jtc void
220 1.1 jtc usage ()
221 1.1 jtc {
222 1.1 jtc fprintf(stderr, "usage: spray [-c count] [-l length] [-d delay] host\n");
223 1.1 jtc exit(1);
224 1.1 jtc }
225