From f8381212ea6d3378b9c8c79eb351bcb98d4e8b75 Mon Sep 17 00:00:00 2001 From: Bob Chen Date: Mon, 19 Feb 2024 19:13:45 +0800 Subject: [PATCH] throttle add test case --- common/test/test_throttle.cpp | 60 ++++++++++++++++++++++++++++------- common/throttle.h | 3 ++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/common/test/test_throttle.cpp b/common/test/test_throttle.cpp index b8d0e6d5..e2ae7246 100644 --- a/common/test/test_throttle.cpp +++ b/common/test/test_throttle.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -17,16 +18,17 @@ TEST(Throttle, basic) { total -= step; } auto end = std::chrono::steady_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); - auto baseline = duration.count(); - LOG_INFO("cosume 10M unit in ` us", DEC(baseline).comma(true)); + auto duration_ns = + std::chrono::duration_cast(end - start); + auto baseline = duration_ns.count(); + LOG_INFO("Baseline: consume 10M unit in ` ns", DEC(baseline).comma(true)); + + //////////////////////////////////////// // try update time photon::thread_yield(); // using throttle to limit increasing count 1M in seconds - photon::throttle t(1UL * 1024 * 1024); - + photon::throttle t1(1UL * 1024 * 1024); // suppose to be done in at least 9 seconds total = 10UL * 1024 * 1024; start = std::chrono::steady_clock::now(); @@ -34,15 +36,51 @@ TEST(Throttle, basic) { // assume each step may consume about 4K ~ 1M auto step = rand() % (1UL * 1024 * 1024 - 4096) + 4096; if (step > total) step = total; - t.consume(step); + t1.consume(step); total -= step; } end = std::chrono::steady_clock::now(); - duration = + auto duration_us = std::chrono::duration_cast(end - start); - LOG_INFO("cosume 10M with 1M throttle in ` us", - DEC(duration.count()).comma(true)); - EXPECT_GT(duration.count(), 9UL * 1000 * 1000); + LOG_INFO("Normal Throttle: consume 10M with 1M throttle in ` us", + DEC(duration_us.count()).comma(true)); + EXPECT_GT(duration_us.count(), 9UL * 1000 * 1000); + + //////////////////////////////////////// + + photon::throttle t2(-1UL); + total = 10UL * 1024 * 1024; + start = std::chrono::steady_clock::now(); + while (total) { + // assume each step may consume about 4K ~ 1M + auto step = rand() % (1UL * 1024 * 1024 - 4096) + 4096; + if (step > total) step = total; + t2.consume(step); + total -= step; + } + end = std::chrono::steady_clock::now(); + duration_ns = std::chrono::duration_cast(end - start); + LOG_INFO("No-limit Throttle: consume 10M with non-throttle in ` ns", + DEC(duration_ns.count()).comma(true)); + EXPECT_LT(duration_ns.count(), 1000UL * 1000); + + //////////////////////////////////////// + + photon::throttle t3(0); + auto th = photon::thread_create11([&] { + start = std::chrono::steady_clock::now(); + t3.consume(1); + end = std::chrono::steady_clock::now(); + }); + photon::thread_enable_join(th); + photon::thread_sleep(2); + photon::thread_interrupt(th); + photon::thread_join((photon::join_handle*) th); + + duration_us = std::chrono::duration_cast(end - start); + LOG_INFO("Maximum Throttle: consume hang ` us", DEC(duration_us.count()).comma(true)); + EXPECT_GT(duration_us.count(), 2000UL * 1000); + EXPECT_LT(duration_us.count(), 2200UL * 1000); } TEST(Throttle, restore) { diff --git a/common/throttle.h b/common/throttle.h index 7de49114..ecb2164f 100644 --- a/common/throttle.h +++ b/common/throttle.h @@ -29,6 +29,9 @@ class throttle { } public: + /** + * @param limit -1UL means no limit, 0 means lowest speed (hang) + */ throttle(uint64_t limit, uint64_t time_window = 1000UL * 1000, uint64_t slice = 10) { m_slice_num = slice;