Skip to content
Snippets Groups Projects
Commit d19fb230 authored by Saif Eddine Askri's avatar Saif Eddine Askri
Browse files

Add latency calculation and improve request processing with mutex for thread safety

parent fa80db89
No related branches found
No related tags found
No related merge requests found
...@@ -71,7 +71,7 @@ ...@@ -71,7 +71,7 @@
"127.0.0.1", "127.0.0.1",
"2525", "2525",
"1024", "1024",
"20000", "100000",
"random", "random",
], ],
"stopAtEntry": false, "stopAtEntry": false,
......
...@@ -5,16 +5,20 @@ ...@@ -5,16 +5,20 @@
#include <vector> #include <vector>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <chrono>
#include <thread>
#include <mutex>
#define HEADER_SIZE 1 #define HEADER_SIZE 1
pid_t pid = getpid(); pid_t pid = getpid();
std::vector<double> latencies;
std::mutex latencies_mutex;
std::string generateRandomKey() { std::string generateRandomKey() {
return "key" + std::to_string(rand() % 10000); return "key" + std::to_string(rand() % 10000);
} }
std::string generateRandomValue() { std::string generateRandomValue() {
return "value" + std::to_string(rand() % 10000); return "value" + std::to_string(rand() % 10000);
} }
...@@ -115,6 +119,21 @@ std::vector<std::pair<char*, int>> generateRequests(int requestCount, std::strin ...@@ -115,6 +119,21 @@ std::vector<std::pair<char*, int>> generateRequests(int requestCount, std::strin
return requests; return requests;
} }
void calculate_average_latency() {
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lock(latencies_mutex);
if (!latencies.empty()) {
double sum = 0;
for (double latency : latencies) {
sum += latency;
}
double average_latency = sum / latencies.size();
std::cout << "Average latency: " << average_latency << " ms" << std::endl;
//latencies.clear();
}
}
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if (argc != 6) { if (argc != 6) {
...@@ -158,26 +177,33 @@ int main(int argc, char* argv[]) { ...@@ -158,26 +177,33 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// Start the latency calculation thread
std::thread latency_thread(calculate_average_latency);
latency_thread.detach();
for (const auto& message : requests) { for (const auto& message : requests) {
auto start = std::chrono::high_resolution_clock::now();
send(sock, message.first, message.second, 0); send(sock, message.first, message.second, 0);
std::cout << "Sent: " << message.first << std::endl; //std::cout << "Sent: " << message.first << std::endl;
ssize_t bytes_received = read(sock, buffer, bufferSize); ssize_t bytes_received = read(sock, buffer, bufferSize);
if (bytes_received > 0) { if (bytes_received > 0) {
std::cout << "Server Response: " << buffer << std::endl; // std::cout << "Server Response: " << buffer << std::endl;
} }
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> latency = end - start;
{
std::lock_guard<std::mutex> lock(latencies_mutex);
latencies.push_back(latency.count());
}
memset(buffer, 0, bufferSize); memset(buffer, 0, bufferSize);
} }
std::cout << "Closing connection..." << std::endl; std::cout << "Closing connection..." << std::endl;
close(sock); close(sock);
return 0; return 0;
} }
\ No newline at end of file
...@@ -71,7 +71,7 @@ struct Request { ...@@ -71,7 +71,7 @@ struct Request {
free(input); free(input);
return req; return req;
} }
// we assume that this function is slow
std::string processRequest_slow(const std::string& request) { std::string processRequest_slow(const std::string& request) {
std::string response; std::string response;
...@@ -105,10 +105,10 @@ std::string processRequest_slow(const std::string& request) { ...@@ -105,10 +105,10 @@ std::string processRequest_slow(const std::string& request) {
free(parsedRequest); free(parsedRequest);
return response; return response;
} }
//=============================================
// we assume that this function is fast
// gelichzeitig lesen aber nur einmal schreiben // gelichzeitig lesen aber nur einmal schreiben
std::string processRequest_fast(const std::string& request) { std::string processRequest_unique_mutex(const std::string& request) {
std::string response; std::string response;
size_t first = request.find(","); size_t first = request.find(",");
...@@ -158,9 +158,6 @@ std::string processRequest_fast(const std::string& request) { ...@@ -158,9 +158,6 @@ std::string processRequest_fast(const std::string& request) {
return response; return response;
} }
//=============================================
// mehrer Thread Lesen gleichzeitig aber ein Thread schreibt. // mehrer Thread Lesen gleichzeitig aber ein Thread schreibt.
std::string processRequest_shared_mtx(const std::string& request) { std::string processRequest_shared_mtx(const std::string& request) {
std::string response; std::string response;
...@@ -217,7 +214,6 @@ std::string processRequest_shared_mtx(const std::string& request) { ...@@ -217,7 +214,6 @@ std::string processRequest_shared_mtx(const std::string& request) {
return response; return response;
} }
void handle_client(int client_socket) { void handle_client(int client_socket) {
std::string response; std::string response;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment