Debian Codegraph @

tests/benchmarks/bench_scp.c

raw
1 /* bench_scp.c
2 *
3 * This file is part of the SSH Library
4 *
5 * Copyright (c) 2011 by Aris Adamantiadis
6 *
7 * The SSH Library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at your
10 * option) any later version.
11 *
12 * The SSH Library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the SSH Library; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * MA 02111-1307, USA.
21 */
22
23 #include "benchmarks.h"
24 #include <libssh/libssh.h>
25 #include <stdio.h>
26
27 #define SCPDIR "/tmp/"
28 #define SCPFILE "scpbenchmark"
29
30 /** @internal
31 * @brief benchmarks a scp upload using an
32 * existing SSH session.
33 * @param[in] session Open SSH session
34 * @param[in] args Parsed command line arguments
35 * @param[out] bps The calculated bytes per second obtained via benchmark.
36 * @return 0 on success, -1 on error.
37 */
38 int benchmarks_scp_up (ssh_session session, struct argument_s *args,
39 float *bps){
40 unsigned long bytes;
41 struct timestamp_struct ts;
42 float ms=0.0;
43 unsigned long total=0;
44 ssh_scp scp;
45
46 bytes = args->datasize * 1024 * 1024;
47 scp = ssh_scp_new(session,SSH_SCP_WRITE,SCPDIR);
48 if(scp == NULL)
49 goto error;
50 if(ssh_scp_init(scp)==SSH_ERROR)
51 goto error;
52 if(ssh_scp_push_file(scp,SCPFILE,bytes,0777) != SSH_OK)
53 goto error;
54 if(args->verbose>0)
55 fprintf(stdout,"Starting upload of %lu bytes now\n",bytes);
56 timestamp_init(&ts);
57 while(total < bytes){
58 unsigned long towrite = bytes - total;
59 int w;
60 if(towrite > args->chunksize)
61 towrite = args->chunksize;
62 w=ssh_scp_write(scp,buffer,towrite);
63 if(w == SSH_ERROR)
64 goto error;
65 total += towrite;
66 }
67 ms=elapsed_time(&ts);
68 *bps=8000 * (float)bytes / ms;
69 if(args->verbose > 0)
70 fprintf(stdout,"Upload took %f ms for %lu bytes, at %f bps\n",ms,
71 bytes,*bps);
72 ssh_scp_close(scp);
73 ssh_scp_free(scp);
74 return 0;
75 error:
76 fprintf(stderr,"Error during scp upload : %s\n",ssh_get_error(session));
77 if(scp){
78 ssh_scp_close(scp);
79 ssh_scp_free(scp);
80 }
81 return -1;
82 }
83
84 /** @internal
85 * @brief benchmarks a scp download using an
86 * existing SSH session.
87 * @param[in] session Open SSH session
88 * @param[in] args Parsed command line arguments
89 * @param[out] bps The calculated bytes per second obtained via benchmark.
90 * @return 0 on success, -1 on error.
91 */
92 int benchmarks_scp_down (ssh_session session, struct argument_s *args,
93 float *bps){
94 unsigned long bytes;
95 struct timestamp_struct ts;
96 float ms=0.0;
97 unsigned long total=0;
98 ssh_scp scp;
99 int r;
100 size_t size;
101
102 bytes = args->datasize * 1024 * 1024;
103 scp = ssh_scp_new(session,SSH_SCP_READ,SCPDIR SCPFILE);
104 if(scp == NULL)
105 goto error;
106 if(ssh_scp_init(scp)==SSH_ERROR)
107 goto error;
108 r=ssh_scp_pull_request(scp);
109 if(r == SSH_SCP_REQUEST_NEWFILE){
110 size=ssh_scp_request_get_size(scp);
111 if(bytes > size){
112 printf("Only %zd bytes available (on %lu requested).\n",size,bytes);
113 bytes = size;
114 }
115 if(size > bytes){
116 printf("File is %zd bytes (on %lu requested). Will cut the end\n",size,bytes);
117 }
118 if(args->verbose>0)
119 fprintf(stdout,"Starting download of %lu bytes now\n",bytes);
120 timestamp_init(&ts);
121 ssh_scp_accept_request(scp);
122 while(total < bytes){
123 unsigned long toread = bytes - total;
124 if(toread > args->chunksize)
125 toread = args->chunksize;
126 r=ssh_scp_read(scp,buffer,toread);
127 if(r == SSH_ERROR || r == 0)
128 goto error;
129 total += r;
130 }
131 ms=elapsed_time(&ts);
132 *bps=8000 * (float)bytes / ms;
133 if(args->verbose > 0)
134 fprintf(stdout,"download took %f ms for %lu bytes, at %f bps\n",ms,
135 bytes,*bps);
136 } else {
137 fprintf(stderr,"Expected SSH_SCP_REQUEST_NEWFILE, got %d\n",r);
138 goto error;
139 }
140 ssh_scp_close(scp);
141 ssh_scp_free(scp);
142 return 0;
143 error:
144 fprintf(stderr,"Error during scp download : %s\n",ssh_get_error(session));
145 if(scp){
146 ssh_scp_close(scp);
147 ssh_scp_free(scp);
148 }
149 return -1;
150 }