-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkv_store_client.cpp
More file actions
139 lines (129 loc) · 3.86 KB
/
kv_store_client.cpp
File metadata and controls
139 lines (129 loc) · 3.86 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
#include <boost/asio.hpp>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <string>
#include <vector>
#include <thread>
#include <iostream>
#include <random>
#include <time.h>
#include <mutex>
#include <fstream>
#include "utils.hpp"
using namespace boost::asio;
using ip::tcp;
using namespace std;
typedef unsigned int uint;
#define NUM_CLIENT 10
#define NUM_REQUEST 1000000
#define MAX_PAYLOAD_SIZE 1024
// Vector to store keys for get request.
vector<string> key_vec;
vector<long int> latency_values;
mutex mtx;
uint32_t get_random_number(){
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::uniform_int_distribution<uint32_t> distribution(0, key_vec.size()-1);
return distribution(generator);
}
long int calculate_latency(){
long int sum = 0;
int j = 0;
for (uint i = 0; i < latency_values.size(); i++) {
sum += latency_values[i];
// To avoid overflow.
if (j == 99) {
sum = sum/100;
j = 0;
}
j++;
}
return (sum/j);
}
namespace Thread_helper{
void client_(){
// Create TCP connection with server.
boost::asio::io_service io_service;
// Socket creation.
tcp::socket socket(io_service);
// Create connection.
socket.connect( tcp::endpoint(
boost::asio::ip::address::from_string("127.0.0.1"), 1234 ));
string key ;
for ( int i = 0; i < NUM_REQUEST/NUM_CLIENT; i++) {
// Fetch the key to query.
key = key_vec[get_random_number()];
// Start the clock.
std::chrono::steady_clock::time_point begin =
std::chrono::steady_clock::now();
boost::system::error_code error;
boost::asio::write(socket, boost::asio::buffer(key, key.size()), error);
if (error) {
cout << "send failed: " << error.message() << endl;
}
// Getting response from the server.
char reply[MAX_PAYLOAD_SIZE] = {0};
/* First read the respones size. */
boost::asio::read(socket, boost::asio::buffer(reply, HEADER_LEN), error);
if (error && error != boost::asio::error::eof) {
cout << "receive failed: " << error.message() << endl;
}
int body_len = stoi(reply);
if (body_len > 0) {
/* Now read the response. */
boost::asio::read(socket, boost::asio::buffer(reply, body_len), error);
if (error && error != boost::asio::error::eof) {
cout << "receive failed: " << error.message() << endl;
} else {
//cout << " " <<reply << " Len: "<< reply_length <<endl ;
}
} else {
cout << " Key not found or value was null Key:" << key << endl;
}
std::chrono::steady_clock::time_point end =
std::chrono::steady_clock::now();
mtx.lock();
latency_values.push_back(
std::chrono::duration_cast<std::chrono::microseconds>
(end - begin).count());
mtx.unlock();
}
socket.close();
return;
}
}
int main(int argc, char *argv[]) {
key_vec.reserve(10000);
cout <<"Reading Input data"<<endl;
if (argc < 2) {
cout<<"Please add data input file"<<endl;
exit(0);
}
std::ifstream file(argv[1]);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
// using printf() in all tests for consistencyS
//cout<<line.c_str() <<endl;
std::size_t first_space_loc = line.find(" ");
if (first_space_loc !=std::string::npos) {
string key = line.substr(0, first_space_loc);
key_vec.emplace_back(key);
}
}
file.close();
}
// Lets create threads.
std::vector<std::thread*> threads;
for ( int i = 0; i< NUM_CLIENT ; i++) {
threads.push_back(new std::thread(Thread_helper::client_));
}
for(int i = 0; i < NUM_CLIENT; i++){
threads[i]->join();
}
//Thread_helper::client_();
cout << " Avg latency is :" << calculate_latency() << " x 10^-6"<<endl;
return 0;
}