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
|
/* bench_scp.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 <stdio.h>
#define SCPDIR "/tmp/"
#define SCPFILE "scpbenchmark"
/** @internal
* @brief benchmarks a scp 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_scp_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;
ssh_scp scp;
bytes = args->datasize * 1024 * 1024;
scp = ssh_scp_new(session,SSH_SCP_WRITE,SCPDIR);
if(scp == NULL)
goto error;
if(ssh_scp_init(scp)==SSH_ERROR)
goto error;
if(ssh_scp_push_file(scp,SCPFILE,bytes,0777) != SSH_OK)
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=ssh_scp_write(scp,buffer,towrite);
if(w == SSH_ERROR)
goto error;
total += towrite;
}
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);
ssh_scp_close(scp);
ssh_scp_free(scp);
return 0;
error:
fprintf(stderr,"Error during scp upload : %s\n",ssh_get_error(session));
if(scp){
ssh_scp_close(scp);
ssh_scp_free(scp);
}
return -1;
}
/** @internal
* @brief benchmarks a scp 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_scp_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;
ssh_scp scp;
int r;
size_t size;
bytes = args->datasize * 1024 * 1024;
scp = ssh_scp_new(session,SSH_SCP_READ,SCPDIR SCPFILE);
if(scp == NULL)
goto error;
if(ssh_scp_init(scp)==SSH_ERROR)
goto error;
r=ssh_scp_pull_request(scp);
if(r == SSH_SCP_REQUEST_NEWFILE){
size=ssh_scp_request_get_size(scp);
if(bytes > size){
printf("Only %d bytes available (on %lu requested).\n",size,bytes);
bytes = size;
}
if(size > bytes){
printf("File is %d bytes (on %lu requested). Will cut the end\n",size,bytes);
}
if(args->verbose>0)
fprintf(stdout,"Starting download of %lu bytes now\n",bytes);
timestamp_init(&ts);
ssh_scp_accept_request(scp);
while(total < bytes){
unsigned long toread = bytes - total;
if(toread > args->chunksize)
toread = args->chunksize;
r=ssh_scp_read(scp,buffer,toread);
if(r == SSH_ERROR || r == 0)
goto error;
total += r;
}
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);
} else {
fprintf(stderr,"Expected SSH_SCP_REQUEST_NEWFILE, got %d\n",r);
goto error;
}
ssh_scp_close(scp);
ssh_scp_free(scp);
return 0;
error:
fprintf(stderr,"Error during scp download : %s\n",ssh_get_error(session));
if(scp){
ssh_scp_close(scp);
ssh_scp_free(scp);
}
return -1;
}
|