-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.hpp
85 lines (68 loc) · 2.53 KB
/
client.hpp
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
#include <thread>
#include <algorithm>
#include "net.hpp"
class Client {
private:
virtual string genReq() = 0;
void sendReq(string ip, vector<double> &handle_time, int id) {
system_clock::time_point start, end;
start = system_clock::now();
const SSL_METHOD *method = TLS_client_method();
SSL_CTX *ctx = SSL_CTX_new(method);
assert(ctx != NULL);
SSL *ssl = SSL_new(ctx);
int fd = connect_to_addr(ip);
SSL_set_fd(ssl, fd);
assert(SSL_connect(ssl) == 1);
// send request
string reqMsg = genReq();
assert(sendMsg(ssl, reqMsg));
// receive response
string response;
recvMsg(ssl, response);
end = system_clock::now();
handle_time[id] =
duration_cast<std::chrono::duration<double>>(end - start).count();
cout << id << " handle time: " << handle_time[id] << endl;
SSL_free(ssl);
close(fd);
SSL_CTX_free(ctx);
}
public:
Client() {}
void run(string ip, int offer_load, int time_unit) {
// init handle_time
vector<double> handle_time = vector<double>(offer_load, 0.0);
assert(handle_time.size() == offer_load);
// 1s
unsigned int sleep_period = 1000000 / 1 / offer_load * time_unit;
cout << "sleep period: " << sleep_period << endl;
vector<thread> t_vec;
system_clock::time_point start, end;
start = system_clock::now();
for (int i = 0; i < offer_load; i++) {
thread t(&Client::sendReq, this, ip, ref(handle_time), i);
t_vec.push_back(move(t));
usleep(sleep_period);
}
for (auto &th : t_vec) {
th.join();
}
end = system_clock::now();
cout
<< "total time spent: "
<< duration_cast<std::chrono::duration<double>>(end - start).count()
<< endl;
double total_handle_time = 0.0;
for (int i = 0; i < handle_time.size(); i++) {
assert(handle_time[i] != 0.0);
total_handle_time += handle_time[i];
}
sort(handle_time.begin(), handle_time.end());
int idx_50 = handle_time.size() * 0.5;
int idx_99 = handle_time.size() * 0.99 - 1;
cout << "50 percent, id: " << idx_50 << ", handle_time: " << handle_time[idx_50] << endl;
cout << "99 percent, id: " << idx_99 << ", handle_time: " << handle_time[idx_99] << endl;
cout << "avg handle time: " << total_handle_time / offer_load << endl;
}
};