Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND
#ifdef _WIN32
#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 10000
#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 1000
#else
#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0
#endif
Expand Down
41 changes: 41 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3155,6 +3155,47 @@ TEST_F(ServerTest, GetMethod200) {
EXPECT_EQ("Hello World!", res->body);
}

TEST(BenchmarkTest, SimpleGetPerformance) {
Server svr;

svr.Get("/benchmark", [&](const Request & /*req*/, Response &res) {
res.set_content("Benchmark Response", "text/plain");
});

auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
auto se = detail::scope_exit([&] {
svr.stop();
listen_thread.join();
ASSERT_FALSE(svr.is_running());
});

svr.wait_until_ready();

Client cli("localhost", PORT);

const int NUM_REQUESTS = 50;
const int MAX_AVERAGE_MS = 5;

auto warmup = cli.Get("/benchmark");
ASSERT_TRUE(warmup);

auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < NUM_REQUESTS; ++i) {
auto res = cli.Get("/benchmark");
ASSERT_TRUE(res) << "Request " << i << " failed";
EXPECT_EQ(StatusCode::OK_200, res->status);
}
auto end = std::chrono::high_resolution_clock::now();

auto total_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
double avg_ms = static_cast<double>(total_ms) / NUM_REQUESTS;

std::cout << "Standalone: " << NUM_REQUESTS << " requests in " << total_ms
<< "ms (avg: " << avg_ms << "ms)" << std::endl;

EXPECT_LE(avg_ms, MAX_AVERAGE_MS) << "Standalone test too slow: " << avg_ms << "ms (Issue #1777)";
}

TEST_F(ServerTest, GetEmptyFile) {
auto res = cli_.Get("/empty_file");
ASSERT_TRUE(res);
Expand Down
Loading