@@ -12,7 +12,7 @@ use metadata::PerfMetadata;
1212use perf_map:: ProcessSymbols ;
1313use shared:: Command as FifoCommand ;
1414use std:: collections:: HashSet ;
15- use std:: path:: PathBuf ;
15+ use std:: path:: { Path , PathBuf } ;
1616use std:: process:: Command ;
1717use std:: time:: Duration ;
1818use std:: { cell:: OnceCell , collections:: HashMap , process:: ExitStatus } ;
@@ -31,48 +31,51 @@ pub mod unwind_data;
3131
3232const PERF_DATA_PREFIX : & str = "perf.data." ;
3333
34- struct EnvGuard ;
34+ struct EnvGuard {
35+ post_bench_script : PathBuf ,
36+ }
3537
3638impl EnvGuard {
37- fn execute_script_from_env ( script_env_var : & str ) -> anyhow:: Result < ( ) > {
38- let Ok ( script_path) = std:: env:: var ( script_env_var) else {
39- debug ! ( "Couldn't find {script_env_var}, skipping script execution" ) ;
40- return Ok ( ( ) ) ;
41- } ;
42-
43- if script_path. is_empty ( ) {
44- return Ok ( ( ) ) ;
45- }
46-
47- let path = std:: path:: Path :: new ( & script_path) ;
39+ fn execute_script_from_path < P : AsRef < Path > > ( path : P ) -> anyhow:: Result < ( ) > {
40+ let path = path. as_ref ( ) ;
4841 if !path. exists ( ) || !path. is_file ( ) {
49- warn ! ( "Script not found or not a file: {}" , script_path ) ;
42+ warn ! ( "Script not found or not a file: {}" , path . display ( ) ) ;
5043 return Ok ( ( ) ) ;
5144 }
5245
53- let output = Command :: new ( "bash" ) . args ( [ & script_path ] ) . output ( ) ?;
46+ let output = Command :: new ( "bash" ) . args ( [ & path ] ) . output ( ) ?;
5447 if !output. status . success ( ) {
5548 info ! ( "stdout: {}" , String :: from_utf8_lossy( & output. stdout) ) ;
5649 error ! ( "stderr: {}" , String :: from_utf8_lossy( & output. stderr) ) ;
57- bail ! ( "Failed to execute script: {}" , script_path ) ;
50+ bail ! ( "Failed to execute script: {}" , path . display ( ) ) ;
5851 }
5952
6053 Ok ( ( ) )
6154 }
6255
63- pub fn setup ( ) -> Self {
64- if let Err ( e) = Self :: execute_script_from_env ( "CODSPEED_PRE_STARTUP_SCRIPT" ) {
65- warn ! ( "Failed to execute pre-startup script: {}" , e) ;
56+ pub fn setup_with_scripts < P : AsRef < Path > > ( pre_bench_script : P , post_bench_script : P ) -> Self {
57+ if let Err ( e) = Self :: execute_script_from_path ( pre_bench_script . as_ref ( ) ) {
58+ warn ! ( "Failed to execute pre-bench script: {}" , e) ;
6659 println ! ( "asdf: {e}" ) ;
6760 }
68- Self
61+
62+ Self {
63+ post_bench_script : post_bench_script. as_ref ( ) . to_path_buf ( ) ,
64+ }
65+ }
66+
67+ pub fn setup ( ) -> Self {
68+ Self :: setup_with_scripts (
69+ "/usr/local/bin/codspeed-pre-bench" ,
70+ "/usr/local/bin/codspeed-post-bench" ,
71+ )
6972 }
7073}
7174
7275impl Drop for EnvGuard {
7376 fn drop ( & mut self ) {
74- if let Err ( e) = Self :: execute_script_from_env ( "CODSPEED_POST_CLEANUP_SCRIPT" ) {
75- warn ! ( "Failed to execute post-cleanup script: {}" , e) ;
77+ if let Err ( e) = Self :: execute_script_from_path ( & self . post_bench_script ) {
78+ warn ! ( "Failed to execute post-bench script: {}" , e) ;
7679 }
7780 }
7881}
@@ -435,28 +438,6 @@ mod tests {
435438 os:: unix:: fs:: PermissionsExt ,
436439 } ;
437440
438- fn with_env < F > ( vars : & [ ( & str , & str ) ] , mut f : F )
439- where
440- F : FnMut ( ) ,
441- {
442- let original_vars: Vec < ( & str , std:: result:: Result < String , std:: env:: VarError > ) > =
443- vars. iter ( ) . map ( |( k, _) | ( * k, std:: env:: var ( * k) ) ) . collect ( ) ;
444-
445- for ( k, v) in vars {
446- std:: env:: set_var ( k, v) ;
447- }
448-
449- f ( ) ;
450-
451- for ( k, v) in original_vars {
452- if let Ok ( val) = v {
453- std:: env:: set_var ( k, val) ;
454- } else {
455- std:: env:: remove_var ( k) ;
456- }
457- }
458- }
459-
460441 #[ test]
461442 fn test_env_guard_no_crash ( ) {
462443 fn create_run_script ( content : & str ) -> anyhow:: Result < NamedTempFile > {
@@ -484,20 +465,9 @@ mod tests {
484465 ) )
485466 . unwrap ( ) ;
486467
487- let env_vars = [
488- (
489- "CODSPEED_PRE_STARTUP_SCRIPT" ,
490- & * pre_script. path ( ) . to_string_lossy ( ) ,
491- ) ,
492- (
493- "CODSPEED_POST_CLEANUP_SCRIPT" ,
494- & post_script. path ( ) . to_string_lossy ( ) ,
495- ) ,
496- ] ;
497-
498- with_env ( & env_vars, || {
499- let _guard = EnvGuard :: setup ( ) ;
500- } ) ;
468+ {
469+ let _guard = EnvGuard :: setup_with_scripts ( pre_script. path ( ) , post_script. path ( ) ) ;
470+ }
501471
502472 let mut result = String :: new ( ) ;
503473 tmp_dst. read_to_string ( & mut result) . unwrap ( ) ;
0 commit comments