aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmarks/bench_sftp.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/benchmarks/bench_sftp.c')
-rw-r--r--tests/benchmarks/bench_sftp.c111
1 files changed, 99 insertions, 12 deletions
diff --git a/tests/benchmarks/bench_sftp.c b/tests/benchmarks/bench_sftp.c
index 915d1b7a..9e4ab34d 100644
--- a/tests/benchmarks/bench_sftp.c
+++ b/tests/benchmarks/bench_sftp.c
@@ -25,6 +25,7 @@
#include <libssh/sftp.h>
#include <stdio.h>
#include <fcntl.h>
+#include <stdlib.h>
#define SFTPDIR "/tmp/"
#define SFTPFILE "scpbenchmark"
@@ -39,16 +40,14 @@
*/
int benchmarks_sync_sftp_up (ssh_session session, struct argument_s *args,
float *bps){
- unsigned long bytes=0x1000000;
- static char buffer[0x10000];
+ unsigned long bytes;
struct timestamp_struct ts;
float ms=0.0;
unsigned long total=0;
sftp_session sftp;
sftp_file file;
- if(args->data != 0)
- bytes = args->data * 1024 * 1024;
+ bytes = args->datasize * 1024 * 1024;
sftp = sftp_new(session);
if(sftp == NULL)
goto error;
@@ -63,8 +62,8 @@ int benchmarks_sync_sftp_up (ssh_session session, struct argument_s *args,
while(total < bytes){
unsigned long towrite = bytes - total;
int w;
- if(towrite > 32758)
- towrite = 32758;
+ if(towrite > args->chunksize)
+ towrite = args->chunksize;
w=sftp_write(file,buffer,towrite);
if(w == SSH_ERROR)
goto error;
@@ -97,8 +96,7 @@ error:
*/
int benchmarks_sync_sftp_down (ssh_session session, struct argument_s *args,
float *bps){
- unsigned long bytes=0x1000000;
- static char buffer[0x10000];
+ unsigned long bytes;
struct timestamp_struct ts;
float ms=0.0;
unsigned long total=0;
@@ -106,8 +104,7 @@ int benchmarks_sync_sftp_down (ssh_session session, struct argument_s *args,
sftp_file file;
int r;
- if(args->data != 0)
- bytes = args->data * 1024 * 1024;
+ bytes = args->datasize * 1024 * 1024;
sftp = sftp_new(session);
if(sftp == NULL)
goto error;
@@ -121,8 +118,8 @@ int benchmarks_sync_sftp_down (ssh_session session, struct argument_s *args,
timestamp_init(&ts);
while(total < bytes){
unsigned long toread = bytes - total;
- if(toread > sizeof(buffer))
- toread = sizeof(buffer);
+ if(toread > args->chunksize)
+ toread = args->chunksize;
r=sftp_read(file,buffer,toread);
if(r == SSH_ERROR)
goto error;
@@ -150,3 +147,93 @@ error:
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;
+ 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(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;
+}