|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "brpc/policy/auto_concurrency_limiter.h" |
| 19 | +#include "butil/time.h" |
| 20 | +#include <gtest/gtest.h> |
| 21 | + |
| 22 | +namespace brpc { |
| 23 | +namespace policy { |
| 24 | + |
| 25 | +DECLARE_int32(auto_cl_sample_window_size_ms); |
| 26 | +DECLARE_int32(auto_cl_min_sample_count); |
| 27 | +DECLARE_int32(auto_cl_max_sample_count); |
| 28 | +DECLARE_bool(auto_cl_enable_error_punish); |
| 29 | +DECLARE_double(auto_cl_fail_punish_ratio); |
| 30 | +DECLARE_double(auto_cl_error_rate_punish_threshold); |
| 31 | + |
| 32 | +} // namespace policy |
| 33 | +} // namespace brpc |
| 34 | + |
| 35 | +class AutoConcurrencyLimiterTest : public ::testing::Test { |
| 36 | +protected: |
| 37 | + void SetUp() override { |
| 38 | + // Save original values |
| 39 | + orig_sample_window_size_ms_ = brpc::policy::FLAGS_auto_cl_sample_window_size_ms; |
| 40 | + orig_min_sample_count_ = brpc::policy::FLAGS_auto_cl_min_sample_count; |
| 41 | + orig_max_sample_count_ = brpc::policy::FLAGS_auto_cl_max_sample_count; |
| 42 | + orig_enable_error_punish_ = brpc::policy::FLAGS_auto_cl_enable_error_punish; |
| 43 | + orig_fail_punish_ratio_ = brpc::policy::FLAGS_auto_cl_fail_punish_ratio; |
| 44 | + orig_error_rate_threshold_ = brpc::policy::FLAGS_auto_cl_error_rate_punish_threshold; |
| 45 | + |
| 46 | + // Set test-friendly values |
| 47 | + brpc::policy::FLAGS_auto_cl_sample_window_size_ms = 1000; |
| 48 | + brpc::policy::FLAGS_auto_cl_min_sample_count = 5; |
| 49 | + brpc::policy::FLAGS_auto_cl_max_sample_count = 200; |
| 50 | + brpc::policy::FLAGS_auto_cl_enable_error_punish = true; |
| 51 | + brpc::policy::FLAGS_auto_cl_fail_punish_ratio = 1.0; |
| 52 | + } |
| 53 | + |
| 54 | + void TearDown() override { |
| 55 | + // Restore original values |
| 56 | + brpc::policy::FLAGS_auto_cl_sample_window_size_ms = orig_sample_window_size_ms_; |
| 57 | + brpc::policy::FLAGS_auto_cl_min_sample_count = orig_min_sample_count_; |
| 58 | + brpc::policy::FLAGS_auto_cl_max_sample_count = orig_max_sample_count_; |
| 59 | + brpc::policy::FLAGS_auto_cl_enable_error_punish = orig_enable_error_punish_; |
| 60 | + brpc::policy::FLAGS_auto_cl_fail_punish_ratio = orig_fail_punish_ratio_; |
| 61 | + brpc::policy::FLAGS_auto_cl_error_rate_punish_threshold = orig_error_rate_threshold_; |
| 62 | + } |
| 63 | + |
| 64 | +private: |
| 65 | + int32_t orig_sample_window_size_ms_; |
| 66 | + int32_t orig_min_sample_count_; |
| 67 | + int32_t orig_max_sample_count_; |
| 68 | + bool orig_enable_error_punish_; |
| 69 | + double orig_fail_punish_ratio_; |
| 70 | + double orig_error_rate_threshold_; |
| 71 | +}; |
| 72 | + |
| 73 | +// Helper function to add samples and trigger window completion |
| 74 | +// Uses synthetic timestamps instead of sleeping for faster, deterministic tests. |
| 75 | +// The final successful sample is used as the trigger, so actual counts match |
| 76 | +// succ_count/fail_count exactly (preserving intended error rates). |
| 77 | +void AddSamplesAndTriggerWindow(brpc::policy::AutoConcurrencyLimiter& limiter, |
| 78 | + int succ_count, int64_t succ_latency, |
| 79 | + int fail_count, int64_t fail_latency) { |
| 80 | + ASSERT_GT(succ_count, 0) << "Need at least 1 success to trigger window"; |
| 81 | + int64_t now = butil::gettimeofday_us(); |
| 82 | + |
| 83 | + // Add successful samples (reserve one for the trigger) |
| 84 | + for (int i = 0; i < succ_count - 1; ++i) { |
| 85 | + limiter.AddSample(0, succ_latency, now); |
| 86 | + } |
| 87 | + // Add failed samples |
| 88 | + for (int i = 0; i < fail_count; ++i) { |
| 89 | + limiter.AddSample(1, fail_latency, now); |
| 90 | + } |
| 91 | + |
| 92 | + // Advance timestamp past window expiry instead of sleeping |
| 93 | + int64_t after_window = now + brpc::policy::FLAGS_auto_cl_sample_window_size_ms * 1000 + 1000; |
| 94 | + |
| 95 | + // Use the final success sample to trigger window submission |
| 96 | + limiter.AddSample(0, succ_latency, after_window); |
| 97 | +} |
| 98 | + |
| 99 | +// Test 1: Backward compatibility - threshold=0 preserves original punishment behavior |
| 100 | +TEST_F(AutoConcurrencyLimiterTest, ThresholdZeroPreservesOriginalBehavior) { |
| 101 | + brpc::policy::FLAGS_auto_cl_error_rate_punish_threshold = 0; |
| 102 | + brpc::policy::FLAGS_auto_cl_sample_window_size_ms = 10; |
| 103 | + |
| 104 | + brpc::policy::AutoConcurrencyLimiter limiter; |
| 105 | + AddSamplesAndTriggerWindow(limiter, 90, 100, 10, 1000); |
| 106 | + |
| 107 | + // 10% error rate, threshold=0 means full punishment applied |
| 108 | + // avg_latency = ceil((10*1000 + 90*100) / 90) = ceil(211.1) = 212us |
| 109 | + ASSERT_GT(limiter._min_latency_us, 180); |
| 110 | + ASSERT_LT(limiter._min_latency_us, 250); |
| 111 | +} |
| 112 | + |
| 113 | +// Test 2: Dead zone - error rate below threshold produces zero punishment |
| 114 | +TEST_F(AutoConcurrencyLimiterTest, BelowThresholdZeroPunishment) { |
| 115 | + brpc::policy::FLAGS_auto_cl_error_rate_punish_threshold = 0.2; // 20% threshold |
| 116 | + brpc::policy::FLAGS_auto_cl_sample_window_size_ms = 10; |
| 117 | + |
| 118 | + brpc::policy::AutoConcurrencyLimiter limiter; |
| 119 | + AddSamplesAndTriggerWindow(limiter, 90, 100, 10, 1000); |
| 120 | + |
| 121 | + // 10% error rate < 20% threshold, punishment should be zero |
| 122 | + // avg_latency = 90*100 / 90 = 100us (no inflation) |
| 123 | + ASSERT_GT(limiter._min_latency_us, 80); |
| 124 | + ASSERT_LT(limiter._min_latency_us, 130); |
| 125 | +} |
| 126 | + |
| 127 | +// Test 3: Boundary - error rate exactly at threshold produces zero punishment |
| 128 | +TEST_F(AutoConcurrencyLimiterTest, ExactlyAtThresholdZeroPunishment) { |
| 129 | + brpc::policy::FLAGS_auto_cl_error_rate_punish_threshold = 0.1; // 10% threshold |
| 130 | + brpc::policy::FLAGS_auto_cl_sample_window_size_ms = 10; |
| 131 | + |
| 132 | + brpc::policy::AutoConcurrencyLimiter limiter; |
| 133 | + AddSamplesAndTriggerWindow(limiter, 90, 100, 10, 1000); |
| 134 | + |
| 135 | + // 10% error rate == 10% threshold, punishment should be zero |
| 136 | + // avg_latency = 90*100 / 90 = 100us |
| 137 | + ASSERT_GT(limiter._min_latency_us, 80); |
| 138 | + ASSERT_LT(limiter._min_latency_us, 130); |
| 139 | +} |
| 140 | + |
| 141 | +// Test 4: Linear scaling - above threshold, punishment scales proportionally |
| 142 | +TEST_F(AutoConcurrencyLimiterTest, AboveThresholdLinearScaling) { |
| 143 | + brpc::policy::FLAGS_auto_cl_error_rate_punish_threshold = 0.1; // 10% threshold |
| 144 | + brpc::policy::FLAGS_auto_cl_sample_window_size_ms = 10; |
| 145 | + |
| 146 | + // Case A: 50% error rate |
| 147 | + // punish_factor = (0.5 - 0.1) / (1.0 - 0.1) = 4/9 ≈ 0.444 |
| 148 | + // failed_punish = 50 * 1000 * (4/9) = 22222.2us |
| 149 | + // avg_latency = ceil((22222.2 + 50*100) / 50) = ceil(544.4) = 545us |
| 150 | + { |
| 151 | + brpc::policy::AutoConcurrencyLimiter limiter; |
| 152 | + AddSamplesAndTriggerWindow(limiter, 50, 100, 50, 1000); |
| 153 | + ASSERT_GT(limiter._min_latency_us, 450); |
| 154 | + ASSERT_LT(limiter._min_latency_us, 650); |
| 155 | + } |
| 156 | + |
| 157 | + // Case B: 90% error rate (near full punishment) |
| 158 | + // punish_factor = (0.9 - 0.1) / (1.0 - 0.1) = 8/9 ≈ 0.889 |
| 159 | + // failed_punish = 90 * 1000 * (8/9) = 80000us |
| 160 | + // avg_latency = ceil((80000 + 10*100) / 10) = ceil(8100) = 8100us |
| 161 | + { |
| 162 | + brpc::policy::AutoConcurrencyLimiter limiter; |
| 163 | + AddSamplesAndTriggerWindow(limiter, 10, 100, 90, 1000); |
| 164 | + ASSERT_GT(limiter._min_latency_us, 7000); |
| 165 | + ASSERT_LT(limiter._min_latency_us, 9000); |
| 166 | + } |
| 167 | +} |
| 168 | + |
0 commit comments