microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
7.99k stars 1.65k forks source link

Can u provide some perfom test vs other lib? #1754

Open unity20221019 opened 1 year ago

unity20221019 commented 1 year ago

i just write a simple test vs curl or other lib in linux. so slow so disappointed? such a big company team should not provide produces like this. the test is use this lib to implement the functionality of wrk.

unity20221019 commented 1 year ago
#include <iostream>
#include <chrono>
#include <pplx/pplxtasks.h>
#include <cpprest/http_client.h>

using namespace pplx;
using namespace web;
using namespace web::http;
using namespace web::http::client;

const int num_threads = 20;
const int num_requests = 50;
const std::string url = "https://www.google.com";
const int success_code = 200;

void send_requests(int thread_id, int& count, int& success_count, long long& total_latency)
{
    http_client client(url);

    for (int i = 0; i < num_requests; i++) {
        auto start = std::chrono::high_resolution_clock::now();

        client.request(methods::GET).then([start, thread_id, i, &count, &success_count, &total_latency](http_response response) {
            auto end = std::chrono::high_resolution_clock::now();
            auto latency = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
            std::cout << "Thread " << thread_id << " request " << i << " latency: " << latency << "ms" << std::endl;

            count++;
            total_latency += latency;
            if (response.status_code() == success_code) {
                success_count++;
            }
        }).wait();
    }
}

int main()
{
    int count = 0;
    int success_count = 0;
    long long total_latency = 0;

    std::vector<task<void>> tasks;
    for (int i = 0; i < num_threads; i++) {
        tasks.push_back(create_task([i, &count, &success_count, &total_latency] {
            send_requests(i, count, success_count, total_latency);
        }));
    }

    pplx::when_all(tasks.begin(), tasks.end()).wait();

    double total_time = total_latency / 1000.0;
    double success_rate = static_cast<double>(success_count) / static_cast<double>(count);
    double avg_rate = static_cast<double>(success_count) / total_time;

    std::cout << "Total time: " << total_time << " seconds" << std::endl;
    std::cout << "Success rate: " << success_rate << std::endl;
    std::cout << "Average rate: " << avg_rate << " successful requests/second" << std::endl;

    return 0;
}

here the test code provide.

unity20221019 commented 1 year ago

terminate called after throwing an instance of 'web::http::http_exception' what(): Error in SSL handshake Aborted

and thy this simple code can end wtih an exception above?