Skip to content

Commit 22b791e

Browse files
committed
Update
1 parent 3e3d5a5 commit 22b791e

File tree

1 file changed

+71
-16
lines changed

1 file changed

+71
-16
lines changed

test/test.cc

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,33 +3160,88 @@ TEST_F(ServerTest, GetMethod200) {
31603160
}
31613161

31623162
TEST_F(ServerTest, GetBenchmark) {
3163-
const int NUM_REQUESTS = 100;
3164-
const int MAX_AVERAGE_MS = 10;
3163+
// Simple benchmark test for detecting performance regressions (Issue #1777)
3164+
// This test helps identify slow response times that may indicate DNS or socket issues
31653165

3166-
double total_time = 0.0;
3166+
const int NUM_REQUESTS = 100;
3167+
const int MAX_AVERAGE_MS = 10; // Fail if average exceeds 10ms (performance regression)
31673168

3168-
// Warm up request
3169+
// Warmup request
31693170
auto warmup = cli_.Get("/benchmark");
31703171
ASSERT_TRUE(warmup);
31713172

3172-
// Perform benchmark requests
3173+
// Measure performance of multiple requests
3174+
auto start = std::chrono::high_resolution_clock::now();
31733175
for (int i = 0; i < NUM_REQUESTS; ++i) {
3174-
auto start = std::chrono::high_resolution_clock::now();
31753176
auto res = cli_.Get("/benchmark");
3176-
auto end = std::chrono::high_resolution_clock::now();
3177-
3178-
ASSERT_TRUE(res);
3177+
ASSERT_TRUE(res) << "Request " << i << " failed";
31793178
EXPECT_EQ(StatusCode::OK_200, res->status);
3180-
EXPECT_EQ("Benchmark Response", res->body);
3181-
3182-
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
3183-
total_time += elapsed / 1000.0; // Convert to milliseconds
31843179
}
3180+
auto end = std::chrono::high_resolution_clock::now();
3181+
3182+
auto total_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
3183+
double avg_ms = static_cast<double>(total_ms) / NUM_REQUESTS;
3184+
3185+
std::cout << "Benchmark: " << NUM_REQUESTS << " requests in " << total_ms
3186+
<< "ms (avg: " << avg_ms << "ms)" << std::endl;
3187+
3188+
// Fail if average response time is too high (indicates performance regression)
3189+
EXPECT_LE(avg_ms, MAX_AVERAGE_MS) << "Average response time too slow: " << avg_ms << "ms"
3190+
<< " - this may indicate DNS resolution or socket creation issues (Issue #1777)";
3191+
}
3192+
3193+
TEST_F(ServerTest, GetBenchmarkParallel) {
3194+
// Test parallel requests to detect performance issues (Issue #1777)
3195+
// This test helps identify connection overhead and potential bottlenecks
3196+
3197+
const int num_requests = 50;
3198+
const int num_threads = 5;
3199+
const int requests_per_thread = num_requests / num_threads;
3200+
3201+
std::vector<std::thread> threads;
3202+
std::vector<double> thread_times(num_threads);
3203+
std::atomic<bool> all_successful{true};
3204+
3205+
auto start_total = std::chrono::high_resolution_clock::now();
3206+
3207+
for (int t = 0; t < num_threads; ++t) {
3208+
threads.emplace_back([&, t]() {
3209+
auto start = std::chrono::high_resolution_clock::now();
3210+
3211+
for (int i = 0; i < requests_per_thread; ++i) {
3212+
auto res = cli_.Get("/benchmark");
3213+
if (!res) {
3214+
all_successful = false;
3215+
return;
3216+
}
3217+
}
3218+
3219+
auto end = std::chrono::high_resolution_clock::now();
3220+
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
3221+
thread_times[static_cast<size_t>(t)] = static_cast<double>(duration) / requests_per_thread;
3222+
});
3223+
}
3224+
3225+
for (auto& t : threads) {
3226+
t.join();
3227+
}
3228+
3229+
auto end_total = std::chrono::high_resolution_clock::now();
3230+
auto total_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_total - start_total).count();
3231+
3232+
ASSERT_TRUE(all_successful) << "Some parallel requests failed";
3233+
3234+
double avg_per_thread = 0;
3235+
for (double time : thread_times) {
3236+
avg_per_thread += time;
3237+
}
3238+
avg_per_thread /= num_threads;
31853239

3186-
double avg_time = total_time / NUM_REQUESTS;
3240+
std::cout << "Parallel: " << num_requests << " requests across " << num_threads
3241+
<< " threads in " << total_ms << "ms"
3242+
<< " (avg per request: " << avg_per_thread << "ms)" << std::endl;
31873243

3188-
ASSERT_LE(avg_time, MAX_AVERAGE_MS)
3189-
<< "Average response time: " << avg_time << "ms exceeds " << MAX_AVERAGE_MS << "ms";
3244+
EXPECT_LE(avg_per_thread, 50.0) << "Parallel requests too slow: " << avg_per_thread << "ms";
31903245
}
31913246

31923247
TEST_F(ServerTest, GetEmptyFile) {

0 commit comments

Comments
 (0)