summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/bench_scp.c
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2011-08-29 20:13:24 +0300
committerAris Adamantiadis <aris@0xbadc0de.be>2011-09-02 11:43:07 +0300
commitb11567ed9bf47688369fec2cf4884082790c43c4 (patch)
tree700006c8f0881b2b649da182f36f3f42b2805dda /tests/benchmarks/bench_scp.c
parentfaaf334aa38a0a71894f78eb20d6c39e5e34f79e (diff)
downloadlibssh-b11567ed9bf47688369fec2cf4884082790c43c4.tar.gz
libssh-b11567ed9bf47688369fec2cf4884082790c43c4.tar.xz
libssh-b11567ed9bf47688369fec2cf4884082790c43c4.zip
benchmarks: refactoring + sync sftp tests
no surprise, sync sftp is much slower, even for localhost, especially for download.
Diffstat (limited to 'tests/benchmarks/bench_scp.c')
-rw-r--r--tests/benchmarks/bench_scp.c131
1 files changed, 130 insertions, 1 deletions
diff --git a/tests/benchmarks/bench_scp.c b/tests/benchmarks/bench_scp.c
index 3ef3bbf..3e0a7c0 100644
--- a/tests/benchmarks/bench_scp.c
+++ b/tests/benchmarks/bench_scp.c
@@ -2,7 +2,7 @@
*
* This file is part of the SSH Library
*
- * Copyright (c) 2010 by Aris Adamantiadis
+ * 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
@@ -22,4 +22,133 @@
#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=0x1000000;
+ static char buffer[0x10000];
+ struct timestamp_struct ts;
+ float ms=0.0;
+ unsigned long total=0;
+ ssh_scp scp;
+
+ if(args->data != 0)
+ bytes = args->data * 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 > 32758)
+ towrite = 32758;
+ 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=0x1000000;
+ static char buffer[0x10000];
+ struct timestamp_struct ts;
+ float ms=0.0;
+ unsigned long total=0;
+ ssh_scp scp;
+ int r;
+ size_t size;
+
+ if(args->data != 0)
+ bytes = args->data * 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 > sizeof(buffer))
+ toread = sizeof(buffer);
+ 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;
+}