-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feature: Add feature: Add support for weighted load balancing.
- Loading branch information
Showing
6 changed files
with
183 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ node_modules | |
.wrangler | ||
.pytest_cache | ||
*.jpg | ||
*.json | ||
*.json | ||
*.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import json | ||
import matplotlib.pyplot as plt | ||
from datetime import datetime, timedelta | ||
from collections import defaultdict | ||
|
||
import matplotlib.font_manager as fm | ||
font_path = '/System/Library/Fonts/PingFang.ttc' | ||
prop = fm.FontProperties(fname=font_path) | ||
plt.rcParams['font.family'] = prop.get_name() | ||
|
||
with open('./test/states.json') as f: | ||
data = json.load(f) | ||
request_arrivals = data["request_arrivals"] | ||
|
||
def create_pic(request_arrivals, key): | ||
request_arrivals = request_arrivals[key] | ||
# 将字符串转换为datetime对象 | ||
datetimes = [datetime.fromisoformat(t) for t in request_arrivals] | ||
# 获取最新的时间 | ||
latest_time = max(datetimes) | ||
|
||
# 创建24小时的时间范围 | ||
time_range = [latest_time - timedelta(hours=i) for i in range(24, 0, -1)] | ||
# 统计每小时的请求数 | ||
hourly_counts = defaultdict(int) | ||
for dt in datetimes: | ||
for t in time_range[::-1]: | ||
if dt >= t: | ||
hourly_counts[t] += 1 | ||
break | ||
|
||
# 准备绘图数据 | ||
hours = [t.strftime('%Y-%m-%d %H:00') for t in time_range] | ||
counts = [hourly_counts[t] for t in time_range] | ||
|
||
# 创建柱状图 | ||
plt.figure(figsize=(15, 6)) | ||
plt.bar(hours, counts) | ||
plt.title(f'{key} 端点请求量 (过去24小时)') | ||
plt.xlabel('时间') | ||
plt.ylabel('请求数') | ||
plt.xticks(rotation=45, ha='right') | ||
plt.tight_layout() | ||
|
||
# 保存图片 | ||
plt.savefig(f'{key.replace("/", "")}.png') | ||
|
||
if __name__ == '__main__': | ||
create_pic(request_arrivals, 'POST /v1/chat/completions') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def weighted_round_robin(weights): | ||
provider_names = list(weights.keys()) | ||
current_weights = {name: 0 for name in provider_names} | ||
num_selections = total_weight = sum(weights.values()) | ||
weighted_provider_list = [] | ||
|
||
for _ in range(num_selections): | ||
max_ratio = -1 | ||
selected_letter = None | ||
|
||
for name in provider_names: | ||
current_weights[name] += weights[name] | ||
ratio = current_weights[name] / weights[name] | ||
|
||
if ratio > max_ratio: | ||
max_ratio = ratio | ||
selected_letter = name | ||
|
||
weighted_provider_list.append(selected_letter) | ||
current_weights[selected_letter] -= total_weight | ||
|
||
return weighted_provider_list | ||
|
||
# 权重和选择次数 | ||
weights = {'a': 5, 'b': 3, 'c': 2} | ||
index = {'a', 'c'} | ||
|
||
result = dict(filter(lambda item: item[0] in index, weights.items())) | ||
print(result) | ||
# result = {k: weights[k] for k in index if k in weights} | ||
# print(result) | ||
weighted_provider_list = weighted_round_robin(weights) | ||
print(weighted_provider_list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters