Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Performance von Anwendungen
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Saif Eddine Askri
Performance von Anwendungen
Commits
d19fb230
Commit
d19fb230
authored
2 months ago
by
Saif Eddine Askri
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
prak3/.vscode/launch.json
+1
-1
1 addition, 1 deletion
prak3/.vscode/launch.json
prak3/src/client.cpp
+38
-12
38 additions, 12 deletions
prak3/src/client.cpp
prak3/src/server.cpp
+3
-7
3 additions, 7 deletions
prak3/src/server.cpp
with
42 additions
and
20 deletions
prak3/.vscode/launch.json
+
1
−
1
View file @
d19fb230
...
@@ -71,7 +71,7 @@
...
@@ -71,7 +71,7 @@
"127.0.0.1"
,
"127.0.0.1"
,
"2525"
,
"2525"
,
"1024"
,
"1024"
,
"
2
0000"
,
"
10
0000"
,
"random"
,
"random"
,
],
],
"stopAtEntry"
:
false
,
"stopAtEntry"
:
false
,
...
...
This diff is collapsed.
Click to expand it.
prak3/src/client.cpp
+
38
−
12
View file @
d19fb230
...
@@ -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
This diff is collapsed.
Click to expand it.
prak3/src/server.cpp
+
3
−
7
View file @
d19fb230
...
@@ -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
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment