-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.cpp
192 lines (171 loc) · 6.91 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
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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <sys/time.h>
#include <sys/resource.h>
#endif
void client_test1(int thread_count, char const* host, char const* port,
size_t block_size, size_t session_count, int timeout);
void client_test2(int thread_count, char const* host, char const* port,
size_t block_size, size_t session_count, int timeout);
void server_test1(int thread_count, char const* host, char const* port,
size_t block_size);
void server_test2(int thread_count, char const* host, char const* port,
size_t block_size);
void socket_pair_test(size_t pair_count, size_t active_count,
size_t write_count);
void client_test3(int thread_count, char const* host, char const* port,
size_t total_count, size_t session_count);
void server_test3(int thread_count, char const* host, char const* port,
size_t total_count);
void posttask_test1(int thread_count, uint64_t post_count);
void posttask_test2(int thread_count, uint64_t post_count);
void posttask_test3(uint64_t post_count);
void posttask_test4(uint64_t post_count);
void posttask_test5(uint64_t post_count);
int usage() {
std::cerr << "Usage: asio_test socketpair <pair_count> <active_count>"
" <write_count>\n";
std::cerr << "Usage: asio_test client1 <host> <port> <threads>"
" <blocksize> <sessions> <time>\n";
std::cerr << "Usage: asio_test server1 <address> <port> <threads>"
" <blocksize>\n";
std::cerr << "Usage: asio_test client2 <host> <port> <threads>"
" <blocksize> <sessions> <time>\n";
std::cerr << "Usage: asio_test server2 <address> <port> <threads>"
" <blocksize>\n";
std::cerr << "Usage: asio_test client3 <host> <port> <threads>"
" <totalcount> <sessions>\n";
std::cerr << "Usage: asio_test server3 <address> <port> <threads>"
" <totalcount>\n";
std::cerr << "Usage: asio_test posttask1 <threads> <totalcount>\n";
std::cerr << "Usage: asio_test posttask2 <threads> <totalcount>\n";
std::cerr << "Usage: asio_test posttask3 <totalcount>\n";
std::cerr << "Usage: asio_test posttask4 <totalcount>\n";
std::cerr << "Usage: asio_test posttask5 <totalcount>\n";
return 1;
}
#ifdef _WIN32
void change_limit() {
}
#else
void change_limit() {
rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
std::cout << "getrlimit failed, error: " << strerror(errno) << "\n";
return;
}
if (rlim.rlim_cur < rlim.rlim_max) {
auto old_cur = rlim.rlim_cur;
rlim.rlim_cur = rlim.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
std::cout << "setrlimit failed, error: " << strerror(errno) <<
" " << std::to_string(old_cur) + " to " <<
std::to_string(rlim.rlim_max) << "\n";
} else {
std::cout << "setrlimit success: " << std::to_string(old_cur) <<
" to " << std::to_string(rlim.rlim_max) << "\n";
}
}
}
#endif
int main(int argc, char* argv[])
{
change_limit();
using namespace std; // For atoi.
if (argc < 2)
return usage();
if (!strcmp(argv[1], "socketpair") && argc != 5)
return usage();
if (!strcmp(argv[1], "client1") && argc != 8)
return usage();
if (!strcmp(argv[1], "server1") && argc != 6)
return usage();
if (!strcmp(argv[1], "client2") && argc != 8)
return usage();
if (!strcmp(argv[1], "server2") && argc != 6)
return usage();
if (!strcmp(argv[1], "client3") && argc != 7)
return usage();
if (!strcmp(argv[1], "server3") && argc != 6)
return usage();
if (!strcmp(argv[1], "posttask1") && argc != 4)
return usage();
if (!strcmp(argv[1], "posttask2") && argc != 4)
return usage();
if (!strcmp(argv[1], "posttask3") && argc != 3)
return usage();
if (!strcmp(argv[1], "posttask4") && argc != 3)
return usage();
if (!strcmp(argv[1], "posttask5") && argc != 3)
return usage();
if (!strcmp(argv[1], "socketpair")) {
size_t pair_count = atoi(argv[2]);
size_t active_count = atoi(argv[3]);
size_t write_count = atoi(argv[4]);
socket_pair_test(pair_count, active_count, write_count);
} else if (!strcmp(argv[1], "client1")) {
const char* host = argv[2];
const char* port = argv[3];
int thread_count = atoi(argv[4]);
size_t block_size = atoi(argv[5]);
size_t session_count = atoi(argv[6]);
int timeout = atoi(argv[7]);
client_test1(thread_count, host, port, block_size, session_count, timeout);
} else if (!strcmp(argv[1], "client2")) {
const char* host = argv[2];
const char* port = argv[3];
int thread_count = atoi(argv[4]);
size_t block_size = atoi(argv[5]);
size_t session_count = atoi(argv[6]);
int timeout = atoi(argv[7]);
client_test2(thread_count, host, port, block_size, session_count, timeout);
} else if (!strcmp(argv[1], "server1")) {
const char* host = argv[2];
const char* port = argv[3];
int thread_count = atoi(argv[4]);
size_t block_size = atoi(argv[5]);
server_test1(thread_count, host, port, block_size);
} else if (!strcmp(argv[1], "server2")) {
const char* host = argv[2];
const char* port = argv[3];
int thread_count = atoi(argv[4]);
size_t block_size = atoi(argv[5]);
server_test2(thread_count, host, port, block_size);
} else if (!strcmp(argv[1], "client3")) {
const char* host = argv[2];
const char* port = argv[3];
int thread_count = atoi(argv[4]);
uint32_t total_count = atoi(argv[5]);
size_t session_count = atoi(argv[6]);
client_test3(thread_count, host, port, total_count, session_count);
} else if (!strcmp(argv[1], "server3")) {
const char* host = argv[2];
const char* port = argv[3];
int thread_count = atoi(argv[4]);
uint32_t total_count = atoi(argv[5]);
server_test3(thread_count, host, port, total_count);
} else if (!strcmp(argv[1], "posttask1")) {
int thread_count = atoi(argv[2]);
uint64_t total_count = strtoull(argv[3], nullptr, 10);
posttask_test1(thread_count, total_count);
} else if (!strcmp(argv[1], "posttask2")) {
int thread_count = atoi(argv[2]);
uint64_t total_count = strtoull(argv[3], nullptr, 10);
posttask_test2(thread_count, total_count);
} else if (!strcmp(argv[1], "posttask3")) {
uint64_t total_count = strtoull(argv[2], nullptr, 10);
posttask_test3(total_count);
} else if (!strcmp(argv[1], "posttask4")) {
uint64_t total_count = strtoull(argv[2], nullptr, 10);
posttask_test4(total_count);
} else if (!strcmp(argv[1], "posttask5")) {
uint64_t total_count = strtoull(argv[2], nullptr, 10);
posttask_test5(total_count);
} else {
return usage();
}
return 0;
}