aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmarks/bench_sftp.c
blob: e6c6824847f97faa671bcba5228c05b5005ed3ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* bench_sftp.c
 *
 * This file is part of the SSH Library
 *
 * Copyright (c) 2011 by Aris Adamantiadis
 *
 * The SSH Library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * The SSH Library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SSH Library; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include "benchmarks.h"
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include <libssh/misc.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>

#define SFTPDIR "/tmp/"
#define SFTPFILE "scpbenchmark"

/** @internal
 * @brief benchmarks a synchronous sftp upload using an
 * existing SSH session.
 * @param[in] session Open SSH session
 * @param[in] args Parsed command line arguments
 * @param[out] bps The calculated bytes per second obtained via benchmark.
 * @return 0 on success, -1 on error.
 */
int benchmarks_sync_sftp_up (ssh_session session, struct argument_s *args,
    float *bps){
  unsigned long bytes;
  struct timestamp_struct ts;
  float ms=0.0;
  unsigned long total=0;
  sftp_session sftp;
  sftp_file file = NULL;

  bytes = args->datasize * 1024 * 1024;
  sftp = sftp_new(session);
  if(sftp == NULL)
    goto error;
  if(sftp_init(sftp)==SSH_ERROR)
    goto error;
  file = sftp_open(sftp,SFTPDIR SFTPFILE,O_RDWR | O_CREAT | O_TRUNC, 0777);
  if(!file)
    goto error;
  if(args->verbose>0)
    fprintf(stdout,"Starting upload of %lu bytes now\n",bytes);
  timestamp_init(&ts);
  while(total < bytes){
    unsigned long towrite = bytes - total;
    int w;
    if(towrite > args->chunksize)
      towrite = args->chunksize;
    w=sftp_write(file,buffer,towrite);
    if(w == SSH_ERROR)
      goto error;
    total += w;
  }
  sftp_close(file);
  ms=elapsed_time(&ts);
  *bps=8000 * (float)bytes / ms;
  if(args->verbose > 0)
    fprintf(stdout,"Upload took %f ms for %lu bytes, at %f bps\n",ms,
        bytes,*bps);
  sftp_free(sftp);
  return 0;
error:
  fprintf(stderr,"Error during scp upload : %s\n",ssh_get_error(session));
  if(file)
    sftp_close(file);
  if(sftp)
    sftp_free(sftp);
  return -1;
}

/** @internal
 * @brief benchmarks a synchronous sftp download using an
 * existing SSH session.
 * @param[in] session Open SSH session
 * @param[in] args Parsed command line arguments
 * @param[out] bps The calculated bytes per second obtained via benchmark.
 * @return 0 on success, -1 on error.
 */
int benchmarks_sync_sftp_down (ssh_session session, struct argument_s *args,
    float *bps){
  unsigned long bytes;
  struct timestamp_struct ts;
  float ms=0.0;
  unsigned long total=0;
  sftp_session sftp;
  sftp_file file = NULL;
  int r;

  bytes = args->datasize * 1024 * 1024;
  sftp = sftp_new(session);
  if(sftp == NULL)
    goto error;
  if(sftp_init(sftp)==SSH_ERROR)
    goto error;
  file = sftp_open(sftp,SFTPDIR SFTPFILE,O_RDONLY,0);
  if(!file)
    goto error;
  if(args->verbose>0)
    fprintf(stdout,"Starting download of %lu bytes now\n",bytes);
  timestamp_init(&ts);
  while(total < bytes){
    unsigned long toread = bytes - total;
    if(toread > args->chunksize)
      toread = args->chunksize;
    r=sftp_read(file,buffer,toread);
    if(r == SSH_ERROR)
      goto error;
    total += r;
    /* we had a smaller file */
    if(r==0){
      fprintf(stdout,"File smaller than expected : %lu (expected %lu).\n",total,bytes);
      bytes = total;
      break;
    }
  }
  sftp_close(file);
  ms=elapsed_time(&ts);
  *bps=8000 * (float)bytes / ms;
  if(args->verbose > 0)
    fprintf(stdout,"download took %f ms for %lu bytes, at %f bps\n",ms,
        bytes,*bps);
  sftp_free(sftp);
  return 0;
error:
  fprintf(stderr,"Error during sftp download : %s\n",ssh_get_error(session));
  if(file)
    sftp_close(file);
  if(sftp)
    sftp_free(sftp);
  return -1;
}

/** @internal
 * @brief benchmarks an asynchronous sftp download using an
 * existing SSH session.
 * @param[in] session Open SSH session
 * @param[in] args Parsed command line arguments
 * @param[out] bps The calculated bytes per second obtained via benchmark.
 * @return 0 on success, -1 on error.
 */
int benchmarks_async_sftp_down (ssh_session session, struct argument_s *args,
    float *bps){
  unsigned long bytes;
  struct timestamp_struct ts;
  float ms=0.0;
  unsigned long total=0;
  sftp_session sftp;
  sftp_file file = NULL;
  int r,i;
  int warned = 0;
  unsigned long toread;
  int *ids=NULL;
  int concurrent_downloads = args->concurrent_requests;

  bytes = args->datasize * 1024 * 1024;
  sftp = sftp_new(session);
  if(sftp == NULL)
    goto error;
  if(sftp_init(sftp)==SSH_ERROR)
    goto error;
  file = sftp_open(sftp,SFTPDIR SFTPFILE,O_RDONLY,0);
  if(!file)
    goto error;
  ids = malloc(concurrent_downloads * sizeof(int));
  if (ids == NULL) {
    return -1;
  }
  if(args->verbose>0)
    fprintf(stdout,"Starting download of %lu bytes now, using %d concurrent downloads\n",bytes,
        concurrent_downloads);
  timestamp_init(&ts);
  for (i=0;i<concurrent_downloads;++i){
    ids[i]=sftp_async_read_begin(file, args->chunksize);
    if(ids[i]==SSH_ERROR)
        goto error;
  }
  i=0;
  while(total < bytes){
    r = sftp_async_read(file, buffer, args->chunksize, ids[i]);
    if(r == SSH_ERROR)
      goto error;
    total += r;
    if(r != (int)args->chunksize && total != bytes && !warned){
      fprintf(stderr,"async_sftp_download : receiving short reads (%d, requested %d) "
          "the received file will be corrupted and shorted. Adapt chunksize to %d\n",
          r, args->chunksize,r);
      warned = 1;
    }
    /* we had a smaller file */
    if(r==0){
      fprintf(stdout,"File smaller than expected : %lu (expected %lu).\n",total,bytes);
      bytes = total;
      break;
    }
    toread = bytes - total;
    if(toread < args->chunksize * concurrent_downloads){
      /* we've got enough launched downloads */
      ids[i]=-1;
    }
    if(toread > args->chunksize)
      toread = args->chunksize;
    ids[i]=sftp_async_read_begin(file,toread);
    if(ids[i] == SSH_ERROR)
      goto error;
    i = (i+1) % concurrent_downloads;
  }
  sftp_close(file);
  ms=elapsed_time(&ts);
  *bps=8000 * (float)bytes / ms;
  if(args->verbose > 0)
    fprintf(stdout,"download took %f ms for %lu bytes, at %f bps\n",ms,
        bytes,*bps);
  sftp_free(sftp);
  free(ids);
  return 0;
error:
  fprintf(stderr,"Error during sftp download : %s\n",ssh_get_error(session));
  if(file)
    sftp_close(file);
  if(sftp)
    sftp_free(sftp);
  free(ids);
  return -1;
}

int benchmarks_async_sftp_aio_down(ssh_session session,
                                   struct argument_s *args,
                                   float *bps)
{
    sftp_session sftp = NULL;
    sftp_limits_t li = NULL;
    sftp_file file = NULL;
    sftp_aio aio = NULL;

    struct ssh_list *aio_queue = NULL;

    int concurrent_downloads = args->concurrent_requests;
    size_t chunksize;
    struct timestamp_struct ts = {0};
    float ms = 0.0f;

    size_t total_bytes = args->datasize * 1024 * 1024;
    size_t total_bytes_requested = 0, total_bytes_read = 0;
    size_t bufsize = args->chunksize;
    size_t to_read;
    ssize_t bytes_read, bytes_requested;
    int warned = 0, i, rc;

    sftp = sftp_new(session);
    if (sftp == NULL) {
        fprintf(stderr, "Error during sftp aio download: %s\n",
                ssh_get_error(session));
        return -1;
    }

    /*
     * Errors which are logged in the ssh session are reported after
     * jumping to the goto label, errors which aren't logged inside the
     * ssh session are reported before jumping to that label
     */

    rc = sftp_init(sftp);
    if (rc == SSH_ERROR) {
        goto error;
    }

    li = sftp_limits(sftp);
    if (li == NULL) {
        goto error;
    }

    if (args->chunksize > li->max_read_length) {
       chunksize = li->max_read_length;
       if (args->verbose > 0) {
           fprintf(stdout,
                   "Using the chunk size %zu (not the set size %u), "
                   "to respect the max data limit for read packet\n",
                   chunksize, args->chunksize);
       }
    } else {
        chunksize = args->chunksize;
    }

    file = sftp_open(sftp, SFTPDIR SFTPFILE, O_RDONLY, 0);
    if (file == NULL) {
        goto error;
    }

    aio_queue = ssh_list_new();
    if (aio_queue == NULL) {
        fprintf(stderr,
                "Error during sftp aio download: Insufficient memory\n");
        goto error;
    }

    if (args->verbose > 0) {
        fprintf(stdout,
                "Starting download of %zu bytes now, "
                "using %d concurrent downloads.\n",
                total_bytes, concurrent_downloads);
    }

    timestamp_init(&ts);

    for (i = 0;
         i < concurrent_downloads && total_bytes_requested < total_bytes;
         ++i) {
        to_read = total_bytes - total_bytes_requested;
        if (to_read > chunksize) {
            to_read = chunksize;
        }

        bytes_requested = sftp_aio_begin_read(file, to_read, &aio);
        if (bytes_requested == SSH_ERROR) {
            goto error;
        }

        if ((size_t)bytes_requested != to_read) {
            fprintf(stderr,
                    "Error during sftp aio download: sftp_aio_begin_read() "
                    "requesting less bytes even when the number of bytes "
                    "asked to read are within the max limit");
            sftp_aio_free(aio);
            goto error;
        }

        total_bytes_requested += (size_t)bytes_requested;

        /* enqueue */
        rc = ssh_list_append(aio_queue, aio);
        if (rc == SSH_ERROR) {
            fprintf(stderr,
                    "Error during sftp aio download: Insufficient memory");
            sftp_aio_free(aio);
            goto error;
        }
    }

    while ((aio = ssh_list_pop_head(sftp_aio, aio_queue)) != NULL) {
        bytes_read = sftp_aio_wait_read(&aio, buffer, bufsize);
        if (bytes_read == -1) {
            goto error;
        }

        total_bytes_read += (size_t)bytes_read;
        if (bytes_read == 0) {
            fprintf(stdout ,
                    "File smaller than expected: %zu bytes (expected %zu).\n",
                    total_bytes_read, total_bytes);
            break;
        }

        if (total_bytes_read != total_bytes &&
            (size_t)bytes_read != chunksize &&
            warned != 1) {
            fprintf(stderr,
                    "async_sftp_aio_download: Receiving short reads "
                    "(%zu, expected %zu) before encountering eof, "
                    "the received file will be corrupted and shorted. "
                    "Adapt chunksize to %zu.\n",
                    bytes_read, chunksize, bytes_read);
            warned = 1;
        }

        if (total_bytes_requested == total_bytes) {
            /* No need to issue more requests */
            continue;
        }

        /* else issue a request */
        to_read = total_bytes - total_bytes_requested;
        if (to_read > chunksize) {
            to_read = chunksize;
        }

        bytes_requested = sftp_aio_begin_read(file, to_read, &aio);
        if (bytes_requested == SSH_ERROR) {
            goto error;
        }

        if ((size_t)bytes_requested != to_read) {
            fprintf(stderr,
                    "Error during sftp aio download: sftp_aio_begin_read() "
                    "requesting less bytes even when the number of bytes "
                    "asked to read are within the max limit");
            sftp_aio_free(aio);
            goto error;
        }

        total_bytes_requested += (size_t)bytes_requested;

        /* enqueue */
        rc = ssh_list_append(aio_queue, aio);
        if (rc == SSH_ERROR) {
            fprintf(stderr,
                    "Error during sftp aio download: Insufficient memory\n");
            sftp_aio_free(aio);
            goto error;
        }
    }

    sftp_close(file);
    ms = elapsed_time(&ts);
    *bps = (float)(8000 * total_bytes_read) / ms;
    if (args->verbose > 0) {
        fprintf(stdout, "Download took %f ms for %zu bytes at %f bps.\n",
                ms, total_bytes_read, *bps);
    }

    ssh_list_free(aio_queue);
    sftp_limits_free(li);
    sftp_free(sftp);
    return 0;

error:
    rc = ssh_get_error_code(session);
    if (rc != SSH_NO_ERROR) {
        fprintf(stderr, "Error during sftp aio download: %s\n",
                ssh_get_error(session));
    }

    /* Release aio structures corresponding to outstanding requests */
    while ((aio = ssh_list_pop_head(sftp_aio, aio_queue)) != NULL) {
        sftp_aio_free(aio);
    }

    ssh_list_free(aio_queue);
    sftp_close(file);
    sftp_limits_free(li);
    sftp_free(sftp);
    return -1;
}

int benchmarks_async_sftp_aio_up(ssh_session session,
                                 struct argument_s *args,
                                 float *bps)
{
    sftp_session sftp = NULL;
    sftp_limits_t li = NULL;
    sftp_file file = NULL;
    sftp_aio aio = NULL;
    struct ssh_list *aio_queue = NULL;

    int concurrent_uploads = args->concurrent_requests;
    size_t chunksize;
    struct timestamp_struct ts = {0};
    float ms = 0.0f;

    size_t total_bytes = args->datasize * 1024 * 1024;
    size_t to_write, total_bytes_requested = 0;
    ssize_t bytes_written, bytes_requested;
    int i, rc;

    sftp = sftp_new(session);
    if (sftp == NULL) {
        fprintf(stderr, "Error during sftp aio upload: %s\n",
                ssh_get_error(session));
        return -1;
    }

    /*
     * Errors which are logged in the ssh session are reported after
     * jumping to the goto label, errors which aren't logged inside the
     * ssh session are reported before jumping to that label
     */

    rc = sftp_init(sftp);
    if (rc == SSH_ERROR) {
        goto error;
    }

    li = sftp_limits(sftp);
    if (li == NULL) {
        goto error;
    }

    if (args->chunksize > li->max_write_length) {
       chunksize = li->max_write_length;
       if (args->verbose > 0) {
           fprintf(stdout,
                   "Using the chunk size %zu (not the set size %u), "
                   "to respect the max data limit for write packet\n",
                   chunksize, args->chunksize);
       }
    } else {
        chunksize = args->chunksize;
    }

    file = sftp_open(sftp, SFTPDIR SFTPFILE,
                     O_WRONLY | O_CREAT | O_TRUNC, 0777);
    if (file == NULL) {
        goto error;
    }

    aio_queue = ssh_list_new();
    if (aio_queue == NULL) {
        fprintf(stderr, "Error during sftp aio upload: Insufficient memory\n");
        goto error;
    }

    if (args->verbose > 0) {
        fprintf(stdout,
                "Starting upload of %zu bytes now, "
                "using %d concurrent uploads.\n",
                total_bytes, concurrent_uploads);
    }

    timestamp_init(&ts);

    for (i = 0;
         i < concurrent_uploads && total_bytes_requested < total_bytes;
         ++i) {
        to_write = total_bytes - total_bytes_requested;
        if (to_write > chunksize) {
            to_write = chunksize;
        }

        bytes_requested = sftp_aio_begin_write(file, buffer, to_write, &aio);
        if (bytes_requested == SSH_ERROR) {
            goto error;
        }

        if ((size_t)bytes_requested != to_write) {
            fprintf(stderr,
                    "Error during sftp aio upload: sftp_aio_begin_write() "
                    "requesting less bytes even when the number of bytes "
                    "asked to write are within the max write limit");
            sftp_aio_free(aio);
            goto error;
        }

        total_bytes_requested += (size_t)bytes_requested;

        /* enqueue */
        rc = ssh_list_append(aio_queue, aio);
        if (rc == SSH_ERROR) {
            fprintf(stderr,
                    "Error during sftp aio upload: Insufficient memory\n");
            sftp_aio_free(aio);
            goto error;
        }
    }

    while ((aio = ssh_list_pop_head(sftp_aio, aio_queue)) != NULL) {
        bytes_written = sftp_aio_wait_write(&aio);
        if (bytes_written == SSH_ERROR) {
            goto error;
        }

        if (total_bytes_requested == total_bytes) {
            /* No need to issue more requests */
            continue;
        }

        /* else issue a request */
        to_write = total_bytes - total_bytes_requested;
        if (to_write > chunksize) {
            to_write = chunksize;
        }

        bytes_requested = sftp_aio_begin_write(file, buffer, to_write, &aio);
        if (bytes_requested == SSH_ERROR) {
            goto error;
        }

        if ((size_t)bytes_requested != to_write) {
            fprintf(stderr,
                    "Error during sftp aio upload: sftp_aio_begin_write() "
                    "requesting less bytes even when the number of bytes "
                    "asked to write are within the max write limit");
            sftp_aio_free(aio);
            goto error;
        }

        total_bytes_requested += bytes_requested;

        /* enqueue */
        rc = ssh_list_append(aio_queue, aio);
        if (rc == SSH_ERROR) {
            fprintf(stderr,
                    "Error during sftp aio upload: Insufficient memory\n");
            sftp_aio_free(aio);
            goto error;
        }
    }

    sftp_close(file);
    ms = elapsed_time(&ts);
    *bps = (float)(8000 * total_bytes) / ms;
    if (args->verbose > 0) {
        fprintf(stdout, "Upload took %f ms for %zu bytes at %f bps.\n",
                ms, total_bytes, *bps);
    }

    ssh_list_free(aio_queue);
    sftp_limits_free(li);
    sftp_free(sftp);
    return 0;

error:
    rc = ssh_get_error_code(session);
    if (rc != SSH_NO_ERROR) {
        fprintf(stderr, "Error during sftp aio upload: %s\n",
                ssh_get_error(session));
    }

    /* Release aio structures corresponding to outstanding requests */
    while ((aio = ssh_list_pop_head(sftp_aio, aio_queue)) != NULL) {
        sftp_aio_free(aio);
    }

    ssh_list_free(aio_queue);
    sftp_close(file);
    sftp_limits_free(li);
    sftp_free(sftp);
    return -1;
}