-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserprogram_latency_checker.cpp
More file actions
381 lines (317 loc) · 11.6 KB
/
userprogram_latency_checker.cpp
File metadata and controls
381 lines (317 loc) · 11.6 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* This program will create 3 clients and do a read and a write operation with each of them cuncurrently.
* Please set the varaiable servers before use
*/
#include "client.h"
#include "commons.h"
#include <cstdio>
#include <string>
#include <vector>
#include <ctime>
#include <thread>
#include <iostream>
using namespace std;
typedef unsigned int uint;
//#define NUMBER_OF_CLIENTS 1
int num_secs = 1;
// Define your server information here
// static struct Server_info servers[] = {
// {"127.0.0.1", 10000},{"127.0.0.1", 10001}, {"127.0.0.1", 10002}, {"127.0.0.1", 10003}, {"127.0.0.1", 10004}};
// static struct Server_info servers[] = {
// {"127.0.0.1", 10000},{"127.0.0.1", 10001}, {"127.0.0.1", 10002}};
// static struct Server_info servers[] = {
// {"34.86.218.217", 10000},{"34.70.0.254", 10001},{"34.76.84.19",10002},{"104.155.208.169",10003},{"35.228.78.135",10004}};
static struct Server_info servers[] = {
{"34.74.37.137", 10000},{"35.188.106.113", 10001},{"35.195.226.75",10002}};
static char key[] = "123456"; // We only have one key in this userprogram
namespace Thread_helper{
void _put(const struct Client* c, const char* key, uint32_t key_size, const char* value, uint32_t value_size, uint64_t *latency){
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int status = put(c, key, key_size, value, value_size);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
*latency = std::chrono::duration_cast<std::chrono::milliseconds> (end - begin).count();
if(status == 0){ // Success
return;
}
else{
exit(-1);
}
return;
}
void _get(const struct Client* c, const char* key, uint32_t key_size, char** value, uint32_t *value_size, uint64_t *latency){
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int status = get(c, key, key_size, value, value_size);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
*latency = std::chrono::duration_cast<std::chrono::milliseconds> (end - begin).count();
if(status == 0){ // Success
return;
}
else{
exit(-1);
}
return;
}
}
void calculate_read_and_write_latency(char * latency_type, uint64_t *latency_value, string protocol){
uint64_t write_count = 0;
uint64_t read_count = 0;
uint64_t read_sum = 0;
uint64_t write_sum = 0;
if(protocol == "ABD") {
for (int i = 0; i< num_secs*ABD_NUMBER_OF_CLIENTS;i++) {
if(latency_type[i] == 'W') {
write_count++;
//cout<<write_count<<" ";
write_sum += latency_value[i];
} else {
read_count++;
read_sum += latency_value[i];
}
}
}
if(protocol == "CM") {
for (int i = 0; i< CM_NUMBER_OF_CLIENTS;i++) {
if(latency_type[i] == 'W') {
write_count++;
//cout<<write_count<<" ";
write_sum += latency_value[i];
} else {
read_count++;
read_sum += latency_value[i];
}
}
}
if(protocol == "MP") {
for (int i = 0; i< num_secs*MP_NUMBER_OF_CLIENTS;i++) {
if(latency_type[i] == 'W') {
write_count++;
//cout<<write_count<<" ";
write_sum += latency_value[i];
} else {
read_count++;
read_sum += latency_value[i];
}
}
}
cout<<" read_count :"<<read_count<<endl;
//cout<<"read_sum :"<<read_sum<<endl;
cout<<" write_count :"<<write_count<<endl;
//cout<<"write_sum"<<write_sum<<endl;
cout<<" Get latency :";
if(read_count==0) {
cout<<"0ms"<<endl;
} else{
cout<<read_sum/read_count<<"ms"<<endl;
}
cout<<" Put latency :";
if(write_count ==0){
cout<<"0ms"<<endl;
} else {
cout<< write_sum/write_count<<"ms"<<endl;
}
cout<<"\n";
}
// if read_right_ratio is 10% then value is 1
void abd_latency_executer(int read_write_ratio) {
struct Client* abd_clt[ABD_NUMBER_OF_CLIENTS];
char latency_type[num_secs*ABD_NUMBER_OF_CLIENTS];
uint64_t latency_value[num_secs*ABD_NUMBER_OF_CLIENTS]={0};
int counter = 0;
cout<<"Read_write_ratio: "<< read_write_ratio*10 <<endl;
for(uint i = 0; i < ABD_NUMBER_OF_CLIENTS; i++){
abd_clt[i] = client_instance(i, "ABD", servers, sizeof(servers) / sizeof(struct Server_info));
if(abd_clt[i] == NULL){
fprintf(stderr, "%s\n", "Error occured in creating clients");
return;
}
}
std::vector<std::thread*> threads;
srand(time(0));
char* values[num_secs*ABD_NUMBER_OF_CLIENTS];
uint32_t value_sizes[num_secs*ABD_NUMBER_OF_CLIENTS];
for (int j = 0; j< num_secs;j++) {
//std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
for(int i = 0; i < ABD_NUMBER_OF_CLIENTS; i++) {
// build a random value
int prob = rand() % 10;
if (prob >= read_write_ratio) {
latency_type[counter] = 'W';
char value[SIZE_OF_VALUE] = {0};
for(int i = 0; i < SIZE_OF_VALUE; i++){
value[i] = '0' + rand() % 10;
}
// run the thread
//cout<<"Client number:"<<i<< "will write value:" <<value<<endl;
threads.push_back(new std::thread(Thread_helper::_put, abd_clt[i], key,
sizeof(key), value, sizeof(value), &latency_value[counter]));
} else {
latency_type[counter] = 'R';
threads.push_back(new std::thread(Thread_helper::_get, abd_clt[i], key,
sizeof(key), &values[counter], &value_sizes[counter], &latency_value[counter]));
}
std::this_thread::sleep_for (std::chrono::milliseconds(98));
counter++;
//cout<<counter<<" ";
}
// std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
// cout<<"\ntime elapsed"<<std::chrono::duration_cast<std::chrono::milliseconds> (end - begin).count();
}
for(int i = 0; i < num_secs*ABD_NUMBER_OF_CLIENTS; i++){
threads[i]->join();
}
threads.clear();
calculate_read_and_write_latency(latency_type, latency_value,"ABD");
for(int i = 0; i < ABD_NUMBER_OF_CLIENTS; i++){
if(client_delete(abd_clt[i]) == -1){
fprintf(stderr, "%s\n", "Error occured in deleting clients");
return;
}
}
}
void cm_latency_executer(int read_write_ratio){
struct Client* cm_clt[CM_NUMBER_OF_CLIENTS];
char latency_type[CM_NUMBER_OF_CLIENTS];
uint64_t latency_value[CM_NUMBER_OF_CLIENTS]={0};
int counter = 0;
cout<<"Read_write_ratio: "<< read_write_ratio*10 <<endl;
for(uint i = 0; i < CM_NUMBER_OF_CLIENTS; i++){
cm_clt[i] = client_instance(i, "CM", servers, sizeof(servers) / sizeof(struct Server_info));
if(cm_clt[i] == NULL){
fprintf(stderr, "%s\n", "Error occured in creating clients");
return;
}
}
std::vector<std::thread*> threads;
srand(time(0));
char* values[CM_NUMBER_OF_CLIENTS];
uint32_t value_sizes[CM_NUMBER_OF_CLIENTS];
for (int j = 0; j< int(CM_NUMBER_OF_CLIENTS/10);j++) {
//std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
for(int i = 0; i < 10; i++) {
// build a random value
int prob = rand() % 10;
if (prob >= read_write_ratio) {
latency_type[counter] = 'W';
char value[SIZE_OF_VALUE] = {0};
for(int i = 0; i < SIZE_OF_VALUE; i++){
value[i] = '0' + rand() % 10;
}
// run the thread
//cout<<"Client number:"<<i<< "will write value:" <<value<<endl;
threads.push_back(new std::thread(Thread_helper::_put, cm_clt[i], key,
sizeof(key), value, sizeof(value), &latency_value[counter]));
} else {
latency_type[counter] = 'R';
threads.push_back(new std::thread(Thread_helper::_get, cm_clt[i], key,
sizeof(key), &values[counter], &value_sizes[counter], &latency_value[counter]));
}
std::this_thread::sleep_for (std::chrono::milliseconds(98));
counter++;
//cout<<counter<<" ";
}
//std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
//cout<<"\ntime elapsed"<<std::chrono::duration_cast<std::chrono::milliseconds> (end - begin).count();
}
for(int i = 0; i < CM_NUMBER_OF_CLIENTS; i++){
threads[i]->join();
}
threads.clear();
calculate_read_and_write_latency(latency_type, latency_value, "CM");
for(int i = 0; i < CM_NUMBER_OF_CLIENTS; i++){
if(client_delete(cm_clt[i]) == -1){
fprintf(stderr, "%s\n", "Error occured in deleting clients");
return;
}
}
}
void mp_latency_executer(int read_write_ratio){
struct Client* mp_clt[MP_NUMBER_OF_CLIENTS];
char latency_type[num_secs*MP_NUMBER_OF_CLIENTS];
uint64_t latency_value[num_secs*MP_NUMBER_OF_CLIENTS]={0};
int counter = 0;
cout<<"Read_write_ratio: "<< read_write_ratio*10 <<endl;
for(uint i = 0; i < MP_NUMBER_OF_CLIENTS; i++){
mp_clt[i] = client_instance(i, "MP", servers, sizeof(servers) / sizeof(struct Server_info));
if(mp_clt[i] == NULL){
fprintf(stderr, "%s\n", "Error occured in creating clients");
return;
}
}
std::vector<std::thread*> threads;
srand(time(0));
char* values[num_secs*MP_NUMBER_OF_CLIENTS];
uint32_t value_sizes[num_secs*MP_NUMBER_OF_CLIENTS];
for (int j = 0; j< num_secs;j++) {
//std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
for(int i = 0; i < MP_NUMBER_OF_CLIENTS; i++) {
// build a random value
int prob = rand() % 10;
if (prob >= read_write_ratio) {
latency_type[counter] = 'W';
char value[SIZE_OF_VALUE] = {0};
for(int i = 0; i < SIZE_OF_VALUE; i++){
value[i] = '0' + rand() % 10;
}
// run the thread
//cout<<"Client number:"<<i<< "will write value:" <<value<<endl;
threads.push_back(new std::thread(Thread_helper::_put, mp_clt[i], key,
sizeof(key), value, sizeof(value), &latency_value[counter]));
} else {
latency_type[counter] = 'R';
threads.push_back(new std::thread(Thread_helper::_get, mp_clt[i], key,
sizeof(key), &values[counter], &value_sizes[counter], &latency_value[counter]));
}
counter++;
}
std::this_thread::sleep_for (std::chrono::milliseconds(900)); // For 3 req/sec
for(int i = 0; i < MP_NUMBER_OF_CLIENTS; i++){
threads[i]->join();
}
threads.clear();
// std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
// cout<<"\ntime elapsed"<<std::chrono::duration_cast<std::chrono::milliseconds> (end - begin).count();
}
calculate_read_and_write_latency(latency_type, latency_value,"MP");
for(int i = 0; i < MP_NUMBER_OF_CLIENTS; i++){
if(client_delete(mp_clt[i]) == -1){
fprintf(stderr, "%s\n", "Error occured in deleting clients");
return;
}
}
}
int main(int argc, char* argv[]){
if(argc != 2){
fprintf(stderr, "%s%s%s\n", "Error\n"
"Usage: ", argv[0], "[ABD/CM/MP]\n\n"
"Please note to run the servers first\n"
"This application is just for your testing purposes, "
"and the evaluation of your code will be done with another initiation of your Client libraries.");
return -1;
}
if(std::string(argv[1]) == "ABD"){
abd_latency_executer(1); //10%
abd_latency_executer(5); //50%
abd_latency_executer(9); //90%
}
else if(std::string(argv[1]) == "CM"){
cm_latency_executer(1); // 10%
std::this_thread::sleep_for (std::chrono::milliseconds(5000));
cm_latency_executer(5); // 50%
std::this_thread::sleep_for (std::chrono::milliseconds(5000));
cm_latency_executer(9); // 90%
}
else if (std::string(argv[1]) == "MP"){
mp_latency_executer(1); // 10%
std::this_thread::sleep_for (std::chrono::milliseconds(5000));
mp_latency_executer(5); // 50%
std::this_thread::sleep_for (std::chrono::milliseconds(5000));
mp_latency_executer(9); // 90%
} else {
fprintf(stderr, "%s%s%s\n", "Error\n"
"Usage: ", argv[0], "[ABD/CM]\n\n"
"Please note to run the servers first\n"
"This application is just for your testing purposes, "
"and the evaluation of your code will be done with another initiation of your Client libraries.");
return -1;
}
return 0;
}