Skip to content

Commit

Permalink
Refactor: performed refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bersen66 committed May 6, 2024
1 parent 63c83bd commit 4d538ad
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ target_link_libraries(
lb_tests PUBLIC
lb2
GTest::gtest
spdlog::spdlog
yaml-cpp::yaml-cpp
jemalloc::jemalloc
)
add_dependencies(lb_tests copy_configs)
gtest_discover_tests(
Expand Down
40 changes: 39 additions & 1 deletion benchmarks/benchmark_heaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct ConnectionsCompare {

static void BenchmarkFibbanaciHeap(benchmark::State& state)
{

for (auto _ : state)
{
boost::heap::fibonacci_heap<CounterWrapper, boost::heap::compare<ConnectionsCompare>> heap;
Expand All @@ -34,6 +35,7 @@ BENCHMARK(BenchmarkFibbanaciHeap);

static void BenchmarkPairingHeap(benchmark::State& state)
{

for (auto _ : state)
{
boost::heap::pairing_heap<CounterWrapper, boost::heap::compare<ConnectionsCompare>> heap;
Expand All @@ -46,4 +48,40 @@ static void BenchmarkPairingHeap(benchmark::State& state)
}
}

BENCHMARK(BenchmarkPairingHeap);
BENCHMARK(BenchmarkPairingHeap);

static void BenchmarkPairingHeapPush(benchmark::State& state)
{
boost::heap::pairing_heap<int> heap;
for (auto _ : state)
{
for (int i = 0; i < state.range(0); ++i)
{
heap.push(i);
}
while (!heap.empty())
{
heap.pop();
}
}
}

BENCHMARK(BenchmarkPairingHeapPush)->Range(1<<0, 1<<16);

static void BenchmarkFibonacciHeapPush(benchmark::State& state)
{
boost::heap::fibonacci_heap<int> heap;
for (auto _ : state)
{
for (int i = 0; i < state.range(0); ++i)
{
heap.push(i);
}
while (!heap.empty())
{
heap.pop();
}
}
}

BENCHMARK(BenchmarkFibonacciHeapPush)->Range(1<<0, 1<<16);
6 changes: 3 additions & 3 deletions src/lb/tcp/selectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,9 @@ void LeastResponseTimeSelector::AddResponseTime(const Backend& backend, long res
const std::string& str = backend.ToString();
if (auto it = handle_pool_.find(str); it != handle_pool_.end()) {
HandleType& handle = it->second;
double old_ewa = (*handle).response_time_ewa;
(*handle).response_time_ewa = (1 - (*handle).alpha) * old_ewa + (*handle).alpha * response_time;
if (old_ewa > (*handle).response_time_ewa) {
double old_ema = (*handle).response_time_ema;
(*handle).response_time_ema = (1 - (*handle).alpha) * old_ema + (*handle).alpha * response_time;
if (old_ema > (*handle).response_time_ema) {
backends_.decrease(handle);
} else {
backends_.increase(handle);
Expand Down
19 changes: 10 additions & 9 deletions src/lb/tcp/selectors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ SelectorPtr DetectSelector(const YAML::Node& config);
class RoundRobinSelector final : public ISelector {
public:
using EndpointType = boost::asio::ip::tcp::endpoint;
using SocketType = boost::asio::ip::tcp::socket;
public:
void Configure(const YAML::Node& config) override;

Expand Down Expand Up @@ -182,8 +181,9 @@ class LeastConnectionsSelector final : public ISelector {
}
};

using PairingMap = boost::heap::pairing_heap<CounterWrapper, boost::heap::compare<ConnectionsCompare>>;
using HandleType = PairingMap::handle_type;
using PairingHeap = boost::heap::pairing_heap<CounterWrapper,
boost::heap::compare<ConnectionsCompare>>;
using HandleType = PairingHeap::handle_type;

private:

Expand All @@ -192,7 +192,7 @@ class LeastConnectionsSelector final : public ISelector {
private:
boost::recursive_mutex mutex_;
std::unordered_map<std::string, HandleType> handle_pool_;
PairingMap backends_;
PairingHeap backends_;
};


Expand All @@ -210,23 +210,24 @@ class LeastResponseTimeSelector final : public ISelector {
private:
struct AverageTimeWrapper {
lb::tcp::Backend backend;
double response_time_ewa = 0.0; // exponential weighted average for response time
double response_time_ema = 0.0;
double alpha = 0.9;
};

struct ResponseTimeEMACompare{
bool operator()(const AverageTimeWrapper& lhs, const AverageTimeWrapper& rhs) const
{
return lhs.response_time_ewa > rhs.response_time_ewa;
return lhs.response_time_ema > rhs.response_time_ema;
}
};
private:
using PairingMap = boost::heap::pairing_heap<AverageTimeWrapper, boost::heap::compare<ResponseTimeEMACompare>>;
using HandleType = PairingMap::handle_type;
using PairingHeap = boost::heap::pairing_heap<AverageTimeWrapper,
boost::heap::compare<ResponseTimeEMACompare>>;
using HandleType = PairingHeap::handle_type;
private:
boost::mutex mutex_;
std::unordered_map<std::string, HandleType> handle_pool_;
PairingMap backends_;
PairingHeap backends_;

};

Expand Down

0 comments on commit 4d538ad

Please sign in to comment.