-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
79 lines (64 loc) · 2.02 KB
/
main.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
#include <iostream>
#include <thread>
#include "Worker.h"
#include "Timer.h"
using namespace std;
u_int _NUM_THREADS = 3;
u_short _NF_PORT = 9995;
int main(int argc, char *argv[]) {
u_int num_threads_ = _NUM_THREADS;
u_short port_ = _NF_PORT;
char *dest_;
mutex mtx_;
atomic_ulong requests_(0);
// handle options
if (argc < 2) {
cout << "Usage: " << argv[0] << " [-t num_threads (" << _NUM_THREADS << ")] [-p port (" << _NF_PORT
<< ")] target_ip" << endl;
return -1;
}
int opt;
char *endptr;
while ((opt = getopt(argc, argv, "t:p:")) != EOF) {
switch (opt) {
case 't':
num_threads_ = (u_int) strtol(optarg, &endptr, 0);
if (num_threads_ == 0) {
cout << "Invalid num_threads: " << endptr << endl;
return EXIT_FAILURE;
}
break;
case 'p':
port_ = (u_int) strtol(optarg, &endptr, 0);
if (port_ == 0) {
cout << "Invalid port: " << endptr << endl;
return EXIT_FAILURE;
}
break;
default:
cout << "Usage: " << argv[0] << " [-t num_threads (" << _NUM_THREADS << ")] [-p port (" << _NF_PORT
<< ")] target_ip" << endl;
return EXIT_FAILURE;
}
}
if (!argv[optind]) {
cout << "Error: destination missing" << endl;
return EXIT_FAILURE;
} else {
dest_ = argv[optind];
}
// create worker threads
thread *thWorker;
thWorker = new thread[num_threads_];
for (u_int i = 0; i < num_threads_; i++) {
thWorker[i] = thread(&Worker::Run, Worker(dest_, port_, &mtx_, &requests_));
}
// timer thread
cout << "Timestamp, Requests/s" << endl;
thread th_timer(&Timer::Start, Timer(&requests_));
// run workers
for (u_int i = 0; i < num_threads_; i++) {
thWorker[i].join();
}
return EXIT_SUCCESS;
}