-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.cpp
328 lines (291 loc) · 9.12 KB
/
client.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <arpa/inet.h>
#include <boost/program_options.hpp>
#include <errno.h>
#include <iostream>
#include <fstream>
#include <pthread.h>
#include <math.h>
#include <netdb.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string>
#include <vector>
#include "network.hpp"
namespace po = boost::program_options;
using namespace dm;
#define OUT_FILE "response_times.csv"
#define FILE_BUFSIZE 10
double secondsDiff(const timeval& val1, const timeval& val2);
struct clientArgs {
std::string host;
int port;
uint32_t size;
int count;
double timeout;
int msgCount;
int writeToFile;
std::ofstream out;
pthread_mutex_t fileMutex;
};
void* requestData(void* args)
{
std::pair<struct clientArgs*, int>* threadArgs
= (std::pair<struct clientArgs*, int>*) args;
struct clientArgs* ca = threadArgs->first;
int threadID = threadArgs->second;
threadID += 1;
delete threadArgs;
int i = 0;
char requestMsg[REQUEST_SIZE];
requestMsg[3] = (uint32_t) ((ca->size >> 24) & 0xFF);
requestMsg[2] = (uint32_t) ((ca->size >> 16) & 0xFF);
requestMsg[1] = (uint32_t) ((ca->size >> 8) & 0xFF);
requestMsg[0] = (uint32_t) ((ca->size & 0xFF));
char* responseMsg = new char[ca->size];
int bytesToRead = ca->size;
int flag = 0;
int sock;
struct sockaddr_in server;
struct hostent* hp;
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
exit(sockError("socket()", 0));
}
int arg = 1;
// set so port can be resused imemediately after ctrl-c
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) == -1)
{
exit(sockError("setsockopt()", 0));
}
// set up address structure
memset((char*) &server, 0, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(ca->port);
if (!(hp = gethostbyname(ca->host.c_str())))
{
std::cerr << "Error: unknown server address\n";
exit(1);
}
memcpy((char*) &server.sin_addr, hp->h_addr, hp->h_length);
// connect
if (connect(sock, (struct sockaddr*) &server, sizeof(server)))
{
exit(sockError("connect()", 0));
}
#ifdef __APPLE__
int set = 1;
if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*) &set, sizeof(int)))
{
exit(sockError("setsockopt()", 0));
}
#else
flag = MSG_NOSIGNAL;
#endif
struct timeval startTime;
if (gettimeofday(&startTime, NULL) == -1)
{
exit(sockError("gettimeofday()", 0));
}
struct timeval sendTime;
struct timeval recvTime;
double roundTrips[FILE_BUFSIZE];
size_t firstMsgNo = 1;
size_t lastMsgNo[FILE_BUFSIZE];
size_t msgInBuf = 0;
double requestTime = 0;
// transmit request and receive packets
for (i = 0; i < ca->count; i++)
{
if (ca->writeToFile && i % ca->msgCount == 0)
{
if (gettimeofday(&sendTime, NULL) == -1)
{
exit(sockError("gettimeofday()", 0));
}
}
if (send(sock, requestMsg, REQUEST_SIZE, flag) < 0)
{
perror("send() failed");
exit(1);
}
clearSocket(sock, responseMsg, bytesToRead);
if (ca->writeToFile &&
(i % ca->msgCount == ca->msgCount - 1 || i == ca->count - 1))
{
if (gettimeofday(&recvTime, NULL) == -1)
{
exit(sockError("gettimeofday()", 0));
}
requestTime = secondsDiff(sendTime, recvTime);
roundTrips[msgInBuf % FILE_BUFSIZE] = requestTime;
lastMsgNo[msgInBuf % FILE_BUFSIZE] = i + 1;
msgInBuf++;
if (requestTime >= ca->timeout)
{
i = ca->count -1;
std::cerr << "Server took too long to respond to request\n";
break;
}
if (msgInBuf % FILE_BUFSIZE == 0 || i == ca->count - 1)
{
pthread_mutex_lock(&ca->fileMutex);
for (size_t j = 0; j < msgInBuf; j++)
{
ca->out << threadID << "," << firstMsgNo << " to "
<< lastMsgNo[j] << "," << ca->size << ","
<< roundTrips[j] << "\n";
firstMsgNo = lastMsgNo[j] + 1;
}
pthread_mutex_unlock(&ca->fileMutex);
msgInBuf = 0;
}
}
}
close(sock);
struct timeval endTime;
if (gettimeofday(&endTime, NULL) == -1)
{
exit(sockError("gettimeofday()", 0));
}
double* timeToComplete = new double();
*timeToComplete = secondsDiff(startTime, endTime);
#ifdef DEBUG
std::cout << *timeToComplete << "\n";
#endif
delete responseMsg;
return timeToComplete;
}
void runClients(struct clientArgs* ca, int clients)
{
std::vector<pthread_t> threads;
threads.resize(clients);
struct rlimit rlim;
// increase allowed threads per process
getrlimit(RLIMIT_NPROC, &rlim);
rlim.rlim_cur = rlim.rlim_max;
setrlimit(RLIMIT_NPROC, &rlim);
int rtn = 0;
int i = 0;
double* timeToComplete = 0;
double totalTime = 0;
double averageTime = 0;
for (i = 0; i < clients; i++)
{
std::pair<struct clientArgs*, int>* args
= new std::pair<struct clientArgs*, int>();
args->first = ca;
args->second = i;
if ((rtn = pthread_create(&threads[i], NULL, &requestData,
(void*) args)))
{
if (rtn == EAGAIN)
{
std::cerr << "Could not create a new thread.\n";
}
perror("pthread_create()");
exit(1);
}
usleep(1000);
}
for (i = 0; i < clients; i++)
{
pthread_join(threads[i], (void**) &timeToComplete);
totalTime += *timeToComplete;
delete timeToComplete;
}
averageTime = totalTime / i;
std::cout << "Average connection time: " << averageTime << " seconds\n\n";
}
int main(int argc, char** argv)
{
int opt = 0;
double dopt = 0;
std::string option = "";
int clients = 0;
po::options_description desc("Allowed options");
desc.add_options()
("host,h", po::value<std::string>(&option)->default_value("localhost"),
"host to connect to")
("port,p", po::value<int>(&opt)->default_value(32000),
"port to use")
("message-size,s", po::value<int>(&opt)->default_value(1024),
"length of packets to request")
("message-count,c", po::value<int>(&opt)->default_value(250),
"number of packets to request")
("clients,x", po::value<int>(&opt)->default_value(250),
"number of clients to create")
("write-to-file,w", po::value<int>(&opt)->default_value(1),
"write response time to disk (0 to disable)")
("record-size,r", po::value<int>(&opt)->default_value(1),
"number of responses in each output record")
("timeout,t", po::value<double>(&dopt)->default_value(10),
"seconds in timeout")
("help", "show this message")
;
po::variables_map vm;
try
{
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
std::cerr << "\tuse --help to see program options\n";
return 1;
}
if (vm.count("help"))
{
std::cout << desc << "\n";
return 0;
}
struct clientArgs args;
args.host = vm["host"].as<std::string>();
args.port = vm["port"].as<int>();
args.size = vm["message-size"].as<int>();
args.count = vm["message-count"].as<int>();
args.timeout = vm["timeout"].as<double>();
args.msgCount = vm["record-size"].as<int>();
args.writeToFile = vm["write-to-file"].as<int>();
clients = vm["clients"].as<int>();
std::cout << "Host:\t\t\t" << args.host << "\n";
std::cout << "Port:\t\t\t" << args.port << "\n";
std::cout << "Message size:\t\t" << args.size << "\n";
std::cout << "Message count:\t\t" << args.count << "\n";
std::cout << "Number of clients:\t" << clients << "\n";
std::cout << "Write to file:\t\t" << args.writeToFile << "\n";
std::cout << "Record size:\t\t" << args.msgCount << "\n";
std::cout << "Seconds to wait:\t" << args.timeout << "\n";
args.out.open(OUT_FILE);
if (!args.out)
{
std::cerr << "unable to open \"" << OUT_FILE << "\"\n";
exit(1);
}
args.out << "Thread ID,Message Count,Message Size,Seconds\n";
if (pthread_mutex_init(&args.fileMutex, NULL))
{
std::cerr << "Error creating mutex\n";
exit(1);
}
runClients(&args, clients);
args.out.close();
return 0;
}
/**
* Finds the difference between val1 and val2 in seconds. Accurate to
* microseconds.
* @return The difference in seconds.
* @author Dean Morin
*/
double secondsDiff(const timeval& val1, const timeval& val2)
{
long sec1 = val1.tv_sec;
long sec2 = val2.tv_sec;
long usec1 = val1.tv_usec;
long usec2 = val2.tv_usec;
return abs(sec2 - sec1) + abs(usec2 - usec1) * pow(10, -6);
}