aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmarks/benchmarks.c
blob: 647e5e7e193b2f01b5bf2e7a0e3d3a71584ca21d (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * This file is part of the SSH Library
 *
 * Copyright (c) 2010 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 "config.h"
#include "benchmarks.h"
#include <libssh/libssh.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

const char *libssh_benchmarks_names[]={
    "benchmark_raw_upload",
    "benchmark_raw_download"
};

#ifdef HAVE_ARGP_H
#include <argp.h>

const char *argp_program_version = "libssh benchmarks 2010-12-28";
const char *argp_program_bug_address = "Aris Adamantiadis <aris@0xbadc0de.be>";

static char **cmdline;

/* Program documentation. */
static char doc[] = "libssh benchmarks";


/* The options we understand. */
static struct argp_option options[] = {
  {
    .name  = "verbose",
    .key   = 'v',
    .arg   = NULL,
    .flags = 0,
    .doc   = "Make libssh benchmark more verbose",
    .group = 0
  },
  {
    .name  = "raw-upload",
    .key   = '1',
    .arg   = NULL,
    .flags = 0,
    .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',
    .arg   = "HOST",
    .flags = 0,
    .doc   = "Add a host to connect for benchmark (format user@hostname)",
    .group = 0
  },
  {NULL, 0, NULL, 0, NULL, 0}
};

/* Parse a single option. */
static error_t parse_opt (int key, char *arg, struct argp_state *state) {
  /* Get the input argument from argp_parse, which we
   * know is a pointer to our arguments structure.
   */
  struct argument_s *arguments = state->input;

  /* arg is currently not used */
  (void) arg;

  switch (key) {
    case '1':
    case '2':
      arguments->benchmarks[key - '1'] = 1;
      arguments->ntests ++;
      break;
    case 'v':
      arguments->verbose++;
      break;
    case 'h':
      if(arguments->nhosts >= MAX_HOSTS_CONNECT){
        fprintf(stderr, "Too much hosts\n");
        return ARGP_ERR_UNKNOWN;
      }
      arguments->hosts[arguments->nhosts]=arg;
      arguments->nhosts++;
      break;
    case ARGP_KEY_ARG:
      /* End processing here. */
      cmdline = &state->argv [state->next - 1];
      state->next = state->argc;
      break;
    default:
      return ARGP_ERR_UNKNOWN;
  }

  return 0;
}

/* Our argp parser. */
static struct argp argp = {options, parse_opt, NULL, doc, NULL, NULL, NULL};

#endif /* HAVE_ARGP_H */

static void cmdline_parse(int argc, char **argv, struct argument_s *arguments) {
  /*
   * Parse our arguments; every option seen by parse_opt will
   * be reflected in arguments.
   */
#ifdef HAVE_ARGP_H
  argp_parse(&argp, argc, argv, 0, 0, arguments);
#else /* HAVE_ARGP_H */
  (void) argc;
  (void) argv;
  (void) arguments;
#endif /* HAVE_ARGP_H */
}

static void arguments_init(struct argument_s *arguments){
  memset(arguments,0,sizeof(*arguments));
}

static ssh_session connect_host(const char *host, int verbose){
  ssh_session session=ssh_new();
  if(session==NULL)
    goto error;
  if(ssh_options_set(session,SSH_OPTIONS_HOST, host)<0)
    goto error;
  ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbose);
  if(ssh_connect(session)==SSH_ERROR)
    goto error;
  if(ssh_userauth_autopubkey(session,NULL) != SSH_AUTH_SUCCESS)
    goto error;
  return session;
error:
  fprintf(stderr,"Error connecting to \"%s\": %s\n",host,ssh_get_error(session));
  ssh_free(session);
  return NULL;
}

static char *network_speed(float bps){
  static char buffer[128];
  if(bps > 1000*1000*1000){
    /* Gbps */
    snprintf(buffer,sizeof(buffer),"%f Gbps",bps/(1000*1000*1000));
  } else if(bps > 1000*1000){
    /* Mbps */
    snprintf(buffer,sizeof(buffer),"%f Mbps",bps/(1000*1000));
  } else if(bps > 1000){
    snprintf(buffer,sizeof(buffer),"%f Kbps",bps/1000);
  } else {
    snprintf(buffer,sizeof(buffer),"%f bps",bps);
  }
  return buffer;
}

static void do_benchmarks(ssh_session session, struct argument_s *arguments,
    const char *hostname){
  float ping_rtt=0.0;
  float ssh_rtt=0.0;
  float bps=0.0;
  int err;

  if(arguments->verbose>0)
    fprintf(stdout,"Testing ICMP RTT\n");
  err=benchmarks_ping_latency(hostname, &ping_rtt);
  if(err == 0){
    fprintf(stdout,"ping RTT : %f ms\n",ping_rtt);
  }
  err=benchmarks_ssh_latency(session, &ssh_rtt);
  if(err==0){
    fprintf(stdout, "SSH RTT : %f ms\n",ssh_rtt);
  }
  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){
  struct argument_s arguments;
  ssh_session session;
  int i;

  arguments_init(&arguments);
  cmdline_parse(argc, argv, &arguments);
  if (arguments.nhosts==0){
    fprintf(stderr,"At least one host (-h) must be specified\n");
    return EXIT_FAILURE;
  }
  if (arguments.ntests==0){
    for(i=1; i < BENCHMARK_NUMBER ; ++i){
      arguments.benchmarks[i]=1;
    }
    arguments.ntests=BENCHMARK_NUMBER;
  }
  if (arguments.verbose > 0){
    fprintf(stdout, "Will try hosts ");
    for(i=0;i<arguments.nhosts;++i){
      fprintf(stdout,"\"%s\" ", arguments.hosts[i]);
    }
    fprintf(stdout,"with benchmarks ");
    for(i=0;i<BENCHMARK_NUMBER;++i){
      if(arguments.benchmarks[i])
        fprintf(stdout,"\"%s\" ",libssh_benchmarks_names[i]);
    }
    fprintf(stdout,"\n");
  }

  for(i=0; i<arguments.nhosts;++i){
    if(arguments.verbose > 0)
      fprintf(stdout,"Connecting to \"%s\"...\n",arguments.hosts[i]);
    session=connect_host(arguments.hosts[i], arguments.verbose);
    if(session != NULL && arguments.verbose > 0)
      fprintf(stdout,"Success\n");
    if(session == NULL){
      fprintf(stderr,"Errors occured, stopping\n");
      return EXIT_FAILURE;
    }
    do_benchmarks(session, &arguments, arguments.hosts[i]);
    ssh_disconnect(session);
    ssh_free(session);
  }
  return EXIT_SUCCESS;
}