-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_distributed_strategy.py
More file actions
55 lines (44 loc) · 2.07 KB
/
Copy pathtest_distributed_strategy.py
File metadata and controls
55 lines (44 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import time
from distributed.coordinator import DistributedCoordinator
from distributed.distributed_strategy import StrategyFactory
def test_load_balancing_strategies():
"""测试不同的负载均衡策略"""
# 获取协调器实例
coordinator = DistributedCoordinator()
# 注册多个Agent
agent_types = ['coding', 'research', 'planning', 'debugging']
for i in range(5):
agent_id = f'agent_{i}'
agent_type = agent_types[i % len(agent_types)]
capabilities = [agent_type, 'general']
endpoint = f'http://localhost:800{i}'
coordinator.register_agent(agent_id, agent_type, capabilities, endpoint)
# 测试不同的负载均衡策略
strategies = ['round_robin', 'random', 'least_connections', 'performance_based', 'task_type_based']
for strategy in strategies:
print(f"\n测试策略: {strategy}")
# 设置当前策略
if not coordinator.set_strategy(strategy):
print(f"跳过无效策略: {strategy}")
continue
# 重置Agent任务计数
for agent_id in coordinator.agents:
coordinator.agents[agent_id]['task_count'] = 0
# 为性能基于策略设置模拟性能指标
coordinator.agents[agent_id]['performance_score'] = 100 - i * 10 if strategy == 'performance_based' else 0
# 分配任务
task_types = ['coding', 'research', 'planning', 'debugging', 'general']
for i in range(20):
task_type = task_types[i % len(task_types)]
task_data = {'task_id': i, 'data': f'Task {i} data'}
coordinator.assign_task(task_type, task_data)
# 打印任务分配结果
print("任务分配结果:")
for agent_id, agent in coordinator.agents.items():
print(f"Agent {agent_id} (类型: {agent['type']}): {agent['task_count']} 个任务")
# 短暂暂停,让任务分配完成
time.sleep(1)
if __name__ == '__main__':
print("开始测试分布式负载均衡策略...")
test_load_balancing_strategies()
print("测试完成!")