example-api-client icon indicating copy to clipboard operation
example-api-client copied to clipboard

The Client Request is too slow.

Open SollyTaylor opened this issue 5 years ago • 1 comments

I'm using oatpp web service framework and it's amazing and accelerating my backend dev works. However when I am running the oatpp api client demo and found out that the API calling is too slow. I added the profiler for each API calling to test the time elapses.

As we can see the demo code implementation Code Snippet 1 and its running result in Code Snippet 2. The http query responding time varied. Ranging from 300ms to 2100ms. Is the normal performance of the client side of oatpp? Is there something wrong within the demo code?

Any suggestions would be appreciated.

Code snippet 1

#ifndef SimpleExample_hpp
#define SimpleExample_hpp

#include "./DemoApiClient.hpp"
#include <chrono>
#include <iostream>

class Profiler {
public:
    Profiler(){
        timepoint_ = std::chrono::steady_clock::now();  // extract start time point
    }
    ~Profiler(){
        auto newtimepoint = std::chrono::steady_clock::now(); // extract end time point
        auto diff = newtimepoint - timepoint_; // get diff
        const auto count = std::chrono::duration <double, std::nano> (diff).count(); // convert to nanoseconds
        auto ms = count  / 1E6; // convert to milliseconds
        std::cout << "time interval:" << ms << "(ms)" << std::endl; // print out
    }

private:
    std::chrono::steady_clock::time_point timepoint_;
};

class SimpleExample {
private:
    constexpr static const char* TAG = "SimpleExample";
public:

    void static runExample(const std::shared_ptr<DemoApiClient>& client) {

        {
            Profiler p;
            auto data = client->doGet()->readBodyToString();
            OATPP_LOGD(TAG, "[doGet] data='%s'", data->c_str());
        }

        {
            Profiler p;
            auto data = client->doPost("Some data passed to POST")->readBodyToString();
            OATPP_LOGD(TAG, "[doPost] data='%s'", data->c_str());
        }

        {
            Profiler p;
            auto data = client->doGetAnything("path-parameter")->readBodyToString();
            OATPP_LOGD(TAG, "[doGetAnything] data='%s'", data->c_str());
        }

        {
            Profiler p;
            auto data = client->doPostAnything("path-parameter", "Some body here")->readBodyToString();
            OATPP_LOGD(TAG, "[doPostAnything] data='%s'", data->c_str());
        }

        {
            Profiler p;
            auto dto = MyRequestDto::createShared();
            dto->message = "Some message";
            dto->code = 200;
            auto data = client->doPostWithDto(dto)->readBodyToString();
            OATPP_LOGD(TAG, "[doPostWithDto] data='%s'", data->c_str());
        }

    }

};

#endif /* SimpleExample_hpp */

Code snippet 2

$ ./EquinoxRequestDemo.exe
 D |2020-11-18 17:05:22 1605690322411377| App:Using Oat++ native HttpRequestExecutor.
 D |2020-11-18 17:05:22 1605690322928226| SimpleExample:[doGet] data='{
  "args": {},
  "headers": {
    "Content-Length": "0",
    "Host": "httpbin.org",
    "X-Amzn-Trace-Id": "Root=1-5fb4e3d2-7003f73172fc020e6eb7b989"
  },
  "origin": "219.143.191.216",
  "url": "http://httpbin.org/get"
}
'
time interval:515.295(ms)
 D |2020-11-18 17:05:23 1605690323412916| SimpleExample:[doPost] data='{
  "args": {},
  "data": "Some data passed to POST",
  "files": {},
  "form": {},
  "headers": {
    "Content-Length": "24",
    "Host": "httpbin.org",
    "X-Amzn-Trace-Id": "Root=1-5fb4e3d3-522c758f7021ee3952adb35d"
  },
  "json": null,
  "origin": "219.143.191.216",
  "url": "http://httpbin.org/post"
}
'
time interval:484.651(ms)
 D |2020-11-18 17:05:24 1605690324890657| SimpleExample:[doGetAnything] data='{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Content-Length": "0",
    "Host": "httpbin.org",
    "X-Amzn-Trace-Id": "Root=1-5fb4e3d4-739ddfe40e1caf1f1167d2b3"
  },
  "json": null,
  "method": "GET",
  "origin": "219.143.191.216",
  "url": "http://httpbin.org/anything/path-parameter"
}
'
time interval:1477.73(ms)
 D |2020-11-18 17:05:25 1605690325375150| SimpleExample:[doPostAnything] data='{
  "args": {},
  "data": "Some body here",
  "files": {},
  "form": {},
  "headers": {
    "Content-Length": "14",
    "Host": "httpbin.org",
    "X-Amzn-Trace-Id": "Root=1-5fb4e3d5-03dffab411edb44113b521f3"
  },
  "json": null,
  "method": "POST",
  "origin": "219.143.191.216",
  "url": "http://httpbin.org/anything/path-parameter"
}
'
time interval:484.474(ms)
 D |2020-11-18 17:05:27 1605690327606193| SimpleExample:[doPostWithDto] data='{
  "args": {},
  "data": "{\"message\":\"Some message\",\"code\":200}",
  "files": {},
  "form": {},
  "headers": {
    "Content-Length": "37",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "X-Amzn-Trace-Id": "Root=1-5fb4e3d6-483bdb096bde01f31eda4889"
  },
  "json": {
    "code": 200,
    "message": "Some message"
  },
  "origin": "219.143.191.216",
  "url": "http://httpbin.org/post"
}
'
time interval:2231.03(ms)

Environment:
objectsCount = 0
objectsCreated = 235


SollyTaylor avatar Nov 18 '20 09:11 SollyTaylor

Hello @SollyTaylor ,

I believe that this might be a location/network thing since you are accessing a remote service. An example is running against the httpbin.org which from my location is resolved to 3.211.1.78 - Virginia, USA.

Here are my results running the same example with your Profiler (location - Europe):

On WIFI:

time interval:290.852(ms)
time interval:245.506(ms)
time interval:245.742(ms)
time interval:242.067(ms)
time interval:243.209(ms)

On the Mobile network:

time interval:333.269(ms)
time interval:359.466(ms)
time interval:362.205(ms)
time interval:368.195(ms)
time interval:342.913(ms)

lganzzzo avatar Nov 18 '20 10:11 lganzzzo