@@ -10,7 +10,8 @@ pub use self::{
1010 options:: BenchOptions ,
1111} ;
1212
13- use codspeed:: codspeed:: CodSpeed ;
13+ use :: codspeed:: codspeed:: CodSpeed ;
14+ use :: codspeed:: instrument_hooks:: InstrumentHooks ;
1415use std:: cell:: RefCell ;
1516
1617/// Using this in place of `()` for `GenI` prevents `Bencher::with_inputs` from
@@ -136,11 +137,15 @@ where
136137 {
137138 let mut codspeed = self . codspeed . borrow_mut ( ) ;
138139 let mut gen_input = self . config . gen_input . borrow_mut ( ) ;
139- let input = gen_input ( ) ;
140- codspeed. start_benchmark ( self . uri . as_str ( ) ) ;
141- let output = benched ( input) ;
142- codspeed. end_benchmark ( ) ;
143- divan:: black_box ( output) ;
140+
141+ codspeed:: run_rounds ( & mut codspeed, self . uri . as_str ( ) , || {
142+ // FIXME: We could also run multiple rounds here
143+ let input = gen_input ( ) ;
144+ InstrumentHooks :: toggle_collect ( ) ;
145+ let output = benched ( divan:: black_box ( input) ) ;
146+ InstrumentHooks :: toggle_collect ( ) ;
147+ divan:: black_box ( output) ;
148+ } ) ;
144149 }
145150
146151 pub fn bench_local_refs < O , B > ( self , mut benched : B )
@@ -149,11 +154,49 @@ where
149154 {
150155 let mut codspeed = self . codspeed . borrow_mut ( ) ;
151156 let mut gen_input = self . config . gen_input . borrow_mut ( ) ;
152- let mut input = gen_input ( ) ;
153157
154- codspeed. start_benchmark ( self . uri . as_str ( ) ) ;
155- let output = benched ( & mut input) ;
158+ codspeed:: run_rounds ( & mut codspeed, self . uri . as_str ( ) , || {
159+ let mut input = gen_input ( ) ;
160+ InstrumentHooks :: toggle_collect ( ) ;
161+ let output = benched ( & mut input) ;
162+ InstrumentHooks :: toggle_collect ( ) ;
163+ divan:: black_box ( input) ;
164+ divan:: black_box ( output) ;
165+ } ) ;
166+ }
167+ }
168+
169+ mod codspeed {
170+ use super :: * ;
171+ use std:: time:: { Duration , Instant } ;
172+
173+ pub fn run_rounds ( codspeed : & mut CodSpeed , uri : & str , mut run_iteration : impl FnMut ( ) ) {
174+ // FIXME: Maybe move this to codspeed
175+ let ( max_rounds, max_duration) = match std:: env:: var ( "CODSPEED_RUNNER_MODE" ) . as_deref ( ) {
176+ Ok ( "simulation" ) | Ok ( "instrumentation" ) => ( None , Some ( Duration :: from_millis ( 100 ) ) ) ,
177+ Ok ( "memory" ) => ( Some ( 1 ) , None ) ,
178+ Ok ( m) => unreachable ! ( "Invalid runner mode: {m}" ) ,
179+ Err ( err) => panic ! ( "Failed to get runner mode: {err}" ) ,
180+ } ;
181+ let mut rounds = 0 ;
182+ let rounds_start_time = Instant :: now ( ) ;
183+
184+ codspeed. start_benchmark ( uri) ;
185+ InstrumentHooks :: toggle_collect ( ) ; // Pause collection
186+
187+ loop {
188+ rounds += 1 ;
189+
190+ run_iteration ( ) ;
191+
192+ let within_rounds = max_rounds. map_or ( true , |max| rounds < max) ;
193+ let within_duration =
194+ max_duration. map_or ( true , |max| rounds_start_time. elapsed ( ) < max) ;
195+ if !( within_rounds && within_duration) {
196+ break ;
197+ }
198+ }
199+
156200 codspeed. end_benchmark ( ) ;
157- divan:: black_box ( output) ;
158201 }
159202}
0 commit comments