Debian Codegraph @

tests/benchmarks/latency.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2010 by Aris Adamantiadis
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "benchmarks.h"
23 #include <libssh/libssh.h>
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <sys/time.h>
30
31 #define PING_PROGRAM "/bin/ping"
32
33 /** @internal
34 * @brief Calculates the RTT of the host with ICMP ping, and returns the
35 * average of the calculated RTT.
36 * @param[in] host hostname to ping.
37 * @param[out] average average RTT in milliseconds.
38 * @returns 0 on success, -1 if there is an error.
39 * @warning relies on an external ping program which may not exist on
40 * certain OS.
41 */
42 int benchmarks_ping_latency (const char *host, float *average){
43 const char *ptr;
44 char cmd[256];
45 char line[1024];
46 FILE *fd;
47 int found=0;
48
49 /* strip out the hostname */
50 ptr=strchr(host,'@');
51 if(ptr)
52 ptr++;
53 else
54 ptr=host;
55
56 snprintf(cmd,sizeof(cmd),"%s -n -q -c3 %s",PING_PROGRAM, ptr);
57 fd=popen(cmd,"r");
58 if(fd==NULL){
59 fprintf(stderr,"Error executing command : %s\n", strerror(errno));
60 return -1;
61 }
62
63 while(!found && fgets(line,sizeof(line),fd)!=NULL){
64 if(strstr(line,"rtt")){
65 ptr=strchr(line,'=');
66 if(ptr==NULL)
67 goto parseerror;
68 ptr=strchr(ptr,'/');
69 if(ptr==NULL)
70 goto parseerror;
71 *average=strtof(ptr+1,NULL);
72 found=1;
73 break;
74 }
75 }
76 if(!found)
77 goto parseerror;
78 pclose(fd);
79 return 0;
80
81 parseerror:
82 fprintf(stderr,"Parse error : couldn't locate average in %s",line);
83 pclose(fd);
84 return -1;
85 }
86
87 /** @internal
88 * @brief initialize a timestamp to the current time.
89 * @param[out] ts A timestamp_struct pointer.
90 */
91 void timestamp_init(struct timestamp_struct *ts){
92 gettimeofday(&ts->timestamp,NULL);
93 }
94
95 /** @internal
96 * @brief return the elapsed time since now and the moment ts was initialized.
97 * @param[in] ts An initialized timestamp_struct pointer.
98 * @return Elapsed time in milliseconds.
99 */
100 float elapsed_time(struct timestamp_struct *ts){
101 struct timeval now;
102 time_t secdiff;
103 long usecdiff; /* may be negative */
104
105 gettimeofday(&now,NULL);
106 secdiff=now.tv_sec - ts->timestamp.tv_sec;
107 usecdiff=now.tv_usec - ts->timestamp.tv_usec;
108 //printf("%d sec diff, %d usec diff\n",secdiff, usecdiff);
109 return (float) (secdiff*1000) + ((float)usecdiff)/1000;
110 }
111
112 /** @internal
113 * @brief Calculates the RTT of the host with SSH channel operations, and
114 * returns the average of the calculated RTT.
115 * @param[in] session active SSH session to test.
116 * @param[out] average average RTT in milliseconds.
117 * @returns 0 on success, -1 if there is an error.
118 */
119 int benchmarks_ssh_latency(ssh_session session, float *average){
120 float times[3];
121 struct timestamp_struct ts;
122 int i;
123 ssh_channel channel;
124 channel=ssh_channel_new(session);
125 if(channel==NULL)
126 goto error;
127 if(ssh_channel_open_session(channel)==SSH_ERROR)
128 goto error;
129
130 for(i=0;i<3;++i){
131 timestamp_init(&ts);
132 if(ssh_channel_request_env(channel,"TEST","test")==SSH_ERROR &&
133 ssh_get_error_code(session)==SSH_FATAL)
134 goto error;
135 times[i]=elapsed_time(&ts);
136 }
137 ssh_channel_close(channel);
138 ssh_channel_free(channel);
139 channel=NULL;
140 printf("SSH request times : %f ms ; %f ms ; %f ms\n", times[0], times[1], times[2]);
141 *average=(times[0]+times[1]+times[2])/3;
142 return 0;
143 error:
144 fprintf(stderr,"Error calculating SSH latency : %s\n",ssh_get_error(session));
145 if(channel)
146 ssh_channel_free(channel);
147 return -1;
148 }