Skip to content

Commit 3e3d5a5

Browse files
committed
Add benchmark unit test
1 parent 3a1f379 commit 3e3d5a5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

test/test.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,6 +3112,10 @@ class ServerTest : public ::testing::Test {
31123112
}
31133113
})
31143114
#endif
3115+
.Get("/benchmark",
3116+
[&](const Request & /*req*/, Response &res) {
3117+
res.set_content("Benchmark Response", "text/plain");
3118+
})
31153119
;
31163120

31173121
persons_["john"] = "programmer";
@@ -3155,6 +3159,36 @@ TEST_F(ServerTest, GetMethod200) {
31553159
EXPECT_EQ("Hello World!", res->body);
31563160
}
31573161

3162+
TEST_F(ServerTest, GetBenchmark) {
3163+
const int NUM_REQUESTS = 100;
3164+
const int MAX_AVERAGE_MS = 10;
3165+
3166+
double total_time = 0.0;
3167+
3168+
// Warm up request
3169+
auto warmup = cli_.Get("/benchmark");
3170+
ASSERT_TRUE(warmup);
3171+
3172+
// Perform benchmark requests
3173+
for (int i = 0; i < NUM_REQUESTS; ++i) {
3174+
auto start = std::chrono::high_resolution_clock::now();
3175+
auto res = cli_.Get("/benchmark");
3176+
auto end = std::chrono::high_resolution_clock::now();
3177+
3178+
ASSERT_TRUE(res);
3179+
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
3184+
}
3185+
3186+
double avg_time = total_time / NUM_REQUESTS;
3187+
3188+
ASSERT_LE(avg_time, MAX_AVERAGE_MS)
3189+
<< "Average response time: " << avg_time << "ms exceeds " << MAX_AVERAGE_MS << "ms";
3190+
}
3191+
31583192
TEST_F(ServerTest, GetEmptyFile) {
31593193
auto res = cli_.Get("/empty_file");
31603194
ASSERT_TRUE(res);

0 commit comments

Comments
 (0)