Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP POST request #15

Closed
notscimmy opened this issue Sep 7, 2017 · 8 comments
Closed

HTTP POST request #15

notscimmy opened this issue Sep 7, 2017 · 8 comments

Comments

@notscimmy
Copy link

POST requests are extremely slow. Each request is taking about 1 second to complete. I am using the library on windows.

@yhirose
Copy link
Owner

yhirose commented Sep 7, 2017

@jl2378, thanks for the report!

Do you mean POST requests are much slower than GET requests?
How much data are you posting to the server, small data or large data?

As you can see in the code, both implementations are almost same except the following POST specific code in void Server::process_request(Stream& strm).

    if (req.method == "POST") {
        if (!detail::read_content(strm, req)) {
            // TODO:
            return;
        }
        static std::string type = "application/x-www-form-urlencoded";
        if (!req.get_header_value("Content-Type").compare(0, type.size(), type)) {
            detail::parse_query_text(req.body, req.params);
        }
    }

It's so simple, right?

I have personally never had such a performance issues on my Windows box. So I am really curious why the problem happenes on your environment.

Please let me know more detailed information regarding the performance issue.

@yhirose yhirose closed this as completed in bfb7f7b Sep 8, 2017
@yhirose
Copy link
Owner

yhirose commented Sep 8, 2017

Oops... Reopen it again.

@yhirose
Copy link
Owner

yhirose commented Sep 9, 2017

Could it be related to the socket connection being reestablished every time a request is made?

It could be. In order to understand the situation more, I made the sample code to measure each post request time.

#include <httplib.h>
#include <chrono>
#include <iostream>

using namespace std;

struct StopWatch {
  StopWatch(const string& label) : label_(label) {
    start_ = chrono::system_clock::now();
  }
  ~StopWatch() {
    auto end = chrono::system_clock::now();
    auto diff = end - start_;
    auto count = chrono::duration_cast<chrono::milliseconds>(diff).count();
    cout << label_ << ": " << count << " millisec." << endl;
  }
  string label_;
  chrono::system_clock::time_point start_;
};

int main(int argc, char* argv[]) {
  string body(1024 * 5, 'a');

  httplib::Client cli("httpbin.org", 80);

  for (int i = 0; i < 3; i++) {
    StopWatch sw(to_string(i).c_str());
    auto res = cli.post("/post", body, "application/octet-stream");
    assert(res->status == 200);
  }

  return 0;
}

When I run it on my Windows box, I got the following result:

0: 392 millisec.
1: 24 millisec.
2: 25 millisec.

It works pretty good on my machine.

What result do you get with the code on your machine?
Or is this code missing something to reproduce this problem?

Thanks for your help!

@yhirose
Copy link
Owner

yhirose commented Nov 14, 2017

Please let me know if it is still an issue with you.

@dkeeney
Copy link

dkeeney commented Feb 27, 2020

I am getting a very slow response also. But it is all messages, not just POST.
I am using your server and client examples as the starting point.

auto res = client.Get("/hi");

Running on windows under Visual Studio 2019. Both debug and release modes. I get about 1 second per message I send in a loop.

On the server, looking at the req object passed to the log using the debugger,
content_length = 14757395258967641292
The Content-Length header is correct but I wonder if something is iterating through that content_length field in the request object that is causing a major delay.

Also, perhaps a separate issue, I noticed that if I send a POST with a body, the req argument in the server's log function never contains the body. The body field is always empty. Yet I know it arrive at the server because the handler processes it.

  res = client.Post("/network", config, "application/json");

Where 'config' is a string containing a JSON encoded structure of about 500 bytes. The handler matches "/network".
So...where is it in the req parameter of the server log so I can log it?

@yhirose
Copy link
Owner

yhirose commented Feb 27, 2020

Thanks for the report. But I still cannot reproduce the slow connection/reading problem...
I just compile and run the above client benchmark code again.

D:\Projects\cpp-httplib\example>cl benchmark.cc -I../

D:\Projects\cpp-httplib\example>benchmark.exe
0: 484 millisec.
1: 385 millisec.
2: 394 millisec.

D:\Projects\cpp-httplib\example>benchmark.exe
0: 37 millisec.
1: 26 millisec.
2: 30 millisec.

As you see, 1st time is decent. 2nd time is super fast. I would like to know what you get with the benchmark. Could you try it when you have time? By this, we can at least check if the problem happens on the client side.

I am really curious about content_length = 14757395258967641292 Did httplib::Client set Content-Length to such a huge value? If so, that's a serious bug which might choke cpp-httplib server. I personally have never experienced such situation... Please let me know where the value came from.

When it comes to the logging, I cannot reproduce it either. Here is my test result:

D:\Projects\cpp-httplib\example>cl server.cc -I../

D:\Projects\cpp-httplib\example>server.exe
================================
GET HTTP/1.1 /hi
Accept: */*
Connection: close
Content-Length: 0
Host: localhost:8080
REMOTE_ADDR: 127.0.0.1
User-Agent: cpp-httplib/0.5
--------------------------------
200 HTTP/1.1
Connection: close
Content-Length: 13
Content-Type: text/plain

Hello World!
D:\Projects\cpp-httplib\example>cl client.cc -I../

D:\Projects\cpp-httplib\example>client.exe
200
text/plain
Hello World!

Could you try the above to see if you get the same result?

Thanks for your help!

@dkeeney
Copy link

dkeeney commented Feb 27, 2020

I will create a separate issue for this.

@dkeeney
Copy link

dkeeney commented Feb 27, 2020

Thanks for the fast response.
The problem with the length seems to have gone away. don't know what that was all about.

The speed problem remains. I moved it to #366

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants