From 5d9cd8bf39cac07fb674a4bb8c7667dba1773ba2 Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Sun, 28 Aug 2011 13:29:25 +0300 Subject: [PATCH] benchmarks: added raw_download test --- tests/benchmarks/bench_raw.c | 104 ++++++++++++++++++++++++++++++++-- tests/benchmarks/benchmarks.c | 32 ++++++++--- tests/benchmarks/benchmarks.h | 7 ++- tests/benchmarks/latency.c | 2 +- 4 files changed, 130 insertions(+), 15 deletions(-) diff --git a/tests/benchmarks/bench_raw.c b/tests/benchmarks/bench_raw.c index c870942a..431659c5 100644 --- a/tests/benchmarks/bench_raw.c +++ b/tests/benchmarks/bench_raw.c @@ -109,13 +109,12 @@ int benchmarks_raw_up (ssh_session session, struct argument_s *args, unsigned long bytes=0x1000000; char *script=get_python_eater(bytes); char cmd[128]; - char buffer[1024]; + static char buffer[0x10000]; int err; ssh_channel channel; struct timestamp_struct ts; float ms=0.0; unsigned long total=0; - (void)bps; err=upload_script(session,"/tmp/eater.py",script); free(script); @@ -144,8 +143,8 @@ int benchmarks_raw_up (ssh_session session, struct argument_s *args, while(total < bytes){ unsigned long towrite = bytes - total; int w; - if(towrite > 0x1000) - towrite = 0x1000; + if(towrite > 0x10000) + towrite = 32758; w=ssh_channel_write(channel,buffer,towrite); if(w == SSH_ERROR) goto error; @@ -180,3 +179,100 @@ error: } return -1; } + +const char python_giver[] = +"#!/usr/bin/python\n" +"import sys\n" +"r=sys.stdin.read(2)\n" +"towrite=XXXXXXXXXX\n" +"wrote=0\n" +"while(wrote < towrite):\n" +" buffersize=towrite-wrote\n" +" if(buffersize > 4096):\n" +" buffersize=4096\n" +" sys.stdout.write('A'*buffersize)\n" +" wrote+=buffersize\n" +"sys.stdout.flush()\n"; + +static char *get_python_giver(unsigned long bytes){ + char *giver=malloc(sizeof(python_giver)); + char *ptr; + char buffer[12]; + + memcpy(giver,python_giver,sizeof(python_giver)); + ptr=strstr(giver,"XXXXXXXXXX"); + if(!ptr){ + free(giver); + return NULL; + } + sprintf(buffer,"0x%.8lx",bytes); + memcpy(ptr,buffer,10); + return giver; +} + +/** @internal + * @brief benchmarks a raw download (simple upload in a SSH channel) 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_raw_down (ssh_session session, struct argument_s *args, + float *bps){ + unsigned long bytes=0x1000000; + char *script=get_python_giver(bytes); + char cmd[128]; + static char buffer[0x10000]; + int err; + ssh_channel channel; + struct timestamp_struct ts; + float ms=0.0; + unsigned long total=0; + + err=upload_script(session,"/tmp/giver.py",script); + free(script); + if(err<0) + return err; + channel=ssh_channel_new(session); + if(channel == NULL) + goto error; + if(ssh_channel_open_session(channel)==SSH_ERROR) + goto error; + snprintf(cmd,sizeof(cmd),"%s /tmp/giver.py", PYTHON_PATH); + if(ssh_channel_request_exec(channel,cmd)==SSH_ERROR) + goto error; + if((err=ssh_channel_write(channel,"go",2))==SSH_ERROR) + 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; + int r; + if(toread > sizeof(buffer)) + toread = sizeof(buffer); + r=ssh_channel_read(channel,buffer,toread,0); + if(r == SSH_ERROR) + goto error; + total += r; + } + + if(args->verbose>0) + fprintf(stdout,"Finished download\n"); + 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); + ssh_channel_close(channel); + ssh_channel_free(channel); + return 0; +error: + fprintf(stderr,"Error during raw upload : %s\n",ssh_get_error(session)); + if(channel){ + ssh_channel_close(channel); + ssh_channel_free(channel); + } + return -1; +} diff --git a/tests/benchmarks/benchmarks.c b/tests/benchmarks/benchmarks.c index 7ff6172e..647e5e7e 100644 --- a/tests/benchmarks/benchmarks.c +++ b/tests/benchmarks/benchmarks.c @@ -28,8 +28,8 @@ #include const char *libssh_benchmarks_names[]={ - "null", - "benchmark_raw_upload" + "benchmark_raw_upload", + "benchmark_raw_download" }; #ifdef HAVE_ARGP_H @@ -62,6 +62,14 @@ static struct argp_option options[] = { .doc = "Upload raw data using channel", .group = 0 }, + { + .name = "raw-download", + .key = '2', + .arg = NULL, + .flags = 0, + .doc = "Download raw data using channel", + .group = 0 + }, { .name = "host", .key = 'h', @@ -85,7 +93,8 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) { switch (key) { case '1': - arguments->benchmarks[key - '1' + 1] = 1; + case '2': + arguments->benchmarks[key - '1'] = 1; arguments->ntests ++; break; case 'v': @@ -185,13 +194,20 @@ static void do_benchmarks(ssh_session session, struct argument_s *arguments, if(err==0){ fprintf(stdout, "SSH RTT : %f ms\n",ssh_rtt); } - if(arguments->benchmarks[BENCHMARK_RAW_UPLOAD-1]){ + if(arguments->benchmarks[BENCHMARK_RAW_UPLOAD]){ err=benchmarks_raw_up(session,arguments,&bps); if(err==0){ fprintf(stdout, "%s : %s : %s\n",hostname, libssh_benchmarks_names[BENCHMARK_RAW_UPLOAD], network_speed(bps)); } } + if(arguments->benchmarks[BENCHMARK_RAW_DOWNLOAD]){ + err=benchmarks_raw_down(session,arguments,&bps); + if(err==0){ + fprintf(stdout, "%s : %s : %s\n",hostname, + libssh_benchmarks_names[BENCHMARK_RAW_DOWNLOAD], network_speed(bps)); + } + } } int main(int argc, char **argv){ @@ -207,9 +223,9 @@ int main(int argc, char **argv){ } if (arguments.ntests==0){ for(i=1; i < BENCHMARK_NUMBER ; ++i){ - arguments.benchmarks[i-1]=1; + arguments.benchmarks[i]=1; } - arguments.ntests=BENCHMARK_NUMBER-1; + arguments.ntests=BENCHMARK_NUMBER; } if (arguments.verbose > 0){ fprintf(stdout, "Will try hosts "); @@ -217,9 +233,9 @@ int main(int argc, char **argv){ fprintf(stdout,"\"%s\" ", arguments.hosts[i]); } fprintf(stdout,"with benchmarks "); - for(i=0;i