1
- use criterion:: { Criterion , black_box , criterion_group , criterion_main , Throughput , BenchmarkId } ;
1
+ use criterion:: { BenchmarkId , Criterion , Throughput , black_box , criterion_group , criterion_main } ;
2
2
use std:: time:: { Duration , Instant } ;
3
3
use tokio:: runtime:: Runtime ;
4
4
5
5
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 ,
9
7
} ;
10
8
11
9
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) {
42
40
b. iter ( || {
43
41
let openai = create_mock_openai ( ) ;
44
42
let config = create_simple_config ( "BenchAgent" ) ;
45
-
43
+
46
44
let _agent = SwarmsAgentBuilder :: new_with_model ( openai)
47
45
. config ( config)
48
46
. disable_task_complete_tool ( ) // Disable tools for faster init
49
47
. build ( ) ;
50
-
48
+
51
49
black_box ( _agent) ;
52
50
} ) ;
53
51
} ) ;
@@ -67,19 +65,19 @@ fn bench_batch_agent_initialization(c: &mut Criterion) {
67
65
|b, & batch_size| {
68
66
b. iter ( || {
69
67
let mut agents = Vec :: with_capacity ( batch_size) ;
70
-
68
+
71
69
for i in 0 ..batch_size {
72
70
let openai = create_mock_openai ( ) ;
73
71
let config = create_simple_config ( & format ! ( "Agent{}" , i) ) ;
74
-
72
+
75
73
let agent = SwarmsAgentBuilder :: new_with_model ( openai)
76
74
. config ( config)
77
75
. disable_task_complete_tool ( )
78
76
. build ( ) ;
79
-
77
+
80
78
agents. push ( agent) ;
81
79
}
82
-
80
+
83
81
black_box ( agents) ;
84
82
} ) ;
85
83
} ,
@@ -92,38 +90,41 @@ fn bench_batch_agent_initialization(c: &mut Criterion) {
92
90
/// Benchmark how many agents can be initialized in one minute
93
91
fn bench_agents_per_minute ( c : & mut Criterion ) {
94
92
let mut group = c. benchmark_group ( "agents_per_minute" ) ;
95
-
93
+
96
94
// Set a longer measurement time for this benchmark
97
95
group. measurement_time ( Duration :: from_secs ( 60 ) ) ;
98
96
group. sample_size ( 10 ) ; // Fewer samples since each takes a minute
99
-
97
+
100
98
group. bench_function ( "max_agents_in_60_seconds" , |b| {
101
99
b. iter_custom ( |iters| {
102
100
let start = Instant :: now ( ) ;
103
101
let mut total_agents = 0 ;
104
-
102
+
105
103
for _ in 0 ..iters {
106
104
let init_start = Instant :: now ( ) ;
107
105
let mut count = 0 ;
108
-
106
+
109
107
// Initialize agents for 60 seconds
110
108
while init_start. elapsed ( ) < Duration :: from_secs ( 60 ) {
111
109
let openai = create_mock_openai ( ) ;
112
110
let config = create_simple_config ( & format ! ( "SpeedAgent{}" , count) ) ;
113
-
111
+
114
112
let _agent = SwarmsAgentBuilder :: new_with_model ( openai)
115
113
. config ( config)
116
114
. disable_task_complete_tool ( )
117
115
. build ( ) ;
118
-
116
+
119
117
count += 1 ;
120
118
black_box ( _agent) ;
121
119
}
122
-
120
+
123
121
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
+ ) ;
125
126
}
126
-
127
+
127
128
start. elapsed ( )
128
129
} ) ;
129
130
} ) ;
@@ -147,8 +148,9 @@ fn bench_concurrent_agent_initialization(c: &mut Criterion) {
147
148
. map ( |i| {
148
149
tokio:: spawn ( async move {
149
150
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
+
152
154
SwarmsAgentBuilder :: new_with_model ( openai)
153
155
. config ( config)
154
156
. disable_task_complete_tool ( )
@@ -184,12 +186,12 @@ fn bench_different_configurations(c: &mut Criterion) {
184
186
. build ( )
185
187
. as_ref ( )
186
188
. clone ( ) ;
187
-
189
+
188
190
let _agent = SwarmsAgentBuilder :: new_with_model ( openai)
189
191
. config ( config)
190
192
. disable_task_complete_tool ( )
191
193
. build ( ) ;
192
-
194
+
193
195
black_box ( _agent) ;
194
196
} ) ;
195
197
} ) ;
@@ -208,11 +210,11 @@ fn bench_different_configurations(c: &mut Criterion) {
208
210
. build ( )
209
211
. as_ref ( )
210
212
. clone ( ) ;
211
-
213
+
212
214
let _agent = SwarmsAgentBuilder :: new_with_model ( openai)
213
215
. config ( config)
214
216
. build ( ) ; // Keep task evaluator tool for full config
215
-
217
+
216
218
black_box ( _agent) ;
217
219
} ) ;
218
220
} ) ;
@@ -223,27 +225,31 @@ fn bench_different_configurations(c: &mut Criterion) {
223
225
/// Quick test to measure initialization rate
224
226
fn quick_initialization_rate_test ( ) {
225
227
println ! ( "\n === Quick Agent Initialization Rate Test ===" ) ;
226
-
228
+
227
229
let start = Instant :: now ( ) ;
228
230
let mut count = 0 ;
229
231
let test_duration = Duration :: from_secs ( 10 ) ; // 10 second test
230
-
232
+
231
233
while start. elapsed ( ) < test_duration {
232
234
let openai = create_mock_openai ( ) ;
233
235
let config = create_simple_config ( & format ! ( "QuickTest{}" , count) ) ;
234
-
236
+
235
237
let _agent = SwarmsAgentBuilder :: new_with_model ( openai)
236
238
. config ( config)
237
239
. disable_task_complete_tool ( )
238
240
. build ( ) ;
239
-
241
+
240
242
count += 1 ;
241
243
}
242
-
244
+
243
245
let elapsed = start. elapsed ( ) ;
244
246
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
+ ) ;
247
253
println ! ( "Rate: {:.2} agents/second" , rate) ;
248
254
println ! ( "Estimated agents per minute: {:.0}" , rate * 60.0 ) ;
249
255
println ! ( "=========================================\n " ) ;
0 commit comments