Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
73f248ac
Unverified
Commit
73f248ac
authored
Mar 29, 2020
by
Igor [hyperxor]
Committed by
GitHub
Mar 29, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #745: changes in http_client example (#746)
parent
a90fecd2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
44 deletions
+51
-44
examples/http_client.cc
examples/http_client.cc
+51
-44
No files found.
examples/http_client.cc
View file @
73f248ac
...
...
@@ -6,64 +6,71 @@
#include <atomic>
#include <pistache/net.h>
#include <pistache/http.h>
#include <pistache/client.h>
#include <pistache/http.h>
#include <pistache/net.h>
using
namespace
Pistache
;
using
namespace
Pistache
::
Http
;
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<
2
)
{
std
::
cerr
<<
"Usage: http_client page [count]"
<<
std
::
endl
;
return
1
;
}
if
(
argc
<
2
)
{
std
::
cerr
<<
"Usage: http_client page [count]"
<<
std
::
endl
;
return
1
;
}
std
::
string
page
=
argv
[
1
];
int
count
=
1
;
if
(
argc
==
3
)
{
count
=
std
::
stoi
(
argv
[
2
]);
}
std
::
string
page
=
argv
[
1
];
int
count
=
1
;
if
(
argc
==
3
)
{
count
=
std
::
stoi
(
argv
[
2
]);
}
Http
::
Client
client
;
Http
::
Client
client
;
auto
opts
=
Http
::
Client
::
options
()
.
threads
(
1
)
.
maxConnectionsPerHost
(
8
);
client
.
init
(
opts
);
auto
opts
=
Http
::
Client
::
options
().
threads
(
1
).
maxConnectionsPerHost
(
8
);
client
.
init
(
opts
);
std
::
vector
<
Async
::
Promise
<
Http
::
Response
>>
responses
;
std
::
vector
<
Async
::
Promise
<
Http
::
Response
>>
responses
;
std
::
atomic
<
size_t
>
completedRequests
(
0
);
std
::
atomic
<
size_t
>
failedRequests
(
0
);
std
::
atomic
<
size_t
>
completedRequests
(
0
);
std
::
atomic
<
size_t
>
failedRequests
(
0
);
auto
start
=
std
::
chrono
::
system_clock
::
now
();
auto
start
=
std
::
chrono
::
system_clock
::
now
();
for
(
int
i
=
0
;
i
<
count
;
++
i
)
{
auto
resp
=
client
.
get
(
page
).
cookie
(
Http
::
Cookie
(
"FOO"
,
"bar"
)).
send
();
resp
.
then
([
&
](
Http
::
Response
response
)
{
++
completedRequests
;
std
::
cout
<<
"Response code = "
<<
response
.
code
()
<<
std
::
endl
;
auto
body
=
response
.
body
();
if
(
!
body
.
empty
())
std
::
cout
<<
"Response body = "
<<
body
<<
std
::
endl
;
},
Async
::
IgnoreException
);
responses
.
push_back
(
std
::
move
(
resp
));
auto
sync
=
Async
::
whenAll
(
responses
.
begin
(),
responses
.
end
());
Async
::
Barrier
<
std
::
vector
<
Http
::
Response
>>
barrier
(
sync
);
for
(
int
i
=
0
;
i
<
count
;
++
i
)
{
auto
resp
=
client
.
get
(
page
).
cookie
(
Http
::
Cookie
(
"FOO"
,
"bar"
)).
send
();
resp
.
then
(
[
&
](
Http
::
Response
response
)
{
++
completedRequests
;
std
::
cout
<<
"Response code = "
<<
response
.
code
()
<<
std
::
endl
;
auto
body
=
response
.
body
();
if
(
!
body
.
empty
())
std
::
cout
<<
"Response body = "
<<
body
<<
std
::
endl
;
},
[
&
](
std
::
exception_ptr
exc
)
{
++
failedRequests
;
PrintException
excPrinter
;
excPrinter
(
exc
);
});
responses
.
push_back
(
std
::
move
(
resp
));
}
if
(
barrier
.
wait_for
(
std
::
chrono
::
seconds
(
5
))
==
std
::
cv_status
::
timeout
)
{
++
failedRequests
;
}
}
auto
sync
=
Async
::
whenAll
(
responses
.
begin
(),
responses
.
end
());
Async
::
Barrier
<
std
::
vector
<
Http
::
Response
>>
barrier
(
sync
);
barrier
.
wait_for
(
std
::
chrono
::
seconds
(
5
));
auto
end
=
std
::
chrono
::
system_clock
::
now
();
std
::
cout
<<
"Summary of execution"
<<
std
::
endl
<<
"Total number of requests sent : "
<<
count
<<
std
::
endl
<<
"Total number of responses received: "
<<
completedRequests
.
load
()
<<
std
::
endl
<<
"Total number of requests failed : "
<<
failedRequests
.
load
()
<<
std
::
endl
<<
"Total time of execution : "
<<
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
end
-
start
).
count
()
<<
"ms"
<<
std
::
endl
;
auto
end
=
std
::
chrono
::
system_clock
::
now
();
std
::
cout
<<
"Summary of execution"
<<
std
::
endl
<<
"Total number of requests sent : "
<<
count
<<
std
::
endl
<<
"Total number of responses received: "
<<
completedRequests
.
load
()
<<
std
::
endl
<<
"Total number of requests failed : "
<<
failedRequests
.
load
()
<<
std
::
endl
<<
"Total time of execution : "
<<
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
end
-
start
)
.
count
()
<<
"ms"
<<
std
::
endl
;
client
.
shutdown
();
client
.
shutdown
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment