Unverified Commit 73f248ac authored by Igor [hyperxor]'s avatar Igor [hyperxor] Committed by GitHub

Issue #745: changes in http_client example (#746)

parent a90fecd2
......@@ -6,9 +6,9 @@
#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;
......@@ -27,9 +27,7 @@ int main(int argc, char *argv[]) {
Http::Client client;
auto opts = Http::Client::options()
.threads(1)
.maxConnectionsPerHost(8);
auto opts = Http::Client::options().threads(1).maxConnectionsPerHost(8);
client.init(opts);
std::vector<Async::Promise<Http::Response>> responses;
......@@ -41,29 +39,38 @@ int main(int argc, char *argv[]) {
for (int i = 0; i < count; ++i) {
auto resp = client.get(page).cookie(Http::Cookie("FOO", "bar")).send();
resp.then([&](Http::Response response) {
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);
},
[&](std::exception_ptr exc) {
++failedRequests;
PrintException excPrinter;
excPrinter(exc);
});
responses.push_back(std::move(resp));
}
auto sync = Async::whenAll(responses.begin(), responses.end());
Async::Barrier<std::vector<Http::Response>> barrier(sync);
if(barrier.wait_for(std::chrono::seconds(5)) == std::cv_status::timeout) {
++failedRequests;
}
}
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 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;
<< std::chrono::duration_cast<std::chrono::milliseconds>(end -
start)
.count()
<< "ms" << std::endl;
client.shutdown();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment