Skip to content

Commit f2cb8e3

Browse files
Implement lazy initialization for agent components
1 parent 4020c4b commit f2cb8e3

File tree

5 files changed

+289
-226
lines changed

5 files changed

+289
-226
lines changed

swarms-rs/benches/agent_initialization_benchmarks.rs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use criterion::{Criterion, black_box, criterion_group, criterion_main, Throughput, BenchmarkId};
1+
use criterion::{BenchmarkId, Criterion, Throughput, black_box, criterion_group, criterion_main};
22
use std::time::{Duration, Instant};
33
use tokio::runtime::Runtime;
44

55
use swarms_rs::{
6-
agent::SwarmsAgentBuilder,
7-
llm::provider::openai::OpenAI,
8-
structs::agent::AgentConfig,
6+
agent::SwarmsAgentBuilder, llm::provider::openai::OpenAI, structs::agent::AgentConfig,
97
};
108

119
fn run_async<F: std::future::Future<Output = T>, T>(future: F) -> T {
@@ -42,12 +40,12 @@ fn bench_single_agent_initialization(c: &mut Criterion) {
4240
b.iter(|| {
4341
let openai = create_mock_openai();
4442
let config = create_simple_config("BenchAgent");
45-
43+
4644
let _agent = SwarmsAgentBuilder::new_with_model(openai)
4745
.config(config)
4846
.disable_task_complete_tool() // Disable tools for faster init
4947
.build();
50-
48+
5149
black_box(_agent);
5250
});
5351
});
@@ -67,19 +65,19 @@ fn bench_batch_agent_initialization(c: &mut Criterion) {
6765
|b, &batch_size| {
6866
b.iter(|| {
6967
let mut agents = Vec::with_capacity(batch_size);
70-
68+
7169
for i in 0..batch_size {
7270
let openai = create_mock_openai();
7371
let config = create_simple_config(&format!("Agent{}", i));
74-
72+
7573
let agent = SwarmsAgentBuilder::new_with_model(openai)
7674
.config(config)
7775
.disable_task_complete_tool()
7876
.build();
79-
77+
8078
agents.push(agent);
8179
}
82-
80+
8381
black_box(agents);
8482
});
8583
},
@@ -92,38 +90,41 @@ fn bench_batch_agent_initialization(c: &mut Criterion) {
9290
/// Benchmark how many agents can be initialized in one minute
9391
fn bench_agents_per_minute(c: &mut Criterion) {
9492
let mut group = c.benchmark_group("agents_per_minute");
95-
93+
9694
// Set a longer measurement time for this benchmark
9795
group.measurement_time(Duration::from_secs(60));
9896
group.sample_size(10); // Fewer samples since each takes a minute
99-
97+
10098
group.bench_function("max_agents_in_60_seconds", |b| {
10199
b.iter_custom(|iters| {
102100
let start = Instant::now();
103101
let mut total_agents = 0;
104-
102+
105103
for _ in 0..iters {
106104
let init_start = Instant::now();
107105
let mut count = 0;
108-
106+
109107
// Initialize agents for 60 seconds
110108
while init_start.elapsed() < Duration::from_secs(60) {
111109
let openai = create_mock_openai();
112110
let config = create_simple_config(&format!("SpeedAgent{}", count));
113-
111+
114112
let _agent = SwarmsAgentBuilder::new_with_model(openai)
115113
.config(config)
116114
.disable_task_complete_tool()
117115
.build();
118-
116+
119117
count += 1;
120118
black_box(_agent);
121119
}
122-
120+
123121
total_agents += count;
124-
println!("Initialized {} agents in 60 seconds (iteration {})", count, total_agents);
122+
println!(
123+
"Initialized {} agents in 60 seconds (iteration {})",
124+
count, total_agents
125+
);
125126
}
126-
127+
127128
start.elapsed()
128129
});
129130
});
@@ -147,8 +148,9 @@ fn bench_concurrent_agent_initialization(c: &mut Criterion) {
147148
.map(|i| {
148149
tokio::spawn(async move {
149150
let openai = create_mock_openai();
150-
let config = create_simple_config(&format!("ConcurrentAgent{}", i));
151-
151+
let config =
152+
create_simple_config(&format!("ConcurrentAgent{}", i));
153+
152154
SwarmsAgentBuilder::new_with_model(openai)
153155
.config(config)
154156
.disable_task_complete_tool()
@@ -184,12 +186,12 @@ fn bench_different_configurations(c: &mut Criterion) {
184186
.build()
185187
.as_ref()
186188
.clone();
187-
189+
188190
let _agent = SwarmsAgentBuilder::new_with_model(openai)
189191
.config(config)
190192
.disable_task_complete_tool()
191193
.build();
192-
194+
193195
black_box(_agent);
194196
});
195197
});
@@ -208,11 +210,11 @@ fn bench_different_configurations(c: &mut Criterion) {
208210
.build()
209211
.as_ref()
210212
.clone();
211-
213+
212214
let _agent = SwarmsAgentBuilder::new_with_model(openai)
213215
.config(config)
214216
.build(); // Keep task evaluator tool for full config
215-
217+
216218
black_box(_agent);
217219
});
218220
});
@@ -223,27 +225,31 @@ fn bench_different_configurations(c: &mut Criterion) {
223225
/// Quick test to measure initialization rate
224226
fn quick_initialization_rate_test() {
225227
println!("\n=== Quick Agent Initialization Rate Test ===");
226-
228+
227229
let start = Instant::now();
228230
let mut count = 0;
229231
let test_duration = Duration::from_secs(10); // 10 second test
230-
232+
231233
while start.elapsed() < test_duration {
232234
let openai = create_mock_openai();
233235
let config = create_simple_config(&format!("QuickTest{}", count));
234-
236+
235237
let _agent = SwarmsAgentBuilder::new_with_model(openai)
236238
.config(config)
237239
.disable_task_complete_tool()
238240
.build();
239-
241+
240242
count += 1;
241243
}
242-
244+
243245
let elapsed = start.elapsed();
244246
let rate = count as f64 / elapsed.as_secs_f64();
245-
246-
println!("Initialized {} agents in {:.2}s", count, elapsed.as_secs_f64());
247+
248+
println!(
249+
"Initialized {} agents in {:.2}s",
250+
count,
251+
elapsed.as_secs_f64()
252+
);
247253
println!("Rate: {:.2} agents/second", rate);
248254
println!("Estimated agents per minute: {:.0}", rate * 60.0);
249255
println!("=========================================\n");

0 commit comments

Comments
 (0)