aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmarks/bench_raw.c
blob: 62e1055de0e5d85847bea26769062a570c1b9caa (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * 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 "benchmarks.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define PYTHON_PATH "/usr/bin/python"

const char python_eater[]=
"#!/usr/bin/python\n"
"import sys\n"
"print 'go'\n"
"sys.stdout.flush()\n"
"toread=XXXXXXXXXX\n"
"read=0\n"
"while(read < toread):\n"
"    buffersize=toread-read\n"
"    if(buffersize > 4096):\n"
"        buffersize=4096\n"
"    r=len(sys.stdin.read(buffersize))\n"
"    read+=r\n"
"    if(r<=0):\n"
"        print 'error'\n"
"        exit()\n"
"print 'done'\n";

static char *get_python_eater(unsigned long bytes){
  char *eater=malloc(sizeof(python_eater));
  char *ptr;
  char buffer[12];

  memcpy(eater,python_eater,sizeof(python_eater));
  ptr=strstr(eater,"XXXXXXXXXX");
  if(!ptr){
    free(eater);
    return NULL;
  }
  sprintf(buffer,"0x%.8lx",bytes);
  memcpy(ptr,buffer,10);
  return eater;
}

/** @internal
 * @brief uploads a script (python or other) at a specific path on the
 * remote host
 * @param[in] session an active SSH session
 * @param[in] path to copy the file
 * @param[in] content of the file to copy
 * @return 0 on success, -1 on error
 */
static int upload_script(ssh_session session, const char *path,
    const char *script){
  ssh_channel channel;
  char cmd[128];
  int err;

  channel=ssh_channel_new(session);
  if(!channel)
    goto error;
  if(ssh_channel_open_session(channel) == SSH_ERROR)
    goto error;
  snprintf(cmd,sizeof(cmd),"cat > %s",path);
  if(ssh_channel_request_exec(channel,cmd) == SSH_ERROR)
    goto error;
  err=ssh_channel_write(channel,script,strlen(script));
  if(err == SSH_ERROR)
    goto error;
  if(ssh_channel_send_eof(channel) == SSH_ERROR)
    goto error;
  if(ssh_channel_close(channel) == SSH_ERROR)
    goto error;
  ssh_channel_free(channel);
  return 0;
error:
  fprintf(stderr,"Error while copying script : %s\n",ssh_get_error(session));
  return -1;
}

/** @internal
 * @brief benchmarks a raw upload (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_up (ssh_session session, struct argument_s *args,
    float *bps){
  unsigned long bytes=0x1000000;
  char *script;
  char cmd[128];
  static char buffer[0x10000];
  int err;
  ssh_channel channel;
  struct timestamp_struct ts;
  float ms=0.0;
  unsigned long total=0;

  if(args->data != 0)
    bytes = args->data * 1024 * 1024;
  script =get_python_eater(bytes);
  err=upload_script(session,"/tmp/eater.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/eater.py", PYTHON_PATH);
  if(ssh_channel_request_exec(channel,cmd)==SSH_ERROR)
    goto error;
  if((err=ssh_channel_read(channel,buffer,sizeof(buffer)-1,0))==SSH_ERROR)
    goto error;
  buffer[err]=0;
  if(!strstr(buffer,"go")){
    fprintf(stderr,"parse error : %s\n",buffer);
    ssh_channel_close(channel);
    ssh_channel_free(channel);
    return -1;
  }
  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 > 0x10000)
      towrite = 32758;
    w=ssh_channel_write(channel,buffer,towrite);
    if(w == SSH_ERROR)
      goto error;
    total += w;
  }

  if(args->verbose>0)
    fprintf(stdout,"Finished upload, now waiting the ack\n");

  if((err=ssh_channel_read(channel,buffer,sizeof(buffer)-1,0))==SSH_ERROR)
      goto error;
  buffer[err]=0;
  if(!strstr(buffer,"done")){
    fprintf(stderr,"parse error : %s\n",buffer);
    ssh_channel_close(channel);
    ssh_channel_free(channel);
    return -1;
  }
  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_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;
}

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;
  char cmd[128];
  static char buffer[0x10000];
  int err;
  ssh_channel channel;
  struct timestamp_struct ts;
  float ms=0.0;
  unsigned long total=0;

  if(args->data != 0)
    bytes = args->data * 1024 * 1024;
  script =get_python_giver(bytes);
  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;
}