|
1 |
| -#include <iostream> |
| 1 | +#include <algorithm> |
| 2 | +#include <atomic> |
| 3 | +#include <condition_variable> |
2 | 4 | #include <cstring>
|
3 |
| -#include <netinet/in.h> |
4 |
| -#include <sys/socket.h> |
5 |
| -#include <arpa/inet.h> |
6 |
| -#include <netdb.h> |
7 |
| -#include <stdio.h> |
8 |
| -#include <stdlib.h> |
9 |
| -#include <unistd.h> |
10 |
| - |
11 |
| - |
| 5 | +#include <iostream> |
12 | 6 | #include <memory>
|
| 7 | +#include <mutex> |
13 | 8 | #include <thread>
|
14 | 9 | #include <vector>
|
15 |
| -#include <fcntl.h> |
16 |
| - |
17 | 10 |
|
| 11 | +#include <arpa/inet.h> |
18 | 12 | #include <errno.h>
|
19 |
| -#include <sys/types.h> |
20 |
| -#include <time.h> |
21 | 13 | #include <fcntl.h>
|
22 |
| -#include <sys/wait.h> |
23 |
| -#include <signal.h> |
24 |
| -#include <thread> |
25 |
| -#include <vector> |
26 |
| -#include <mutex> |
27 |
| -#include <condition_variable> |
28 |
| -#include <algorithm> |
29 |
| -#include <memory> |
30 |
| -#include <atomic> |
31 |
| -#include "client_message.pb.h" |
32 |
| -#include "client_thread.h" |
33 |
| - |
34 |
| - |
35 | 14 | #include <google/protobuf/io/coded_stream.h>
|
36 | 15 | #include <google/protobuf/io/zero_copy_stream_impl.h>
|
37 | 16 | #include <google/protobuf/text_format.h>
|
| 17 | +#include <netdb.h> |
| 18 | +#include <netinet/in.h> |
| 19 | +#include <signal.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <sys/socket.h> |
| 23 | +#include <sys/types.h> |
| 24 | +#include <sys/wait.h> |
| 25 | +#include <time.h> |
| 26 | +#include <unistd.h> |
38 | 27 |
|
| 28 | +#include "client_message.pb.h" |
| 29 | +#include "client_thread.h" |
39 | 30 | #include "shared.h"
|
40 | 31 |
|
41 |
| -int nb_clients = -1; |
42 |
| -int port = -1; |
| 32 | +int nb_clients = -1; |
| 33 | +int port = -1; |
43 | 34 | int nb_messages = -1;
|
44 | 35 |
|
45 | 36 | uint64_t last_result = 0;
|
46 | 37 |
|
47 |
| -std::string _string = "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll"; |
48 |
| -std::atomic<uint32_t> global_number{0}; |
49 |
| - |
50 |
| - |
51 |
| - |
52 |
| -std::unique_ptr<char[]> get_operation(size_t& size) { |
53 |
| - static int i = 0; |
54 |
| - static int j = 1; |
55 |
| - if (j > 100) { |
56 |
| - j = 0; |
57 |
| - |
58 |
| - } |
59 |
| - |
60 |
| - sockets::client_msg _msg; |
61 |
| - if (i % 3 == 0) { |
62 |
| - i++; |
63 |
| - for (int k = 0; k < j; k++) { |
64 |
| - sockets::client_msg::OperationData* op = _msg.add_ops(); |
65 |
| - op->set_argument(1); |
66 |
| - global_number.fetch_add(1); |
67 |
| - op->set_type(sockets::client_msg::ADD); |
68 |
| - |
69 |
| - } |
70 |
| - j++; |
71 |
| - |
72 |
| - std::string msg_str; |
73 |
| - _msg.SerializeToString(&msg_str); |
74 |
| - char number[4]; |
75 |
| - size_t sz = msg_str.size(); |
76 |
| - convertIntToByteArray(number, sz); |
77 |
| - std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz+4); |
78 |
| - ::memcpy(buf.get(), number, 4); |
79 |
| - |
80 |
| - ::memcpy(buf.get()+4, msg_str.c_str(), sz); |
81 |
| - size = sz + 4; |
82 |
| - |
83 |
| - return std::move(buf); |
84 |
| - |
85 |
| - |
| 38 | +std::string _string = |
| 39 | + "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll" |
| 40 | + "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll" |
| 41 | + "llllllllllllllllllll"; |
| 42 | +std::atomic<uint32_t> global_number {0}; |
| 43 | + |
| 44 | +std::unique_ptr<char[]> get_operation(size_t & size) { |
| 45 | + static int i = 0; |
| 46 | + static int j = 1; |
| 47 | + if (j > 100) { |
| 48 | + j = 0; |
| 49 | + } |
| 50 | + |
| 51 | + sockets::client_msg _msg; |
| 52 | + if (i % 3 == 0) { |
| 53 | + i++; |
| 54 | + for (int k = 0; k < j; k++) { |
| 55 | + sockets::client_msg::OperationData * op = _msg.add_ops(); |
| 56 | + op->set_argument(1); |
| 57 | + global_number.fetch_add(1); |
| 58 | + op->set_type(sockets::client_msg::ADD); |
86 | 59 | }
|
87 |
| - else if (i % 3 == 1){ |
88 |
| - i++; |
89 |
| - for (int k = 0; k < j; k++) { |
90 |
| - sockets::client_msg::OperationData* op = _msg.add_ops(); |
91 |
| - op->set_argument(1); |
92 |
| - global_number.fetch_sub(1); |
93 |
| - op->set_type(sockets::client_msg::SUB); |
94 |
| - |
95 |
| - } |
96 |
| - j++; |
97 |
| - |
98 |
| - std::string msg_str; |
99 |
| - _msg.SerializeToString(&msg_str); |
100 |
| - |
101 |
| - char number[4]; |
102 |
| - size_t sz = msg_str.size(); |
103 |
| - convertIntToByteArray(number, sz); |
104 |
| - std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz+4); |
105 |
| - ::memcpy(buf.get(), number, 4); |
106 |
| - ::memcpy(buf.get()+4, msg_str.c_str(), sz); |
107 |
| - size = sz + 4; |
108 |
| - return std::move(buf); |
109 |
| - |
110 |
| - |
| 60 | + j++; |
| 61 | + |
| 62 | + std::string msg_str; |
| 63 | + _msg.SerializeToString(&msg_str); |
| 64 | + char number[4]; |
| 65 | + size_t sz = msg_str.size(); |
| 66 | + convertIntToByteArray(number, sz); |
| 67 | + std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz + 4); |
| 68 | + ::memcpy(buf.get(), number, 4); |
| 69 | + |
| 70 | + ::memcpy(buf.get() + 4, msg_str.c_str(), sz); |
| 71 | + size = sz + 4; |
| 72 | + |
| 73 | + return std::move(buf); |
| 74 | + |
| 75 | + } else if (i % 3 == 1) { |
| 76 | + i++; |
| 77 | + for (int k = 0; k < j; k++) { |
| 78 | + sockets::client_msg::OperationData * op = _msg.add_ops(); |
| 79 | + op->set_argument(1); |
| 80 | + global_number.fetch_sub(1); |
| 81 | + op->set_type(sockets::client_msg::SUB); |
111 | 82 | }
|
112 |
| - else { |
113 |
| - i++; |
114 |
| - for (int k = 0; k < j; k++) { |
115 |
| - sockets::client_msg::OperationData* op = _msg.add_ops(); |
116 |
| - op->set_random_data(_string); |
117 |
| - op->set_type(sockets::client_msg::RANDOM_DATA); |
118 |
| - |
119 |
| - } |
120 |
| - j++; |
121 |
| - |
122 |
| - std::string msg_str; |
123 |
| - _msg.SerializeToString(&msg_str); |
124 |
| - |
125 |
| - char number[4]; |
126 |
| - size_t sz = msg_str.size(); |
127 |
| - convertIntToByteArray(number, sz); |
128 |
| - std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz+4); |
129 |
| - ::memcpy(buf.get(), number, 4); |
130 |
| - ::memcpy(buf.get()+4, msg_str.c_str(), sz); |
131 |
| - size = sz + 4; |
132 |
| - return std::move(buf); |
133 |
| - |
134 |
| - |
135 |
| - |
| 83 | + j++; |
| 84 | + |
| 85 | + std::string msg_str; |
| 86 | + _msg.SerializeToString(&msg_str); |
| 87 | + |
| 88 | + char number[4]; |
| 89 | + size_t sz = msg_str.size(); |
| 90 | + convertIntToByteArray(number, sz); |
| 91 | + std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz + 4); |
| 92 | + ::memcpy(buf.get(), number, 4); |
| 93 | + ::memcpy(buf.get() + 4, msg_str.c_str(), sz); |
| 94 | + size = sz + 4; |
| 95 | + return std::move(buf); |
| 96 | + |
| 97 | + } else { |
| 98 | + i++; |
| 99 | + for (int k = 0; k < j; k++) { |
| 100 | + sockets::client_msg::OperationData * op = _msg.add_ops(); |
| 101 | + op->set_random_data(_string); |
| 102 | + op->set_type(sockets::client_msg::RANDOM_DATA); |
136 | 103 | }
|
137 |
| - |
138 |
| - |
| 104 | + j++; |
| 105 | + |
| 106 | + std::string msg_str; |
| 107 | + _msg.SerializeToString(&msg_str); |
| 108 | + |
| 109 | + char number[4]; |
| 110 | + size_t sz = msg_str.size(); |
| 111 | + convertIntToByteArray(number, sz); |
| 112 | + std::unique_ptr<char[]> buf = std::make_unique<char[]>(sz + 4); |
| 113 | + ::memcpy(buf.get(), number, 4); |
| 114 | + ::memcpy(buf.get() + 4, msg_str.c_str(), sz); |
| 115 | + size = sz + 4; |
| 116 | + return std::move(buf); |
| 117 | + } |
139 | 118 | }
|
140 | 119 |
|
141 |
| - |
142 |
| - |
143 |
| - |
144 |
| - |
145 |
| - |
146 | 120 | void client() {
|
147 |
| - class client_thread c_thread; |
148 |
| - |
149 |
| - c_thread.connect_to_the_server(port, "localhost"); |
150 |
| - uint64_t bytes_sent = 0; |
151 |
| - int iterations = nb_messages; |
152 |
| - while (iterations > 0) { |
153 |
| - size_t size = 0; |
154 |
| - std::unique_ptr<char[]> buf = get_operation(size); |
155 |
| - c_thread.sent_request(buf.get(), size); |
156 |
| - iterations--; |
157 |
| - } |
| 121 | + class client_thread c_thread; |
| 122 | + |
| 123 | + c_thread.connect_to_the_server(port, "localhost"); |
| 124 | + uint64_t bytes_sent = 0; |
| 125 | + int iterations = nb_messages; |
| 126 | + while (iterations > 0) { |
| 127 | + size_t size = 0; |
| 128 | + std::unique_ptr<char[]> buf = get_operation(size); |
| 129 | + c_thread.sent_request(buf.get(), size); |
| 130 | + iterations--; |
| 131 | + } |
158 | 132 | }
|
159 | 133 |
|
| 134 | +int main(int args, char * argv[]) { |
| 135 | + if (args < 5) { |
| 136 | + std::cerr |
| 137 | + << "usage: ./client <nb_threads> <hostname> <port> <nb_messages>\n"; |
| 138 | + exit(-1); |
| 139 | + } |
160 | 140 |
|
161 |
| -int main (int args, char* argv[]) { |
162 |
| - |
163 |
| - if (args < 5) { |
164 |
| - std::cerr << "usage: ./client <nb_threads> <hostname> <port> <nb_messages>\n"; |
165 |
| - exit(-1); |
166 |
| - } |
167 |
| - |
168 |
| - nb_clients = std::atoi(argv[1]); |
169 |
| - port = std::atoi(argv[3]); |
170 |
| - nb_messages = std::atoi(argv[4]); |
171 |
| - |
| 141 | + nb_clients = std::atoi(argv[1]); |
| 142 | + port = std::atoi(argv[3]); |
| 143 | + nb_messages = std::atoi(argv[4]); |
172 | 144 |
|
173 |
| - // creating the client threads |
174 |
| - std::vector<std::thread> threads; |
| 145 | + // creating the client threads |
| 146 | + std::vector<std::thread> threads; |
175 | 147 |
|
176 |
| - for (size_t i = 0; i < nb_clients; i++) { |
| 148 | + for (size_t i = 0; i < nb_clients; i++) { |
| 149 | + threads.emplace_back(std::thread(client)); |
| 150 | + } |
177 | 151 |
|
178 |
| - threads.emplace_back(std::thread(client)); |
179 |
| - } |
180 |
| - |
181 |
| - for (auto& thread : threads) { |
182 |
| - thread.join(); |
183 |
| - } |
| 152 | + for (auto & thread : threads) { |
| 153 | + thread.join(); |
| 154 | + } |
184 | 155 |
|
185 |
| - std::cout << "** all threads joined **\n"; |
| 156 | + std::cout << "** all threads joined **\n"; |
186 | 157 | }
|
0 commit comments