@@ -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,48 @@ 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 ( "analysis" ) => ( None , Some ( Duration :: from_millis ( 100 ) ) ) ,
177+ Ok ( "memory" ) => ( Some ( 1 ) , None ) ,
178+ _ => unreachable ! ( ) ,
179+ } ;
180+ let mut rounds = 0 ;
181+ let rounds_start_time = Instant :: now ( ) ;
182+
183+ codspeed. start_benchmark ( uri) ;
184+ InstrumentHooks :: toggle_collect ( ) ; // Pause collection
185+
186+ loop {
187+ rounds += 1 ;
188+
189+ run_iteration ( ) ;
190+
191+ let within_rounds = max_rounds. map_or ( true , |max| rounds < max) ;
192+ let within_duration =
193+ max_duration. map_or ( true , |max| rounds_start_time. elapsed ( ) < max) ;
194+ if !( within_rounds && within_duration) {
195+ break ;
196+ }
197+ }
198+
156199 codspeed. end_benchmark ( ) ;
157- divan:: black_box ( output) ;
158200 }
159201}
0 commit comments