私たちのアプリケーションは通常、(HTTP GET を使用して) 数百メガバイトのデータを転送します。デフォルトの 64 Kb のチャンク サイズは、最適なダウンロード速度には小さすぎるようです。値を 5 Mb に変更すると、2 Gb データのダウンロード時間を 2 分から 28 秒に短縮できます。
要求されたデータをメモリ内に割り当てて送信するだけのデモ コード:
#include <Windows.h>
#include <cpprest/http_listener.h>
#include <cpprest/json.h>
#include <cpprest/streams.h>
#include <cpprest/filestream.h>
#include <cpprest/producerconsumerstream.h>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <string>
using namespace concurrency::streams;
using namespace web;
using namespace http;
using namespace http::experimental::listener;
int main(int argc, char *argv[]) {
http_listener listener(L"http://*:8080/bytes");
listener.support(methods::GET, [](http_request &request) {
auto q = web::uri::split_query(request.request_uri().query());
// default: 100 MB of data
std::size_t bytes_to_write = 100 * 1048576;
if (q.find(L"b") != std::end(q)) {
bytes_to_write = std::stoul(q[L"b"]);
}
if (q.find(L"kb") != std::end(q)) {
bytes_to_write = std::stoul(q[L"kb"]) * 1024;
}
if (q.find(L"mb") != std::end(q)) {
bytes_to_write = std::stoul(q[L"mb"]) * 1024 * 1024;
}
request.reply(status_codes::OK, std::string(bytes_to_write, '+'));
std::cout << "Sent " << bytes_to_write << "bytes\n";
});
listener.open().wait();
std::wcout << "Listening on " << listener.uri().port() << std::endl;
while (true) {
try {
Sleep(1);
}
catch (...) {
break;
}
}
listener.close().wait();
return 0;
}
そして、テストに curl を使用します:
curl -o NUL http://localhost:8080/bytes?mb=2000
64Kb のチャンク サイズを使用する場合:
[root@localhost ~]# curl -o NUL 10.50.10.51:8080/bytes?mb=2000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2000M 100 2000M 0 0 15.7M 0 0:02:06 0:02:06 --:--:-- 14.8M
5Mb のチャンク サイズを使用する場合:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2000M 100 2000M 0 0 69.2M 0 0:00:28 0:00:28 --:--:-- 77.1M
現在、cpprest のソース コードを変更して、後の結果を取得しています。CHUNK_SIZE
これは、ソース ファイル ( http_server_httpsys.cpp
)の 1 つで定義されたという名前のマクロです。
#define CHUNK_SIZE 64 * 1024
これを行う簡単な方法はありますか?または、間違った方法で cpprest を使用していますか?