Debian Codegraph @

tests/benchmarks/benchmarks.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 #define LIBSSH_STATIC
23
24 #include "config.h"
25 #include "benchmarks.h"
26 #include <libssh/libssh.h>
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 struct benchmark benchmarks[] = {
33 {
34 .name = "benchmark_raw_upload",
35 .fct = benchmarks_raw_up,
36 .enabled = 0
37 },
38 {
39 .name = "benchmark_raw_download",
40 .fct = benchmarks_raw_down,
41 .enabled = 0
42 },
43 {
44 .name = "benchmark_scp_upload",
45 .fct = benchmarks_scp_up,
46 .enabled = 0
47 },
48 {
49 .name = "benchmark_scp_download",
50 .fct = benchmarks_scp_down,
51 .enabled = 0
52 },
53 #ifdef WITH_SFTP
54 {
55 .name = "benchmark_sync_sftp_upload",
56 .fct = benchmarks_sync_sftp_up,
57 .enabled = 0
58 },
59 {
60 .name = "benchmark_sync_sftp_download",
61 .fct = benchmarks_sync_sftp_down,
62 .enabled = 0
63 },
64 {
65 .name="benchmark_async_sftp_download",
66 .fct=benchmarks_async_sftp_down,
67 .enabled=0
68 },
69 {
70 .name = "benchmark_async_sftp_aio_download",
71 .fct = benchmarks_async_sftp_aio_down,
72 .enabled = 0
73 },
74 {
75 .name = "benchmark_async_sftp_aio_upload",
76 .fct = benchmarks_async_sftp_aio_up,
77 .enabled = 0
78 }
79 #endif /* WITH_SFTP */
80 };
81
82 #ifdef HAVE_ARGP_H
83 #include <argp.h>
84
85 const char *argp_program_version = "libssh benchmarks 2011-08-28";
86 const char *argp_program_bug_address = "Aris Adamantiadis <[email protected]>";
87
88 static char **cmdline = NULL;
89
90 /* Program documentation. */
91 static char doc[] = "libssh benchmarks";
92
93
94 /* The options we understand. */
95 static struct argp_option options[] = {
96 {
97 .name = "verbose",
98 .key = 'v',
99 .arg = NULL,
100 .flags = 0,
101 .doc = "Make libssh benchmark more verbose",
102 .group = 0
103 },
104 {
105 .name = "raw-upload",
106 .key = '1',
107 .arg = NULL,
108 .flags = 0,
109 .doc = "Upload raw data using channel",
110 .group = 0
111 },
112 {
113 .name = "raw-download",
114 .key = '2',
115 .arg = NULL,
116 .flags = 0,
117 .doc = "Download raw data using channel",
118 .group = 0
119 },
120 {
121 .name = "scp-upload",
122 .key = '3',
123 .arg = NULL,
124 .flags = 0,
125 .doc = "Upload data using SCP",
126 .group = 0
127 },
128 {
129 .name = "scp-download",
130 .key = '4',
131 .arg = NULL,
132 .flags = 0,
133 .doc = "Download data using SCP",
134 .group = 0
135 },
136 {
137 .name = "sync-sftp-upload",
138 .key = '5',
139 .arg = NULL,
140 .flags = 0,
141 .doc = "Upload data using synchronous SFTP",
142 .group = 0
143 },
144 {
145 .name = "sync-sftp-download",
146 .key = '6',
147 .arg = NULL,
148 .flags = 0,
149 .doc = "Download data using synchronous SFTP (slow)",
150 .group = 0
151 },
152 {
153 .name = "async-sftp-download",
154 .key = '7',
155 .arg = NULL,
156 .flags = 0,
157 .doc = "Download data using asynchronous SFTP (fast)",
158 .group = 0
159 },
160 {
161 .name = "async-sftp-aio-download",
162 .key = '8',
163 .arg = NULL,
164 .flags = 0,
165 .doc = "Download data using asynchronous SFTP AIO api (fast)",
166 .group = 0
167 },
168 {
169 .name = "async-sftp-aio-upload",
170 .key = '9',
171 .arg = NULL,
172 .flags = 0,
173 .doc = "Upload data using asynchronous SFTP AIO api (fast)",
174 .group = 0
175 },
176 {
177 .name = "host",
178 .key = 'h',
179 .arg = "HOST",
180 .flags = 0,
181 .doc = "Add a host to connect for benchmark (format user@hostname)",
182 .group = 0
183 },
184 {
185 .name = "size",
186 .key = 's',
187 .arg = "MBYTES",
188 .flags = 0,
189 .doc = "MBytes of data to send/receive per test",
190 .group = 0
191 },
192 {
193 .name = "chunk",
194 .key = 'c',
195 .arg = "bytes",
196 .flags = 0,
197 .doc = "size of data chunks to send/receive",
198 .group = 0
199 },
200 {
201 .name = "prequests",
202 .key = 'p',
203 .arg = "number [20]",
204 .flags = 0,
205 .doc = "[async SFTP] number of concurrent requests",
206 .group = 0
207 },
208 {
209 .name = "cipher",
210 .key = 'C',
211 .arg = "cipher",
212 .flags = 0,
213 .doc = "Cryptographic cipher to be used",
214 .group = 0
215 },
216
217 {NULL, 0, NULL, 0, NULL, 0}
218 };
219
220 /* Parse a single option. */
221 static error_t parse_opt (int key, char *arg, struct argp_state *state)
222 {
223 /* Get the input argument from argp_parse, which we
224 * know is a pointer to our arguments structure.
225 */
226 struct argument_s *arguments = state->input;
227
228 /* arg is currently not used */
229 (void) arg;
230
231 switch (key) {
232 case '1':
233 case '2':
234 case '3':
235 case '4':
236 case '5':
237 case '6':
238 case '7':
239 case '8':
240 case '9':
241 benchmarks[key - '1'].enabled = 1;
242 arguments->ntests++;
243 break;
244 case 'v':
245 arguments->verbose++;
246 break;
247 case 's':
248 arguments->datasize = atoi(arg);
249 break;
250 case 'p':
251 arguments->concurrent_requests = atoi(arg);
252 break;
253 case 'c':
254 arguments->chunksize = atoi(arg);
255 break;
256 case 'C':
257 arguments->cipher = arg;
258 break;
259 case 'h':
260 if (arguments->nhosts >= MAX_HOSTS_CONNECT) {
261 fprintf(stderr, "Too much hosts\n");
262 return ARGP_ERR_UNKNOWN;
263 }
264
265 arguments->hosts[arguments->nhosts] = arg;
266 arguments->nhosts++;
267 break;
268 case ARGP_KEY_ARG:
269 /* End processing here. */
270 cmdline = &state->argv [state->next - 1];
271 state->next = state->argc;
272 break;
273 default:
274 return ARGP_ERR_UNKNOWN;
275 }
276
277 return 0;
278 }
279
280 /* Our argp parser. */
281 static struct argp argp = {options, parse_opt, NULL, doc, NULL, NULL, NULL};
282
283 #endif /* HAVE_ARGP_H */
284
285 static void cmdline_parse(int argc, char **argv, struct argument_s *arguments)
286 {
287 /*
288 * Parse our arguments; every option seen by parse_opt will
289 * be reflected in arguments.
290 */
291 #ifdef HAVE_ARGP_H
292 argp_parse(&argp, argc, argv, 0, 0, arguments);
293 #else /* HAVE_ARGP_H */
294 (void) argc;
295 (void) argv;
296 arguments->hosts[0] = "localhost";
297 arguments->nhosts = 1;
298 #endif /* HAVE_ARGP_H */
299 }
300
301 static void arguments_init(struct argument_s *arguments)
302 {
303 memset(arguments, 0, sizeof(*arguments));
304 arguments->chunksize = 32758;
305 arguments->concurrent_requests = 20;
306 arguments->datasize = 10;
307 }
308
309 static ssh_session connect_host(const char *host, int verbose, char *cipher)
310 {
311 ssh_session session = NULL;
312 int rc;
313
314 session = ssh_new();
315 if (session == NULL) {
316 fprintf(stderr, "Error connecting to \"%s\": %s\n",
317 host, "Unable to create a new ssh session");
318 return NULL;
319 }
320
321 rc = ssh_options_set(session, SSH_OPTIONS_HOST, host);
322 if (rc < 0)
323 goto error;
324
325 rc = ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbose);
326 if (rc < 0)
327 goto error;
328
329 if (cipher != NULL) {
330 rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, cipher);
331 if (rc < 0)
332 goto error;
333
334 rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_S_C, cipher);
335 if (rc < 0)
336 goto error;
337 }
338
339 rc = ssh_options_parse_config(session, NULL);
340 if (rc < 0)
341 goto error;
342
343 rc = ssh_connect(session);
344 if (rc == SSH_ERROR)
345 goto error;
346
347 rc = ssh_userauth_autopubkey(session, NULL);
348 if (rc != SSH_AUTH_SUCCESS)
349 goto error;
350
351 return session;
352
353 error:
354 fprintf(stderr, "Error connecting to \"%s\": %s\n",
355 host, ssh_get_error(session));
356 ssh_free(session);
357 return NULL;
358 }
359
360 static char *network_speed(float bps)
361 {
362 static char buf[128];
363 if (bps > 1000 * 1000 * 1000) {
364 /* Gbps */
365 snprintf(buf, sizeof(buf), "%f Gbps", bps / (1000 * 1000 * 1000));
366 } else if (bps > 1000 * 1000) {
367 /* Mbps */
368 snprintf(buf, sizeof(buf), "%f Mbps", bps / (1000 * 1000));
369 } else if (bps > 1000) {
370 snprintf(buf, sizeof(buf), "%f Kbps", bps / 1000);
371 } else {
372 snprintf(buf, sizeof(buf), "%f bps", bps);
373 }
374
375 return buf;
376 }
377
378 static void do_benchmarks(ssh_session session, struct argument_s *arguments,
379 const char *hostname)
380 {
381 float ping_rtt = 0.0;
382 float ssh_rtt = 0.0;
383 float bps = 0.0;
384 int i;
385 int err;
386 struct benchmark *b = NULL;
387
388 if (arguments->verbose > 0)
389 fprintf(stdout, "Testing ICMP RTT\n");
390
391 err = benchmarks_ping_latency(hostname, &ping_rtt);
392 if (err == 0) {
393 fprintf(stdout, "ping RTT : %f ms\n", ping_rtt);
394 }
395
396 err = benchmarks_ssh_latency(session, &ssh_rtt);
397 if (err == 0) {
398 fprintf(stdout,
399 "SSH RTT : %f ms. Theoretical max BW (win=128K) : %s\n",
400 ssh_rtt, network_speed(128000.0 / (ssh_rtt / 1000.0)));
401 }
402
403 for (i=0; i < BENCHMARK_NUMBER; ++i){
404 b = &benchmarks[i];
405 if (b->enabled) {
406 err=b->fct(session, arguments, &bps);
407
408 if (err == 0) {
409 fprintf(stdout,
410 "%s : %s : %s\n",
411 hostname, b->name, network_speed(bps));
412 }
413 }
414 }
415 }
416
417 char *buffer = NULL;
418
419 int main(int argc, char **argv)
420 {
421 struct argument_s arguments;
422 ssh_session session = NULL;
423 int i, r;
424
425 arguments_init(&arguments);
426 cmdline_parse(argc, argv, &arguments);
427 if (arguments.nhosts == 0) {
428 fprintf(stderr, "At least one host (-h) must be specified\n");
429 return EXIT_FAILURE;
430 }
431
432 if (arguments.ntests == 0) {
433 for (i=0; i < BENCHMARK_NUMBER; ++i) {
434 benchmarks[i].enabled = 1;
435 }
436 arguments.ntests = BENCHMARK_NUMBER;
437 }
438
439 buffer = malloc(arguments.chunksize > 1024 ? arguments.chunksize : 1024);
440 if (buffer == NULL) {
441 fprintf(stderr, "Allocation of chunk buffer failed\n");
442 return EXIT_FAILURE;
443 }
444
445 if (arguments.verbose > 0) {
446 fprintf(stdout, "Will try hosts ");
447 for (i=0; i < arguments.nhosts; ++i) {
448 fprintf(stdout, "\"%s\" ", arguments.hosts[i]);
449 }
450
451 fprintf(stdout, "with benchmarks ");
452 for (i = 0; i < BENCHMARK_NUMBER; ++i) {
453 if (benchmarks[i].enabled)
454 fprintf(stdout, "\"%s\" ", benchmarks[i].name);
455 }
456
457 fprintf(stdout,"\n");
458 }
459
460 r = ssh_init();
461 if (r == SSH_ERROR) {
462 fprintf(stderr, "Failed to initialize libssh\n");
463 return EXIT_FAILURE;
464 }
465
466 for (i = 0; i < arguments.nhosts; ++i) {
467 if (arguments.verbose > 0)
468 fprintf(stdout, "Connecting to \"%s\"...\n", arguments.hosts[i]);
469
470 session = connect_host(arguments.hosts[i],
471 arguments.verbose,
472 arguments.cipher);
473 if (session != NULL && arguments.verbose > 0)
474 fprintf(stdout, "Success\n");
475
476 if (session == NULL) {
477 fprintf(stderr, "Errors occurred, stopping\n");
478 return EXIT_FAILURE;
479 }
480
481 do_benchmarks(session, &arguments, arguments.hosts[i]);
482 ssh_disconnect(session);
483 ssh_free(session);
484 }
485
486 r = ssh_finalize();
487 if (r == SSH_ERROR) {
488 fprintf(stderr, "Failed to finalize libssh\n");
489 return EXIT_FAILURE;
490 }
491
492 return EXIT_SUCCESS;
493 }
494