Debian Codegraph @

tests/benchmarks/bench_raw.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2010 by Aris Adamantiadis
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "benchmarks.h"
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #define PYTHON_PATH "/usr/bin/python"
28
29 const char python_eater[]=
30 "#!/usr/bin/python\n"
31 "import sys\n"
32 "print 'go'\n"
33 "sys.stdout.flush()\n"
34 "toread=XXXXXXXXXX\n"
35 "read=0\n"
36 "while(read < toread):\n"
37 " buffersize=toread-read\n"
38 " if(buffersize > 4096):\n"
39 " buffersize=4096\n"
40 " r=len(sys.stdin.read(buffersize))\n"
41 " read+=r\n"
42 " if(r<=0):\n"
43 " print 'error'\n"
44 " exit()\n"
45 "print 'done'\n";
46
47 static char *get_python_eater(unsigned long bytes){
48 char *eater=malloc(sizeof(python_eater));
49 char *ptr;
50 char buf[12];
51
52 if (eater == NULL) {
53 return NULL;
54 }
55 memcpy(eater,python_eater,sizeof(python_eater));
56 ptr=strstr(eater,"XXXXXXXXXX");
57 if(!ptr){
58 free(eater);
59 return NULL;
60 }
61 sprintf(buf,"0x%.8lx",bytes);
62 memcpy(ptr,buf,10);
63 return eater;
64 }
65
66 /** @internal
67 * @brief uploads a script (python or other) at a specific path on the
68 * remote host
69 * @param[in] session an active SSH session
70 * @param[in] path to copy the file
71 * @param[in] content of the file to copy
72 * @return 0 on success, -1 on error
73 */
74 static int upload_script(ssh_session session, const char *path,
75 const char *script){
76 ssh_channel channel;
77 char cmd[128];
78 int err;
79
80 channel=ssh_channel_new(session);
81 if(!channel)
82 goto error;
83 if(ssh_channel_open_session(channel) == SSH_ERROR)
84 goto error;
85 snprintf(cmd,sizeof(cmd),"cat > %s",path);
86 if(ssh_channel_request_exec(channel,cmd) == SSH_ERROR)
87 goto error;
88 err=ssh_channel_write(channel,script,strlen(script));
89 if(err == SSH_ERROR)
90 goto error;
91 if(ssh_channel_send_eof(channel) == SSH_ERROR)
92 goto error;
93 if(ssh_channel_close(channel) == SSH_ERROR)
94 goto error;
95 ssh_channel_free(channel);
96 return 0;
97 error:
98 fprintf(stderr,"Error while copying script : %s\n",ssh_get_error(session));
99 return -1;
100 }
101
102 /** @internal
103 * @brief benchmarks a raw upload (simple upload in a SSH channel) using an
104 * existing SSH session.
105 * @param[in] session Open SSH session
106 * @param[in] args Parsed command line arguments
107 * @param[out] bps The calculated bytes per second obtained via benchmark.
108 * @return 0 on success, -1 on error.
109 */
110 int benchmarks_raw_up (ssh_session session, struct argument_s *args,
111 float *bps){
112 unsigned long bytes;
113 char *script;
114 char cmd[128];
115 int err;
116 ssh_channel channel;
117 struct timestamp_struct ts;
118 float ms=0.0;
119 unsigned long total=0;
120
121 bytes = args->datasize * 1024 * 1024;
122 script = get_python_eater(bytes);
123 if (script == NULL) {
124 return -1;
125 }
126 err=upload_script(session,"/tmp/eater.py",script);
127 free(script);
128 if(err<0)
129 return err;
130 channel=ssh_channel_new(session);
131 if(channel == NULL)
132 goto error;
133 if(ssh_channel_open_session(channel)==SSH_ERROR)
134 goto error;
135 snprintf(cmd,sizeof(cmd),"%s /tmp/eater.py", PYTHON_PATH);
136 if(ssh_channel_request_exec(channel,cmd)==SSH_ERROR)
137 goto error;
138 err = ssh_channel_read(channel, buffer, sizeof(buffer) - 1, 0);
139 if (err == SSH_ERROR)
140 goto error;
141 if (err == SSH_AGAIN) {
142 fprintf(stderr, "ssh_channel_read timeout");
143 goto error;
144 }
145 buffer[err]=0;
146 if(!strstr(buffer,"go")){
147 fprintf(stderr,"parse error : %s\n",buffer);
148 ssh_channel_close(channel);
149 ssh_channel_free(channel);
150 return -1;
151 }
152 if(args->verbose>0)
153 fprintf(stdout,"Starting upload of %lu bytes now\n",bytes);
154 timestamp_init(&ts);
155 while(total < bytes){
156 unsigned long towrite = bytes - total;
157 int w;
158 if(towrite > args->chunksize)
159 towrite = args->chunksize;
160 w=ssh_channel_write(channel,buffer,towrite);
161 if(w == SSH_ERROR)
162 goto error;
163 total += w;
164 }
165
166 if(args->verbose>0)
167 fprintf(stdout,"Finished upload, now waiting the ack\n");
168 err = ssh_channel_read(channel, buffer, 5, 0);
169 if (err == SSH_ERROR)
170 goto error;
171 if (err == SSH_AGAIN) {
172 fprintf(stderr, "ssh_channel_read timeout");
173 goto error;
174 }
175 buffer[err]=0;
176 if(!strstr(buffer,"done")){
177 fprintf(stderr,"parse error : %s\n",buffer);
178 ssh_channel_close(channel);
179 ssh_channel_free(channel);
180 return -1;
181 }
182 ms=elapsed_time(&ts);
183 *bps=8000 * (float)bytes / ms;
184 if(args->verbose > 0)
185 fprintf(stdout,"Upload took %f ms for %lu bytes, at %f bps\n",ms,
186 bytes,*bps);
187 ssh_channel_close(channel);
188 ssh_channel_free(channel);
189 return 0;
190 error:
191 fprintf(stderr,"Error during raw upload : %s\n",ssh_get_error(session));
192 if(channel){
193 ssh_channel_close(channel);
194 ssh_channel_free(channel);
195 }
196 return -1;
197 }
198
199 const char python_giver[] =
200 "#!/usr/bin/python\n"
201 "import sys\n"
202 "r=sys.stdin.read(2)\n"
203 "towrite=XXXXXXXXXX\n"
204 "wrote=0\n"
205 "mtu = 32786\n"
206 "buf = 'A'*mtu\n"
207 "while(wrote < towrite):\n"
208 " buffersize=towrite-wrote\n"
209 " if(buffersize > mtu):\n"
210 " buffersize=mtu\n"
211 " if(buffersize == mtu):\n"
212 " sys.stdout.write(buf)\n"
213 " else:\n"
214 " sys.stdout.write('A'*buffersize)\n"
215 " wrote+=buffersize\n"
216 "sys.stdout.flush()\n";
217
218 static char *get_python_giver(unsigned long bytes){
219 char *giver=malloc(sizeof(python_giver));
220 char *ptr;
221 char buf[12];
222
223 if (giver == NULL) {
224 return NULL;
225 }
226 memcpy(giver,python_giver,sizeof(python_giver));
227 ptr=strstr(giver,"XXXXXXXXXX");
228 if(!ptr){
229 free(giver);
230 return NULL;
231 }
232 sprintf(buf,"0x%.8lx",bytes);
233 memcpy(ptr,buf,10);
234 return giver;
235 }
236
237 /** @internal
238 * @brief benchmarks a raw download (simple upload in a SSH channel) using an
239 * existing SSH session.
240 * @param[in] session Open SSH session
241 * @param[in] args Parsed command line arguments
242 * @param[out] bps The calculated bytes per second obtained via benchmark.
243 * @return 0 on success, -1 on error.
244 */
245 int benchmarks_raw_down (ssh_session session, struct argument_s *args,
246 float *bps){
247 unsigned long bytes;
248 char *script;
249 char cmd[128];
250 int err;
251 ssh_channel channel;
252 struct timestamp_struct ts;
253 float ms=0.0;
254 unsigned long total=0;
255
256 bytes = args->datasize * 1024 * 1024;
257 script = get_python_giver(bytes);
258 if (script == NULL) {
259 return -1;
260 }
261 err=upload_script(session,"/tmp/giver.py",script);
262 free(script);
263 if(err<0)
264 return err;
265 channel=ssh_channel_new(session);
266 if(channel == NULL)
267 goto error;
268 if(ssh_channel_open_session(channel)==SSH_ERROR)
269 goto error;
270 snprintf(cmd,sizeof(cmd),"%s /tmp/giver.py", PYTHON_PATH);
271 if(ssh_channel_request_exec(channel,cmd)==SSH_ERROR)
272 goto error;
273 if((err=ssh_channel_write(channel,"go",2))==SSH_ERROR)
274 goto error;
275 if(args->verbose>0)
276 fprintf(stdout,"Starting download of %lu bytes now\n",bytes);
277 timestamp_init(&ts);
278 while(total < bytes){
279 unsigned long toread = bytes - total;
280 int r;
281 if(toread > args->chunksize)
282 toread = args->chunksize;
283 r=ssh_channel_read(channel,buffer,toread,0);
284 if (r == SSH_ERROR)
285 goto error;
286 if (r == SSH_AGAIN) {
287 fprintf(stderr, "ssh_channel_read timeout");
288 goto error;
289 }
290 total += r;
291 }
292
293 if(args->verbose>0)
294 fprintf(stdout,"Finished download\n");
295 ms=elapsed_time(&ts);
296 *bps=8000 * (float)bytes / ms;
297 if(args->verbose > 0)
298 fprintf(stdout,"Download took %f ms for %lu bytes, at %f bps\n",ms,
299 bytes,*bps);
300 ssh_channel_close(channel);
301 ssh_channel_free(channel);
302 return 0;
303 error:
304 fprintf(stderr,"Error during raw upload : %s\n",ssh_get_error(session));
305 if(channel){
306 ssh_channel_close(channel);
307 ssh_channel_free(channel);
308 }
309 return -1;
310 }