1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-05 20:55:46 +03:00

Reformat tests/benchmarks/benchmarks.c

tests/benchmarks/benchmarks.c has been reformatted
according to current coding style.

Signed-off-by: Eshan Kelkar <eshankelkar@galorithm.com>
Reviewed-by: Sahana Prasad <sahana@redhat.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
Eshan Kelkar
2023-06-01 20:18:05 +05:30
committed by Sahana Prasad
parent 4768d2970a
commit d8790d06c4

View File

@@ -27,42 +27,42 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
struct benchmark benchmarks[]= { struct benchmark benchmarks[] = {
{ {
.name="benchmark_raw_upload", .name = "benchmark_raw_upload",
.fct=benchmarks_raw_up, .fct = benchmarks_raw_up,
.enabled=0 .enabled = 0
}, },
{ {
.name="benchmark_raw_download", .name = "benchmark_raw_download",
.fct=benchmarks_raw_down, .fct = benchmarks_raw_down,
.enabled=0 .enabled = 0
}, },
{ {
.name="benchmark_scp_upload", .name = "benchmark_scp_upload",
.fct=benchmarks_scp_up, .fct = benchmarks_scp_up,
.enabled=0 .enabled = 0
}, },
{ {
.name="benchmark_scp_download", .name = "benchmark_scp_download",
.fct=benchmarks_scp_down, .fct = benchmarks_scp_down,
.enabled=0 .enabled = 0
}, },
#ifdef WITH_SFTP #ifdef WITH_SFTP
{ {
.name="benchmark_sync_sftp_upload", .name = "benchmark_sync_sftp_upload",
.fct=benchmarks_sync_sftp_up, .fct = benchmarks_sync_sftp_up,
.enabled=0 .enabled = 0
}, },
{ {
.name="benchmark_sync_sftp_download", .name = "benchmark_sync_sftp_download",
.fct=benchmarks_sync_sftp_down, .fct = benchmarks_sync_sftp_down,
.enabled=0 .enabled = 0
}, },
{ {
.name="benchmark_async_sftp_download", .name = "benchmark_async_sftp_download",
.fct=benchmarks_async_sftp_down, .fct = benchmarks_async_sftp_down,
.enabled=0 .enabled = 0
} }
#endif /* WITH_SFTP */ #endif /* WITH_SFTP */
}; };
@@ -73,7 +73,7 @@ struct benchmark benchmarks[]= {
const char *argp_program_version = "libssh benchmarks 2011-08-28"; const char *argp_program_version = "libssh benchmarks 2011-08-28";
const char *argp_program_bug_address = "Aris Adamantiadis <aris@0xbadc0de.be>"; const char *argp_program_bug_address = "Aris Adamantiadis <aris@0xbadc0de.be>";
static char **cmdline; static char **cmdline = NULL;
/* Program documentation. */ /* Program documentation. */
static char doc[] = "libssh benchmarks"; static char doc[] = "libssh benchmarks";
@@ -128,7 +128,6 @@ static struct argp_option options[] = {
.flags = 0, .flags = 0,
.doc = "Upload data using synchronous SFTP", .doc = "Upload data using synchronous SFTP",
.group = 0 .group = 0
}, },
{ {
.name = "sync-sftp-download", .name = "sync-sftp-download",
@@ -137,7 +136,6 @@ static struct argp_option options[] = {
.flags = 0, .flags = 0,
.doc = "Download data using synchronous SFTP (slow)", .doc = "Download data using synchronous SFTP (slow)",
.group = 0 .group = 0
}, },
{ {
.name = "async-sftp-download", .name = "async-sftp-download",
@@ -146,7 +144,6 @@ static struct argp_option options[] = {
.flags = 0, .flags = 0,
.doc = "Download data using asynchronous SFTP (fast)", .doc = "Download data using asynchronous SFTP (fast)",
.group = 0 .group = 0
}, },
{ {
.name = "host", .name = "host",
@@ -193,7 +190,8 @@ static struct argp_option options[] = {
}; };
/* Parse a single option. */ /* Parse a single option. */
static error_t parse_opt (int key, char *arg, struct argp_state *state) { static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we /* Get the input argument from argp_parse, which we
* know is a pointer to our arguments structure. * know is a pointer to our arguments structure.
*/ */
@@ -211,7 +209,7 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) {
case '6': case '6':
case '7': case '7':
benchmarks[key - '1'].enabled = 1; benchmarks[key - '1'].enabled = 1;
arguments->ntests ++; arguments->ntests++;
break; break;
case 'v': case 'v':
arguments->verbose++; arguments->verbose++;
@@ -229,11 +227,12 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) {
arguments->cipher = arg; arguments->cipher = arg;
break; break;
case 'h': case 'h':
if(arguments->nhosts >= MAX_HOSTS_CONNECT){ if (arguments->nhosts >= MAX_HOSTS_CONNECT) {
fprintf(stderr, "Too much hosts\n"); fprintf(stderr, "Too much hosts\n");
return ARGP_ERR_UNKNOWN; return ARGP_ERR_UNKNOWN;
} }
arguments->hosts[arguments->nhosts]=arg;
arguments->hosts[arguments->nhosts] = arg;
arguments->nhosts++; arguments->nhosts++;
break; break;
case ARGP_KEY_ARG: case ARGP_KEY_ARG:
@@ -253,7 +252,8 @@ static struct argp argp = {options, parse_opt, NULL, doc, NULL, NULL, NULL};
#endif /* HAVE_ARGP_H */ #endif /* HAVE_ARGP_H */
static void cmdline_parse(int argc, char **argv, struct argument_s *arguments) { static void cmdline_parse(int argc, char **argv, struct argument_s *arguments)
{
/* /*
* Parse our arguments; every option seen by parse_opt will * Parse our arguments; every option seen by parse_opt will
* be reflected in arguments. * be reflected in arguments.
@@ -263,140 +263,170 @@ static void cmdline_parse(int argc, char **argv, struct argument_s *arguments) {
#else /* HAVE_ARGP_H */ #else /* HAVE_ARGP_H */
(void) argc; (void) argc;
(void) argv; (void) argv;
arguments->hosts[0]="localhost"; arguments->hosts[0] = "localhost";
arguments->nhosts=1; arguments->nhosts = 1;
#endif /* HAVE_ARGP_H */ #endif /* HAVE_ARGP_H */
} }
static void arguments_init(struct argument_s *arguments){ static void arguments_init(struct argument_s *arguments)
memset(arguments,0,sizeof(*arguments)); {
arguments->chunksize=32758; memset(arguments, 0, sizeof(*arguments));
arguments->concurrent_requests=20; arguments->chunksize = 32758;
arguments->concurrent_requests = 20;
arguments->datasize = 10; arguments->datasize = 10;
} }
static ssh_session connect_host(const char *host, int verbose, char *cipher){ static ssh_session connect_host(const char *host, int verbose, char *cipher)
ssh_session session=ssh_new(); {
if(session==NULL) ssh_session session = ssh_new();
if (session == NULL)
goto error; goto error;
if(ssh_options_set(session,SSH_OPTIONS_HOST, host)<0)
if (ssh_options_set(session, SSH_OPTIONS_HOST, host) < 0)
goto error; goto error;
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbose); ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbose);
if(cipher != NULL){ if (cipher != NULL) {
if (ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, cipher) || if (ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, cipher) ||
ssh_options_set(session, SSH_OPTIONS_CIPHERS_S_C, cipher)){ ssh_options_set(session, SSH_OPTIONS_CIPHERS_S_C, cipher)) {
goto error; goto error;
} }
} }
ssh_options_parse_config(session, NULL); ssh_options_parse_config(session, NULL);
if(ssh_connect(session)==SSH_ERROR) if (ssh_connect(session) == SSH_ERROR)
goto error; goto error;
if(ssh_userauth_autopubkey(session,NULL) != SSH_AUTH_SUCCESS) if (ssh_userauth_autopubkey(session, NULL) != SSH_AUTH_SUCCESS)
goto error; goto error;
return session; return session;
error: error:
fprintf(stderr,"Error connecting to \"%s\": %s\n",host,ssh_get_error(session)); fprintf(stderr, "Error connecting to \"%s\": %s\n",
host, ssh_get_error(session));
ssh_free(session); ssh_free(session);
return NULL; return NULL;
} }
static char *network_speed(float bps){ static char *network_speed(float bps)
{
static char buf[128]; static char buf[128];
if(bps > 1000*1000*1000){ if (bps > 1000 * 1000 * 1000) {
/* Gbps */ /* Gbps */
snprintf(buf,sizeof(buf),"%f Gbps",bps/(1000*1000*1000)); snprintf(buf, sizeof(buf), "%f Gbps", bps / (1000 * 1000 * 1000));
} else if(bps > 1000*1000){ } else if (bps > 1000 * 1000) {
/* Mbps */ /* Mbps */
snprintf(buf,sizeof(buf),"%f Mbps",bps/(1000*1000)); snprintf(buf, sizeof(buf), "%f Mbps", bps / (1000 * 1000));
} else if(bps > 1000){ } else if (bps > 1000) {
snprintf(buf,sizeof(buf),"%f Kbps",bps/1000); snprintf(buf, sizeof(buf), "%f Kbps", bps / 1000);
} else { } else {
snprintf(buf,sizeof(buf),"%f bps",bps); snprintf(buf, sizeof(buf), "%f bps", bps);
} }
return buf; return buf;
} }
static void do_benchmarks(ssh_session session, struct argument_s *arguments, static void do_benchmarks(ssh_session session, struct argument_s *arguments,
const char *hostname){ const char *hostname)
float ping_rtt=0.0; {
float ssh_rtt=0.0; float ping_rtt = 0.0;
float bps=0.0; float ssh_rtt = 0.0;
float bps = 0.0;
int i; int i;
int err; int err;
struct benchmark *b; struct benchmark *b = NULL;
if(arguments->verbose>0) if (arguments->verbose > 0)
fprintf(stdout,"Testing ICMP RTT\n"); fprintf(stdout, "Testing ICMP RTT\n");
err=benchmarks_ping_latency(hostname, &ping_rtt);
if(err == 0){ err = benchmarks_ping_latency(hostname, &ping_rtt);
fprintf(stdout,"ping RTT : %f ms\n",ping_rtt); if (err == 0) {
fprintf(stdout, "ping RTT : %f ms\n", ping_rtt);
} }
err=benchmarks_ssh_latency(session, &ssh_rtt);
if(err==0){ err = benchmarks_ssh_latency(session, &ssh_rtt);
fprintf(stdout, "SSH RTT : %f ms. Theoretical max BW (win=128K) : %s\n",ssh_rtt,network_speed(128000.0/(ssh_rtt / 1000.0))); if (err == 0) {
fprintf(stdout,
"SSH RTT : %f ms. Theoretical max BW (win=128K) : %s\n",
ssh_rtt, network_speed(128000.0 / (ssh_rtt / 1000.0)));
} }
for (i=0 ; i<BENCHMARK_NUMBER ; ++i){
for (i=0; i < BENCHMARK_NUMBER; ++i){
b = &benchmarks[i]; b = &benchmarks[i];
if(b->enabled){ if (b->enabled) {
err=b->fct(session,arguments,&bps); err=b->fct(session, arguments, &bps);
if(err==0){
fprintf(stdout, "%s : %s : %s\n",hostname, b->name, network_speed(bps)); if (err == 0) {
fprintf(stdout,
"%s : %s : %s\n",
hostname, b->name, network_speed(bps));
} }
} }
} }
} }
char *buffer; char *buffer = NULL;
int main(int argc, char **argv){ int main(int argc, char **argv)
{
struct argument_s arguments; struct argument_s arguments;
ssh_session session; ssh_session session = NULL;
int i; int i;
arguments_init(&arguments); arguments_init(&arguments);
cmdline_parse(argc, argv, &arguments); cmdline_parse(argc, argv, &arguments);
if (arguments.nhosts==0){ if (arguments.nhosts == 0) {
fprintf(stderr,"At least one host (-h) must be specified\n"); fprintf(stderr, "At least one host (-h) must be specified\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (arguments.ntests==0){
for(i=0; i < BENCHMARK_NUMBER ; ++i){ if (arguments.ntests == 0) {
benchmarks[i].enabled=1; for (i=0; i < BENCHMARK_NUMBER; ++i) {
benchmarks[i].enabled = 1;
} }
arguments.ntests=BENCHMARK_NUMBER; arguments.ntests = BENCHMARK_NUMBER;
} }
buffer=malloc(arguments.chunksize > 1024 ? arguments.chunksize : 1024);
if(buffer == NULL){ buffer = malloc(arguments.chunksize > 1024 ? arguments.chunksize : 1024);
fprintf(stderr,"Allocation of chunk buffer failed\n"); if (buffer == NULL) {
fprintf(stderr, "Allocation of chunk buffer failed\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (arguments.verbose > 0){
if (arguments.verbose > 0) {
fprintf(stdout, "Will try hosts "); fprintf(stdout, "Will try hosts ");
for(i=0;i<arguments.nhosts;++i){ for (i=0; i < arguments.nhosts; ++i) {
fprintf(stdout,"\"%s\" ", arguments.hosts[i]); fprintf(stdout, "\"%s\" ", arguments.hosts[i]);
} }
fprintf(stdout,"with benchmarks ");
for(i=0;i<BENCHMARK_NUMBER;++i){ fprintf(stdout, "with benchmarks ");
if(benchmarks[i].enabled) for (i = 0; i < BENCHMARK_NUMBER; ++i) {
fprintf(stdout,"\"%s\" ",benchmarks[i].name); if (benchmarks[i].enabled)
fprintf(stdout, "\"%s\" ", benchmarks[i].name);
} }
fprintf(stdout,"\n"); fprintf(stdout,"\n");
} }
for(i=0; i<arguments.nhosts;++i){ for (i = 0; i < arguments.nhosts; ++i) {
if(arguments.verbose > 0) if (arguments.verbose > 0)
fprintf(stdout,"Connecting to \"%s\"...\n",arguments.hosts[i]); fprintf(stdout, "Connecting to \"%s\"...\n", arguments.hosts[i]);
session=connect_host(arguments.hosts[i], arguments.verbose, arguments.cipher);
if(session != NULL && arguments.verbose > 0) session = connect_host(arguments.hosts[i],
fprintf(stdout,"Success\n"); arguments.verbose,
if(session == NULL){ arguments.cipher);
fprintf(stderr,"Errors occurred, stopping\n"); if (session != NULL && arguments.verbose > 0)
fprintf(stdout, "Success\n");
if (session == NULL) {
fprintf(stderr, "Errors occurred, stopping\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
do_benchmarks(session, &arguments, arguments.hosts[i]); do_benchmarks(session, &arguments, arguments.hosts[i]);
ssh_disconnect(session); ssh_disconnect(session);
ssh_free(session); ssh_free(session);
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }